Prevent dcc.dropdown from being empty

Greetings

In the attached image, is it possible to prevent the ‘driver’ dropdown from being empty i.e. prevent the user from removing “Lewis Hamilton”? I thought of trying to prevent the resulting empty array(if “Lewis Hamilton” was cleared) but I am not sure how to reflect it graphically to the user that it cannot be cleared. My reason for doing this, is to prevent an empty plot from being rendered.

Any help would be appreciated. Thank you for reading

Hi @Suva1

Try setting the clearable prop to False, for example:

dcc.Dropdown(
    options=[
        {'label': 'New York City', 'value': 'NYC'},
        {'label': 'Montreal', 'value': 'MTL'},
        {'label': 'San Francisco', 'value': 'SF'},
    ],
    value='MTL',
    clearable=False
)  

Hi @AnnMarieW

Thank you for replying.

I have tried that but from what I can see, and from what’s been said by the devs, the ‘clearable’ prop only prevents the dropdown from having the ‘x’ which could clear any number of elements at the time it is pressed (clearable is set to True, for the other dropdowns to show this).

Oh, yes — you are correct. If the Dropdown is multi=False and clearable=False, then it’s not possible to clear the selection. However with 'multi=True` it’s possible to clear it - otherwise it would be awkward to change that first selection.

If you always want to plot the same driver as a default, that would be fairly easy to make that a default plot. But that might be confusing too. Is it bad to show an empty plot if the selection is empty?

Come to think of it, maybe I am over-engineering the situation and that would not look bad if its what the code supposed to do (showing an empty plot for an empty list). I think I will move on from this. Thank you for your input though.