Skip to content

Commit a6d9c99

Browse files
authored
Merge pull request #2491 from plotly/fix/#2488
Fix clientside inline function name.
2 parents 0377ab6 + fa32db0 commit a6d9c99

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

Diff for: CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
All notable changes to `dash` will be documented in this file.
33
This project adheres to [Semantic Versioning](https://semver.org/).
44

5+
## [UNRELEASED]
6+
7+
## Fixed
8+
9+
- [#2491](https://github.com/plotly/dash/pull/2491) Fix clientside inline function name not found, fix [#2488](https://github.com/plotly/dash/issues/2488)
10+
511
## [2.9.2] - 2023-03-29
612

713
## Fixed

Diff for: dash/_callback.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import collections
2-
import uuid
2+
import hashlib
33
from functools import wraps
44

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

540540
inline_scripts.append(
541541
_inline_clientside_template.format(
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import pytest
2+
from dash import Dash, html, Output, Input
3+
4+
5+
@pytest.mark.skip(reason="Hot-reload & clientside callbacks doesn't work properly")
6+
def test_clrs001_clientside_inline_restarts(dash_duo_mp):
7+
# FIXME find another way to test clientside callbacks restarts
8+
reloads = 0
9+
10+
def create_app():
11+
nonlocal reloads
12+
13+
app = Dash(__name__)
14+
15+
app.layout = html.Div([
16+
html.Button("Click", id="click"),
17+
html.Div(id="output"),
18+
html.Div(reloads, id="reload")
19+
])
20+
21+
app.clientside_callback(
22+
"(n_clicks) => `clicked ${n_clicks}`",
23+
Output("output", "children"),
24+
Input("click", "n_clicks"),
25+
prevent_initial_call=True
26+
)
27+
reloads += 1
28+
return app
29+
30+
hot_reload_settings = dict(
31+
dev_tools_hot_reload=True,
32+
dev_tools_ui=True,
33+
dev_tools_serve_dev_bundles=True,
34+
dev_tools_hot_reload_interval=0.1,
35+
dev_tools_hot_reload_max_retry=100,
36+
)
37+
38+
dash_duo_mp.start_server(
39+
create_app(),
40+
**hot_reload_settings
41+
)
42+
dash_duo_mp.find_element("#click").click()
43+
dash_duo_mp.wait_for_text_to_equal("#output", "clicked 1")
44+
45+
dash_duo_mp.server.stop()
46+
47+
dash_duo_mp.start_server(
48+
create_app(),
49+
navigate=False,
50+
**hot_reload_settings
51+
)
52+
dash_duo_mp.wait_for_text_to_equal("#reload", "1")
53+
dash_duo_mp.find_element("#click").click()
54+
# reloaded so 1 again.
55+
dash_duo_mp.wait_for_text_to_equal("#output", "clicked 1")

0 commit comments

Comments
 (0)