Hi! Im starting with nested callbacks and I have a problem:
In this example:
@app.callback(
Output('minor_cat_dd', 'options'),
Input('major_cat_dd', 'value'))
def update_dd(major_cat_dd):
# Filter options (list of dicts)
return minor_options
@app.callback(
Output('minor_cat_dd', 'value'),
Input('minor_cat_dd', 'options'))
def update_dd(minor_cat_options):
# Pick a default value
return chosen_value
When changing the first dropdown, then what the second callback does is changing to a default value
Another example from dash documentation: Part 3. Basic Callbacks | Dash for Python Documentation | Plotly
# -*- coding: utf-8 -*-
from dash import Dash, dcc, html, Input, Output
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = Dash(__name__, external_stylesheets=external_stylesheets)
all_options = {
'America': ['New York City', 'San Francisco', 'Cincinnati'],
'Canada': [u'Montréal', 'Toronto', 'Ottawa']
}
app.layout = html.Div([
dcc.RadioItems(
list(all_options.keys()),
'America',
id='countries-radio',
),
html.Hr(),
dcc.RadioItems(id='cities-radio'),
html.Hr(),
html.Div(id='display-selected-values')
])
@app.callback(
Output('cities-radio', 'options'),
Input('countries-radio', 'value'))
def set_cities_options(selected_country):
return [{'label': i, 'value': i} for i in all_options[selected_country]]
@app.callback(
Output('cities-radio', 'value'),
Input('cities-radio', 'options'))
def set_cities_value(available_options):
return available_options[0]['value']
@app.callback(
Output('display-selected-values', 'children'),
Input('countries-radio', 'value'),
Input('cities-radio', 'value'))
def set_display_children(selected_country, selected_city):
return u'{} is a city in {}'.format(
selected_city, selected_country,
)
if __name__ == '__main__':
app.run_server(debug=True)
My doubt is, in the second callback, the one that is updating the default value, if you change the value of the second option, instead of just picking one value why the callback is not being triggered?
@app.callback(
Output('minor_cat_dd', 'value'),
Input('minor_cat_dd', 'options'))
def update_dd(minor_cat_options):
# Pick a default value
return chosen_value
I mean, what this callback does is updatint the callback to a default value. If you are picking one value, It shout be triggered and choose the default callbak