Skip to content

[BUG] Updating options of a dropdown triggers update on value of the dropdown for pattern-matching callbacks #2487

Closed
@ltsimple

Description

If there are two pattern-matching callbacks for a dropdowns' options and value, the callback that changes the dropdown's options also triggers the other callback that uses the dropdown's value.

import dash
from dash import dcc, html, Input, Output, MATCH, ALL
import random

app = dash.Dash(__name__)
app.layout = html.Div([
    dcc.Dropdown(id="d1", options=[1, 2, 3]),
    dcc.Dropdown(id={'type': 'd2', "id": 1}, options=[]),
    dcc.Dropdown(id={'type': 'd2', "id": 2}, options=[]),
    html.Pre(id={"type": 'out', "id": 1}, style={'margin-top': '100px'}),
    html.Pre(id={"type": 'out', "id": 2}, style={'margin-top': '100px'})
])


@app.callback(
    Output({"type": 'd2', "id": ALL}, 'options'),
    Input('d1', 'value'),
    prevent_initial_call=True
)
def update_options(val):
    return [{
        1: ['a', 'b'],
        2: ['A', 'B'],
        3: ['x', 'y'],
    }[val] for _ in range(2)]


@app.callback(
    Output({"type": 'out', "id": MATCH}, 'children'),
    Input({"type": 'd2', "id": MATCH}, 'value'),
    prevent_initial_call=True
)
def update(_):
    return f'got_triggered_{random.randint(1, 10000)}'


if __name__ == "__main__":
    app.run_server(debug=True)

In the example above, when I use the dropdown 'd1' to change the options of 'd2's, the first callback triggers as expected but after that second callback also triggers where it is not supposed to.

pip list

  • asyncstdlib 3.10.6
  • boto3 1.26.96
  • botocore 1.29.96
  • Brotli 1.0.9
  • cachelib 0.9.0
  • cachetools 5.3.0
  • certifi 2022.12.7
  • charset-normalizer 3.1.0
  • click 8.1.3
  • colorama 0.4.6
  • confuse 2.0.0
  • Cython 0.29.33
  • dash 2.9.2
  • dash-bootstrap-components 1.4.1
  • dash-core-components 2.0.0
  • dash-html-components 2.0.0
  • dash-table 5.0.0
  • dnspython 2.3.0
  • EditorConfig 0.12.3
  • Flask 2.2.3
  • Flask-Caching 2.0.2
  • Flask-Compress 1.13
  • gunicorn 20.1.0
  • idna 3.4
  • itsdangerous 2.1.2
  • Jinja2 3.1.2
  • jmespath 1.0.1
  • jsbeautifier 1.14.7
  • MarkupSafe 2.1.2
  • more-itertools 9.1.0
  • numpy 1.24.2
  • pandas 1.5.3
  • pip 23.0.1
  • plotly 5.13.1
  • pymongo 4.3.3
  • python-amazon-sp-api 0.18.2
  • python-dateutil 2.8.2
  • python-dotenv 1.0.0
  • pytz 2023.2
  • PyYAML 6.0
  • requests 2.28.2
  • s3transfer 0.6.0
  • setuptools 65.5.0
  • six 1.16.0
  • tenacity 8.2.2
  • urllib3 1.26.15
  • Werkzeug 2.2.3

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions