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

[BUG] A callback gets triggered without a corresponding event #2452

Closed
sergiykhan opened this issue Mar 11, 2023 · 5 comments
Closed

[BUG] A callback gets triggered without a corresponding event #2452

sergiykhan opened this issue Mar 11, 2023 · 5 comments

Comments

@sergiykhan
Copy link

Software

dash                      2.8.1
dash-bootstrap-components 1.3.1
dash-core-components      2.0.0
dash-html-components      2.0.0
dash-table                5.0.0

Describe the bug

The value of the dropdown menu gets reset by a callback function that is only supposed to be triggered by an independent button in the interface. As you can see from the video recording below, typing in the drop-down menu triggers the button pressed event. There is no apparent connection between the two.

The Callback Graph is also recorded in the video below, showing the callback is called while no button is pressed.

Expected behavior

The callback on_button (see code below) should only be triggered when the button is actually pressed.

Console output

Button pressed 0 None
Button pressed 0 None
Button pressed 0 None
Button pressed 0 None
Button pressed 0 None

Video

screen-capture.mp4

Sample code

from dash import Dash, dcc, html, dcc, Input, Output, ctx
from dash.exceptions import PreventUpdate

options = [
    {"label": "aa1", "value": "aa1"},
    {"label": "aa2", "value": "aa2"},
    {"label": "aa3", "value": "aa3"},
    {"label": "best value", "value": "bb1"},
    {"label": "better value", "value": "bb2"},
    {"label": "bye", "value": "bb3"},
]

app = Dash(__name__)
app.layout = html.Div([
    html.Div([
        "Single dynamic Dropdown",
        dcc.Dropdown(id="my-dynamic-dropdown")
    ], style={'width': 200, 'marginLeft': 20, 'marginTop': 20}),
    html.Button(
        'Reset',
        id='button',
        n_clicks=0,
    ),
])


@app.callback(
    Output("my-dynamic-dropdown", "options"),
    Input("my-dynamic-dropdown", "search_value")
)
def update_options(search_value):
    if not search_value:
        raise PreventUpdate
    return [o for o in options if search_value in o["label"]]


@app.callback(
    Output('my-dynamic-dropdown', 'value'),
    Input('button', 'n_clicks'),
)
def on_button(n_clicks):
    print('Button pressed', n_clicks, ctx.triggered_id)
    return None


if __name__ == "__main__":
    app.run_server(debug=True, port=5000)
@sergiykhan
Copy link
Author

I think it's likely a duplicate of issue #2428

@T4rk1n
Copy link
Contributor

T4rk1n commented Mar 17, 2023

I think it's likely a duplicate of issue #2428

Yes duplicate of #2428, fixed in 2.9

@T4rk1n T4rk1n closed this as completed Mar 17, 2023
@sergiykhan
Copy link
Author

I have just tested it in Dash 2.9.2, and the bug is still present. It appeared in Dash 2.7.1. Downgrading to 2.7.0 resolves the issue.

@sergiykhan
Copy link
Author

@T4rk1n should I create a new issue or you can re-open this one (or #2428)?

@T4rk1n
Copy link
Contributor

T4rk1n commented Apr 11, 2023

@sergiykhan No, seems like I fixed the inputs but Output behave strangely when callbacks share the same dropdown/checklist as Output.

#2487 is still open and contains some more info, so maybe add a link to this example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants