Skip to content

Commit

Permalink
Merge pull request plotly#2491 from plotly/fix/plotly#2488
Browse files Browse the repository at this point in the history
Fix clientside inline function name.
  • Loading branch information
T4rk1n committed Apr 4, 2023
2 parents 0377ab6 + fa32db0 commit a6d9c99
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
All notable changes to `dash` will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/).

## [UNRELEASED]

## Fixed

- [#2491](https://github.com/plotly/dash/pull/2491) Fix clientside inline function name not found, fix [#2488](https://github.com/plotly/dash/issues/2488)

## [2.9.2] - 2023-03-29

## Fixed
Expand Down
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
55 changes: 55 additions & 0 deletions tests/integration/clientside/test_clientside_restarts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import pytest
from dash import Dash, html, Output, Input


@pytest.mark.skip(reason="Hot-reload & clientside callbacks doesn't work properly")
def test_clrs001_clientside_inline_restarts(dash_duo_mp):
# FIXME find another way to test clientside callbacks restarts
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")

0 comments on commit a6d9c99

Please sign in to comment.