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/multi set props #2856

Merged
merged 7 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
- [#2860](https://github.com/plotly/dash/pull/2860) Fix dcc.Loading to apply overlay_style only to the children and not the spinner. Fixes [#2858](https://github.com/plotly/dash/issues/2858)
- [#2854](https://github.com/plotly/dash/pull/2854) Fix dcc.Dropdown resetting empty values to null and triggering callbacks. Fixes [#2850](https://github.com/plotly/dash/issues/2850)
- [#2859](https://github.com/plotly/dash/pull/2859) Fix base patch operators. fixes [#2855](https://github.com/plotly/dash/issues/2855)
- [#2856](https://github.com/plotly/dash/pull/2856) Fix multiple consecutive calls with same id to set_props only keeping the last props. Fixes [#2852](https://github.com/plotly/dash/issues/2852)

## [2.17.0] - 2024-05-03

Expand Down
6 changes: 5 additions & 1 deletion dash/_callback_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,11 @@ def timing_information(self):
def set_props(self, component_id: typing.Union[str, dict], props: dict):
ctx_value = _get_context_value()
_id = stringify_id(component_id)
ctx_value.updated_props[_id] = props
existing = ctx_value.updated_props.get(_id)
if existing is not None:
ctx_value.updated_props[_id] = {**existing, **props}
else:
ctx_value.updated_props[_id] = props


callback_context = CallbackContext()
Expand Down
6 changes: 6 additions & 0 deletions dash/long_callback/_proxy_set_props.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ class ProxySetProps(dict):
def __init__(self, on_change):
super().__init__()
self.on_change = on_change
self._data = {}

def __setitem__(self, key, value):
self.on_change(key, value)
T4rk1n marked this conversation as resolved.
Show resolved Hide resolved
self._data.setdefault(key, {})
self._data[key] = {**self._data[key], **value}

def get(self, key):
return self._data.get(key)
23 changes: 23 additions & 0 deletions tests/integration/callbacks/test_arbitrary_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,26 @@ def on_click(clicked):
".dash-fe-error__title",
"Callback error with no output from input start.n_clicks",
)


def test_arb006_multi_set_props(dash_duo):
app = Dash()

app.layout = [
html.Button("start", id="start"),
html.Div("initial", id="output"),
]

@app.callback(
Input("start", "n_clicks"),
)
def on_click(_):
set_props("output", {"children": "changed"})
set_props("output", {"style": {"background": "rgb(255,0,0)"}})
T4rk1n marked this conversation as resolved.
Show resolved Hide resolved

dash_duo.start_server(app)
dash_duo.wait_for_element("#start").click()
dash_duo.wait_for_text_to_equal("#output", "changed")
dash_duo.wait_for_style_to_equal(
"#output", "background-color", "rgba(255, 0, 0, 1)"
)
2 changes: 2 additions & 0 deletions tests/integration/long_callback/app_arbitrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
Input("start", "n_clicks"),
prevent_initial_call=True,
background=True,
interval=500,
)
def on_click(_):
set_props("secondary", {"children": "first"})
set_props("secondary", {"style": {"background": "red"}})
time.sleep(2)
set_props("secondary", {"children": "second"})
return "completed"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ def test_lcbc017_long_callback_set_props(dash_duo, manager):
dash_duo.find_element("#start").click()

dash_duo.wait_for_text_to_equal("#secondary", "first")
dash_duo.wait_for_style_to_equal(
"#secondary", "background-color", "rgba(255, 0, 0, 1)"
)
dash_duo.wait_for_text_to_equal("#output", "initial")
dash_duo.wait_for_text_to_equal("#secondary", "second")
dash_duo.wait_for_text_to_equal("#output", "completed")
Expand Down