Skip to content
Advertisement

Python Dash: Exclude option from one dropdown when chosing the same option in another dropdown

I am working on a Python Dash dashboard and have two dropdowns with the same options:

enter image description here

When I select an option in the first dropdown, how do I exclude the same option from the second dropdown? – So that I cannot choose the same material with both dropdowns.

Where and how would I do this in my code? I suppose it must be somewhere in my callbacks?

Any help is highly appreciated.

@app.callback(
    dash.dependencies.Output('dropdown3', 'options'),    
    [dash.dependencies.Input('dropdown2', 'value')])

def set_options1(first_dropdown):
    return [{'label': i, 'value': i} for i in all_options[first_dropdown]]

@app.callback(
    dash.dependencies.Output('dropdown3', 'value'),
    [dash.dependencies.Input('dropdown3', 'options')])

def set_1_value(available_options):
    return available_options[0]['value']

Advertisement

Answer

You can certainly do this, and you’re most of the way there. Use the value from the first dropdown to modify the options for the second one. Here’s a way:

def set_options1(first_dropdown):
    return [
        {'label': i, 'value': i} 
        for i in all_options[first_dropdown] if i != first_dropdown
    ]
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement