plotly / plotly/dash

[BUG] Callbacks triggered unnecessarily with MATCH (pattern-matching callbacks)

Open
#1,681 8 comments 0 reactions 0 assignees View on GitHub

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:

  1. Select a value from the dropdown. The input next to it is populated with an initial value
  2. Change that value (e.g. just increment it by 1)
  3. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.