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

Fix clientside inline function name. #2491

Merged
merged 5 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions dash/_callback.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import collections
import uuid
import hashlib
from functools import wraps

import flask
Expand Down Expand Up @@ -534,8 +534,8 @@ def register_clientside_callback(
# name, then inject the code.
if isinstance(clientside_function, str):
namespace = "_dashprivate_clientside_funcs"
# Just make sure every function has a different name if not provided.
function_name = uuid.uuid4().hex
# Create a hash from the function, it will be the same always
function_name = hashlib.md5(clientside_function.encode("utf-8")).hexdigest()

inline_scripts.append(
_inline_clientside_template.format(
Expand Down
51 changes: 51 additions & 0 deletions tests/integration/clientside/test_clientside.py
Original file line number Diff line number Diff line change
Expand Up @@ -829,3 +829,54 @@ def test_clsd019_clientside_inline_promise(dash_duo):

dash_duo.start_server(app)
dash_duo.wait_for_text_to_equal("#output-div", "initial-inline")


def test_clsd020_clientside_inline_restarts(dash_duo_mp):
reloads = 0

def create_app():
nonlocal reloads

app = Dash(__name__)

app.layout = html.Div([
html.Button("Click", id="click"),
html.Div(id="output"),
html.Div(reloads, id="reload")
])

app.clientside_callback(
"(n_clicks) => `clicked ${n_clicks}`",
Output("output", "children"),
Input("click", "n_clicks"),
prevent_initial_call=True
)
reloads += 1
return app

hot_reload_settings = dict(
dev_tools_hot_reload=True,
dev_tools_ui=True,
dev_tools_serve_dev_bundles=True,
dev_tools_hot_reload_interval=0.1,
dev_tools_hot_reload_max_retry=100,
)

dash_duo_mp.start_server(
create_app(),
**hot_reload_settings
)
dash_duo_mp.find_element("#click").click()
dash_duo_mp.wait_for_text_to_equal("#output", "clicked 1")

dash_duo_mp.server.stop()

dash_duo_mp.start_server(
create_app(),
navigate=False,
**hot_reload_settings
)
dash_duo_mp.wait_for_text_to_equal("#reload", "1")
dash_duo_mp.find_element("#click").click()
# reloaded so 1 again.
dash_duo_mp.wait_for_text_to_equal("#output", "clicked 1")