I’m updating options for dropdown via a callback, as soon as any item is selected from dropdown menu list, a new option list is returned to the same dropdown but it clears the items that were previously selected.
Q) How do I update the dropdown option/list without clearing the items that previously selected ?
In the MRE posted below , dropdown is first populated with ‘option_1’ list and then upon selection of any value, a new list is passed ‘option_2’ . Since multi is set to TRUE , the item(s) selected from ‘option_1’ list is cleared and dropdown menu/list is updated as expected but I want items that were previously selected (from ‘option_1’) to also be there.
from dash import Dash, dcc, html, dcc, Input, Output, State, callback
from dash.exceptions import PreventUpdate
option_1 = [
{"label": "Top level", "value": "TP"},
{"label": "Top level 2", "value": "TP2"},
{"label": "Top level 3", "value": "TP3"},
]
option_2 = [
{"label": "options1", "value": "op1"},
{"label": "options2", "value": "op2"},
{"label": "options3", "value": "op3"},
]
app = Dash(__name__)
app.layout = html.Div([
html.Div([
"Dynamic Dropdown",
dcc.Dropdown(id="dyn-dropdown",
multi=True,
searchable=False,
options=option_1),
]),
])
@callback(
Output("dyn-dropdown", "options"),
Input("dyn-dropdown", "value"),
prevent_initial_call=True
)
def update_multi_options(value):
if not value:
raise PreventUpdate
return option_2
if __name__ == "__main__":
app.run(debug=True)
As previously described, the gif demonstrates that ‘Top level’ option is cleared as soon as option list is updated via callback, but I want ‘top level’ option to be there as well.
What I want is to create a sort of ‘filtered’ menu list, where upon selecting a single item in dropdown menu, it updates the same dropdown with new options while also keeping the previously selected options.
Any assistance would be appreciated please.