So, your error was due to not having return
values in both callbacks when the if
condition was not satisfied. Also, you would have run into another problem as your callbacks would have an infinite loop. Not sure if you intended this to be the case, but based on your dropdowns, I would think you would want hte country dropdown to be populated based on what region was selected.
The following code, slightly tweaking yours (and since you didn’t include a self-supporting example), works.
I removed one of your callbacks and combined them into one. If the user selects the region dropdown, you can return a new dropdown for the country based on this selection.
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output, State
from dash.dash import no_update
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = dash.Dash(__name__, external_stylesheets=external_stylesheets)
server = app.server
app.config.suppress_callback_exceptions = True
app.css.config.serve_locally = False
list1 = ['Country1', 'Country2', 'Country3']
app.layout = html.Div(
children=[
html.Div(
children=[
dcc.Dropdown(
id='region_choose',
options=[
{'label': 'all', 'value': 'all'},
{'label': 'South East Asia', 'value': 'South East Asia'},
{'label': 'AUS & NZL', 'value': 'AUS & NZL'},
{'label': 'CHX & HKG', 'value': 'CHX & HKG'}
],
placeholder='Select a region',
style=dict(
width='800px'
)
),
],
id='region_dropdown'
),
html.Div(
children=[
dcc.Dropdown(
id='country_choose',
options=[
{'label': i, 'value': i} for i in list1
],
placeholder='Select a country',
style=dict(
width='800px'
)
),
],
id='country_dropdown'
)
]
)
def clicked(ctx):
if not ctx.triggered or not ctx.triggered[0]['value']:
return None
else:
return ctx.triggered[0]['prop_id'].split('.')[0]
@app.callback(
output=[Output('country_dropdown', 'children'),
Output('region_dropdown', 'children')],
inputs=[Input('region_choose', 'value'),
Input('country_choose', 'value')])
def link_dropdown(region_value, country_value):
# Define dropdowns
country_dropdown = dcc.Dropdown(
id='country_choose',
options=[
{'label': i, 'value': i} for i in list1
],
placeholder='Select a country',
style=dict(
width='800px'
)
)
region_dropdown = dcc.Dropdown(
id='region_choose',
options=[
{'label': 'all', 'value': 'all'},
{'label': 'South East Asia', 'value': 'South East Asia'},
{'label': 'AUS & NZL', 'value': 'AUS & NZL'},
{'label': 'CHX & HKG', 'value': 'CHX & HKG'}
],
placeholder='Select a region',
style=dict(
width='800px'
)
)
user_clicked = clicked(dash.callback_context)
print(u'''clicked = {}'''.format(user_clicked))
if user_clicked == 'region_choose' and region_value:
# return a country dropdown with countries based on region selected
return country_dropdown, no_update
elif user_clicked == 'country_choose' and country_value:
# do no update either dropdowns (you can if you want, but for getting this work I didn't want to update them)
return no_update, no_update
else:
# Ensure return values are populated if other conditions aren't met
return no_update, no_update
if __name__ == '__main__':
app.run_server(debug=True)