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 allow_duplicate output with clientside callback. #2471

Merged
merged 4 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
All notable changes to `dash` will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/).

## [UNRELEASED
## [UNRELEASED]

## Fixed

- [#2479](https://github.com/plotly/dash/pull/2479) Fix `KeyError` "Callback function not found for output [...], , perhaps you forgot to prepend the '@'?" issue when using duplicate callbacks targeting the same output. This issue would occur when the app is restarted or when running with multiple `gunicorn` workers.
- [#2471](https://github.com/plotly/dash/pull/2471) Fix `allow_duplicate` output with clientside callback, fix [#2467](https://github.com/plotly/dash/issues/2467)

## [2.9.1] - 2023-03-17

Expand Down
15 changes: 6 additions & 9 deletions dash/_callback.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import collections
import uuid
from functools import wraps

import flask
Expand Down Expand Up @@ -530,18 +531,14 @@ 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):

out0 = output
if isinstance(output, (list, tuple)):
out0 = output[0]

namespace = f"_dashprivate_{out0.component_id}"
function_name = out0.component_property
namespace = "_dashprivate_clientside_funcs"
# Just make sure every function has a different name if not provided.
function_name = uuid.uuid4().hex

inline_scripts.append(
_inline_clientside_template.format(
namespace=namespace.replace('"', '\\"'),
function_name=function_name.replace('"', '\\"'),
namespace=namespace,
function_name=function_name,
clientside_function=clientside_function,
)
)
Expand Down
8 changes: 6 additions & 2 deletions dash/dash-renderer/src/actions/callbacks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ const getVals = (input: any) =>
const zipIfArray = (a: any, b: any) =>
Array.isArray(a) ? zip(a, b) : [[a, b]];

function cleanOutputProp(property: string) {
return property.split('@')[0];
}

async function handleClientside(
dispatch: any,
clientside_function: any,
Expand Down Expand Up @@ -275,7 +279,7 @@ async function handleClientside(
const idStr = stringifyId(id);
const dataForId = (result[idStr] = result[idStr] || {});
if (retij !== dc.no_update) {
dataForId[property] = retij;
dataForId[cleanOutputProp(property)] = retij;
}
});
});
Expand Down Expand Up @@ -704,7 +708,7 @@ export function executeCallback(
// Layout may have changed.
const currentLayout = getState().layout;
flatten(outputs).forEach((out: any) => {
const propName = out.property.split('@')[0];
const propName = cleanOutputProp(out.property);
const outputPath = getPath(paths, out.id);
const previousValue = path(
outputPath.concat(['props', propName]),
Expand Down
32 changes: 32 additions & 0 deletions tests/integration/test_patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,35 @@ def on_click(_):

dash_duo_mp.wait_for_element("#click2").click()
dash_duo_mp.wait_for_text_to_equal("#output", "click 2")


def test_pch005_clientside_duplicate(dash_duo):
app = Dash(__name__)

app.layout = html.Div(
[
html.Button("Click 1", id="click1"),
html.Button("Click 2", id="click2"),
html.Div("initial", id="output"),
]
)

app.clientside_callback(
"function onClickOne() { return 'click1';}",
Output("output", "children", allow_duplicate=True),
Input("click1", "n_clicks"),
prevent_initial_call=True,
)
app.clientside_callback(
"function onClickTwo(){ return 'click2';}",
Copy link
Member

Choose a reason for hiding this comment

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

Would a more comprehensive test be to have an anonymous function? To test the uuid.uuid4.hex() thing as above. So like () => { return 'click2' }?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really, the function name is actually discarded/unimportant when the script inline, I had fixed on the rendered first but the test still failed so I tried a few thing like adding name on the function to found out where it was getting the name and replaced with the hex.

Output("output", "children", allow_duplicate=True),
Input("click2", "n_clicks"),
prevent_initial_call=True,
)
dash_duo.start_server(app)

dash_duo.find_element("#click1").click()
dash_duo.wait_for_text_to_equal("#output", "click1")

dash_duo.find_element("#click2").click()
dash_duo.wait_for_text_to_equal("#output", "click2")