Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[BUG] search_value triggers unrelated callbacks, making dropdown search unusable #2428

Closed
oegedijk opened this issue Feb 17, 2023 · 7 comments

Comments

@oegedijk
Copy link

While chasing down a bug in explainerdashboard, I noticed that when you have a dynamic server side dropdown search, and also have a callback targeting dropdown.value, then this latter gets triggered everytime you search in the dropdown. Usually with unwanted side effects. In this case it makes the dropdown search completely unusable.

Below an example, with a dropdown, and a select random item button. Searching in the dropdown triggers the button callback. Including a callback.context check to see if the button callback was indeed triggered by the button n_clicks input seems to fix the problem.

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

options = [str(i) for i in range(1000)]

app = Dash(__name__)
app.layout = html.Div([
    html.Div(id='dropdown-output'),
    html.Button("Random Button", id='button'),
    dcc.Checklist(options=['fix bug'], value=[], id='fix-bug'),
    dcc.Dropdown(id="index-dropdown", value="654")   
])
    
@app.callback(
    Output("index-dropdown", "value"),
    Input("button", "n_clicks"),
    State('fix-bug', 'value'),
)
def random_index(n_clicks, fix_bug):
    print("triggered random click!")
    if fix_bug and dash.callback_context.triggered_id != 'button':
       raise PreventUpdate
    return random.choice(options)

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

@app.callback(
    Output("dropdown-output", "children"),
    Input("index-dropdown", "value")
)
def send_output(index):
    return f"selected {index}!"

app.run_server(port=8051, debug=False)
@T4rk1n
Copy link
Contributor

T4rk1n commented Feb 17, 2023

Duplicate of #2411

@T4rk1n T4rk1n marked this as a duplicate of #2411 Feb 17, 2023
@T4rk1n
Copy link
Contributor

T4rk1n commented Mar 10, 2023

Fixed along with #2411

@sergiykhan
Copy link

I have just used an example provided by @oegedijk in Dash 2.9.2 and the problem is still there.

@bambinobino
Copy link

Same thing here, having the problem still in 2.9.2

@oegedijk
Copy link
Author

oegedijk commented Jul 6, 2023

@T4rk1n could we add a failing test for this so that it will get fixed in the next release?

@bambinobino
Copy link

@T4rk1n could we add a failing test for this so that it will get fixed in the next release?

I believe this is fixed in Dash v2.10.2

@sergiykhan
Copy link

Indeed, fixed in 2.10.1
https://github.com/plotly/dash/releases

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

No branches or pull requests

4 participants