Change dash Checklist values by callback

I would like to change the selected options of an plotly dash checklist by an callback. My minimal working example is as following but it doesn’t work for now. (Values should change by hitting the ‘load’ button → at loading the page, nothing ist selected, after click the button ‘SF’ should be selected):

import dash

app = dash.Dash(__name__)

app.layout = html.Div([

        {'label': 'New York City', 'value': 'NYC'},
        {'label': 'Montréal', 'value': 'MTL'},
        {'label': 'San Francisco', 'value': 'SF'}
    labelStyle={'display': 'block'},

    html.Button('load', id='load-button', n_clicks=0)

@app.callback(Output('checkliste', 'value'),
              Input('load-button', 'n_clicks'))
def change_values(n_clicks):
    if n_clicks > 1:
        return ['SF']

if __name__ == '__main__':

Hope, someone can help!

HI @HConBike and welcome to the Dash community!

Be sure to check out the Dash tutorial for more details and examples on callbacks.

A couple things to keep in mind

  • All the components used in callbacks must have a unique id . There is no id for the dcc.Checklist in your code
  • There must be a return statement. In this case, there is no return defined if n_clicks < 1
  • if n_clicks > 1 means the button must be clicked twice for something to happen. It should be n_clicks > 0

Try running this example:

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

app = dash.Dash(__name__)

app.layout = html.Div(
                {"label": "New York City", "value": "NYC"},
                {"label": "Montréal", "value": "MTL"},
                {"label": "San Francisco", "value": "SF"},
            labelStyle={"display": "block"},
        html.Button("load", id="load-button", n_clicks=0),

@app.callback(Output("checklist", "value"), Input("load-button", "n_clicks"))
def change_values(n_clicks):
    return ["SF"] if n_clicks > 0 else []

if __name__ == "__main__":
1 Like

Thanks so much! Sometimes it’s the little things that you overlook…

1 Like