🚀 Gen 5 of the leading AI app deployment platform launches October 6. Click for the livestream.

Can the value of a dcc.Dropdown be set via callback?

I assumed any property of layout elements can be changed via callback, so I tried populating the values of a dcc.Dropdown(multi=True) with the id group-code-select on the click of a button:

   @app.callback(
        Output('group-code-select', 'value'),
        [
            Input('group-edit-button', 'n_clicks')
        ],
        [
            State('group-management-dropdown', 'value')
        ]
    )
    def edit_group_codes(n_clicks, group_label):
        if n_clicks:
            groups = dl_core.get_groups()
            group_codes = groups[group_label[1:]].codes
            return group_codes

This does not work. What am I missing?

Maybe you need to convert group_codes into a list?

Yes you definitely can.
Something like

dcc.Dropdown( id="dropdown_id",multi=True,placeholder="Select an Option", ),

and the callback would be something like :
FYI I think you need an input even if its not used

@app.callback(
    Output("dropdown_id", "options"),
    [Input("interval_update", "n_intervals")],
)
def update_dropdown(n_intervals): 
    list_to_use = list() #Pretend this is already populated
    for i in range(0, len(list_to_use)):
        options.append(
            {"label": list_to_use[i][0], "value": list_to_use[i][0]}
        )
1 Like

Yep, as @adi suggests, you want to target the options property of the Dropdown component, filling it with a list of dropdown dictionaries.

There’s a couple of gotchas with this though. Make sure the options property has an initial value in the layout (empty list if you don’t want any initial values). Also, you need to make sure that your callback always returns a list, even if it’s empty.

See my response here: Upload file to update Dropdown component

It seems my question has been unclear: I know it is possible to set the options of a dropdown (the items that can be selected) this way, but what I am asking here is how to set the already selected items (which I assumed is setting the value property).

if you are using a multi-value dropdown, return a list of value(s) (the ones you set in your list of dict options)

else, just return a value