[BUG] Callbacks triggered unnecessarily with MATCH (pattern-matching callbacks)
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 24.4k
- Forks
- 2.3k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 13
Description
Describe your context
dash 1.20.0
dash-bootstrap-components 0.12.2
dash-core-components 1.16.0
dash-html-components 1.1.3
dash-renderer 1.9.1
dash-table 4.11.3
Describe the bug
In the example below, the callback using MATCH is triggered for all matching components, even ones that haven't changed and don't need updating.
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State, MATCH, ALL
app = dash.Dash(__name__, suppress_callback_exceptions=True)
app.layout = html.Div(
[
html.Button("Add Filter", id="dynamic-add-filter", n_clicks=0),
html.Div(id="dynamic-dropdown-container", children=[]),
]
)
@app.callback(
Output("dynamic-dropdown-container", "children"),
Input("dynamic-add-filter", "n_clicks"),
State("dynamic-dropdown-container", "children"),
)
def display_dropdowns(n_clicks, children):
new_element = html.Div(
[
dcc.Dropdown(
id={
"type": "dynamic-dropdown",
"index": n_clicks,
},
options=[
{"label": i, "value": i} for i in ["NYC", "MTL", "LA", "TOKYO"]
],
style={"width": "200px", "display": "inline-block"},
),
dcc.Input(
id={
"type": "dynamic-input",
"index": n_clicks,
},
type="number",
style={"width": "200px", "display": "inline-block"},
),
html.Div(
id={
"type": "dynamic-output",
"index": n_clicks,
}
),
]
)
children.append(new_element)
return children
@app.callback(
Output({"type": "dynamic-input", "index": MATCH}, "value"),
Input({"type": "dynamic-dropdown", "index": MATCH}, "value"),
)
def set_initial_value(city):
values = {
"NYC": 10,
"MTL": 20,
"LA": 30,
"TOKYO": 40,
None: None,
}
return values[city]
@app.callback(
Output({"type": "dynamic-output", "index": MATCH}, "children"),
Input({"type": "dynamic-dropdown", "index": MATCH}, "value"),
State({"type": "dynamic-dropdown", "index": MATCH}, "id"),
)
def display_output(value, id):
return html.Div("Dropdown {} = {}".format(id["index"], value))
if __name__ == "__main__":
app.run_server(debug=True)
Example:
- Select a value from the dropdown. The input next to it is populated with an initial value
- Change that value (e.g. just increment it by 1)
- Click "Add Filter". The new row is added correctly, but the first input is reset to its initial value even though the first dropdown has not changed.
Expected behavior
The set_initial_value function should only be called for components that have changed. It seems to be called for all matching components even if only one of them has changed.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the supplied Dash reproducer and observe the MATCH callbacks for set_initial_value and display_output when a new filter is added. Trace the callback dispatch path to determine why unchanged matching components run, then verify that adding a filter only invokes callbacks for the changed component while preserving edited values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- frontend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100