dcc.Checklist: how to ensure that one option is ALWAYS ticked?

I have the same question as this person: Require at least one checked checkbox in a checklist

I have a checklist with 5 options, and at least one (it can be any of them) needs to remain checked.

Any ideas on how I could accomplish that? dcc.RadioItems doesn’t logically make sense for my use case.

Hey @the.oldest.house
I am not sure if this is the best solution but it’s the one I got!

Because we have no way to set that at least one should be active and we can’t know which value was the last one to be unchecked, we should store the data!

I have a checklist and a dcc.Store which has the active value of the checklist
in the callback, the input is the checklist value, and I get the store data in a state; that way, I know which is the last value to get unchecked.

inside the callback, I check if the active checklist items are lower than 1, you can also just check if it’s 0.
if it is, then we want to return the dcc store data which contains the last item that got unchecked.

If it’s higher than 1 (not 0) then we return the checklist value to the dcc.store as well.

app.layout = html.Div([
    dcc.Checklist(
        options=['New York City', 'Montreal', 'San Francisco'],
        value=['Montreal'],
        id = 'checklist'
    ),
    dcc.Store(data=['Montreal'], id='checklist_active')
])


@app.callback(
    Output('checklist', 'value'),
    Output('checklist_active', 'data'),
    Input('checklist', 'value'),
    State('checklist_active', 'data')
)
def update_checklist(value, active):
    if len(value) < 1:
        return active, active
    else:
        return value, value
1 Like