Home > Back-end >  Python Dash: Exclude option from one dropdown when chosing the same option in another dropdown
Python Dash: Exclude option from one dropdown when chosing the same option in another dropdown

Time:10-12

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']

CodePudding user response:

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
    ]
  • Related