Skip to content

Commit

Permalink
Merge pull request #2856 from plotly/fix/multi-set-props
Browse files Browse the repository at this point in the history
Fix/multi set props
  • Loading branch information
T4rk1n authored May 15, 2024
2 parents 2d21e62 + 72707ad commit 993b57f
Showing 6 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

6 changes: 5 additions & 1 deletion dash/_callback_context.py
Original file line number Diff line number Diff line change
@@ -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()
6 changes: 6 additions & 0 deletions dash/long_callback/_proxy_set_props.py
Original file line number Diff line number Diff line change
@@ -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)
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
@@ -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)"}})

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
@@ -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"
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit 993b57f

Please # to comment.