Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generated caller filename hash as clientside callback namespace. #3161

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion dash/_callback.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import collections
import hashlib
from functools import wraps
import importlib
from typing import Callable, Optional, Any

import flask
Expand Down Expand Up @@ -32,6 +33,7 @@
coerce_to_list,
AttributeDict,
clean_property_name,
get_caller_name,
)

from . import _validate
Expand Down Expand Up @@ -618,7 +620,13 @@ def register_clientside_callback(
# If JS source is explicitly given, create a namespace and function
# name, then inject the code.
if isinstance(clientside_function, str):
namespace = "_dashprivate_clientside_funcs"

# Namespace need to be unique and deterministic.
# Generate the hash from the caller filename.
caller = get_caller_name()
module = importlib.import_module(caller)
namespace = hashlib.sha256(module.__file__.encode("utf-8")).hexdigest()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice


# Create a hash from the function, it will be the same always
function_name = hashlib.sha256(clientside_function.encode("utf-8")).hexdigest()

Expand Down