I am working on a Python Dash dashboard and have two dropdowns with the same options:
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.
JavaScript
x
14
14
1
@app.callback(
2
dash.dependencies.Output('dropdown3', 'options'),
3
[dash.dependencies.Input('dropdown2', 'value')])
4
5
def set_options1(first_dropdown):
6
return [{'label': i, 'value': i} for i in all_options[first_dropdown]]
7
8
@app.callback(
9
dash.dependencies.Output('dropdown3', 'value'),
10
[dash.dependencies.Input('dropdown3', 'options')])
11
12
def set_1_value(available_options):
13
return available_options[0]['value']
14
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:
JavaScript
1
6
1
def set_options1(first_dropdown):
2
return [
3
{'label': i, 'value': i}
4
for i in all_options[first_dropdown] if i != first_dropdown
5
]
6