Am using dash 2.9.2. I came across curious case of dcc.Dropdown option not triggering callback.
Here is simple dash code : (I built this for changing layout from 3 Rows to 2 Rows based on Dropdown). It starts with 3Row layout. When I select 2Rows option, it responds correctly. However after that when I click 3Rows, nothing happens.
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
ddown = dcc.Dropdown(id='dropdown',
options=[
{'label': '3-Rows', 'value': '3_Rows'},
{'label': '2-Rows', 'value': '2_Rows'},],
value='3Rows')
#placeholder='Select One') #value='3_Rows')#
rowr1 = dbc.Row([dbc.Col(children="This is R1 C1", class_name='r1c1'),
dbc.Col(children="This is R1 C2", class_name='r1c2'),
dbc.Col(children=ddown, class_name='r1c3')], class_name='r1 g-0')
def layout3():
global rowr1
rowr2 = dbc.Row([dbc.Col(children="This is R2 C1", class_name='r2c1'),
dbc.Col(children="This is R2 C2", class_name='r2c2')],class_name='r2 g-0')
rowr3 = dbc.Row([dbc.Col(children="This is R3 C1", class_name='r3c1'),
dbc.Col(children="This is R3 C2", class_name='r3c2')],class_name='r3 g-0')
layout = [rowr1, rowr2, rowr3]
return layout
def layout2():
global rowr1
rowr2 = dbc.Row([dbc.Col(children="This is R2 C1", class_name='r2c1'),
dbc.Col(children="This is R2 C2", class_name='r2c2')],class_name='r2 g-0')
layout = [rowr1, rowr2]
return layout
layout = layout3()
app.layout = dbc.Container(id ='main', children=layout,class_name='container')
@app.callback(
Output('main','children'),
[Input('dropdown','value')], prevent_initial_call=True
)
def update_lay(ddvalue):
if ddvalue == '3_Rows':
layout = layout3()
return layout
elif ddvalue == '2_Rows':
layout=layout2()
return layout
Suppress Werkzeug logging output
import logging
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
if __name__ == "__main__":
app.run(port=8888, use_reloader=False)
I found a workaround by commenting in Dropdown code "value=‘3_Rows’) and replaced with 'placeholder=‘Select One’) and it works !!!.
I haven’t understood why. Am I missing something ??
Though problem got resolved, welcome comments for further understanding.
Cheers.