Black Lives Matter. Please consider donating to Black Girls Code today.

Checkboxes don't work, but RadioItems do

Hi,

I want to create a number of checkboxes based on an an input slider, but I stumble upon an “Error loading layout”. When I replace my Checkboxes for RadioItems, it works perfectly, so I don’t really know what I should do anymore.

Sample of my code (I hope this formats correctly):

app = dash.Dash()

app.layout = html.Div([

dcc.Slider(
    id='n_clusters',
    min=0,
    max=10,
    step=1,
    marks=[{'label': i, 'value': i} for i in range(0,10)],
    value=5,
),

html.Hr(),
dcc.Checklist(id='selected_clusters')

])

@app.callback(
Output(‘selected_clusters’, ‘options’),
[Input(‘n_clusters’, ‘value’)])

def display_checkboxes(clusters):
return[{‘label’: i, ‘value’: i} for i in range(clusters)]

if name == ‘main’:
app.run_server()

I can’t wrap my head around why this works perfectly when I change Checklist to RadioItems… Thanks in advance if you can help me!

Edit: tried to fix some layout in my code… Didn’t really work.

Did you try to make sure the items of the dict were strings, i.e,

?

Hi,

Try this:

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

app = dash.Dash()

app.layout = html.Div([

dcc.Slider(
    id='n_clusters',
    min=0,
    max=10,
    step=1,
    marks=[{'label': i, 'value': i} for i in range(0,10)],
    value=5,
),


html.Hr(),
dcc.Checklist(id='selected_clusters'
              , values=[0]
              )
])

@app.callback(
Output('selected_clusters', 'options'),
[Input('n_clusters', 'value')])
def display_checkboxes(clusters):
    return [{'label': i, 'value': i} for i in range(clusters)]

if __name__ == '__main__':
    app.run_server()

The Checklist works if values are defined. I set values=[0] in the checklist. Not sure if it is applicable in your real case.

2 Likes