Hello,
“Example 2 - Computing Aggregations Upfront” on Part 5. Sharing Data Between Callbacks | Dash for Python Documentation | Plotly appeared to be a bit confusing to me:
@app.callback(
Output('intermediate-value', 'children'),
Input('dropdown', 'value'))
def clean_data(value):
# an expensive query step
cleaned_df = your_expensive_clean_or_compute_step(value)
# a few filter steps that compute the data
# as it's needed in the future callbacks
df_1 = cleaned_df[cleaned_df['fruit'] == 'apples']
df_2 = cleaned_df[cleaned_df['fruit'] == 'oranges']
df_3 = cleaned_df[cleaned_df['fruit'] == 'figs']
datasets = {
'df_1': df_1.to_json(orient='split', date_format='iso'),
'df_2': df_2.to_json(orient='split', date_format='iso'),
'df_3': df_3.to_json(orient='split', date_format='iso'),
}
return json.dumps(datasets)
@app.callback(
Output('graph', 'figure'),
Input('intermediate-value', 'children'))
def update_graph_1(jsonified_cleaned_data):
datasets = json.loads(jsonified_cleaned_data)
dff = pd.read_json(datasets['df_1'], orient='split')
figure = create_figure_1(dff)
return figure
@app.callback(
Output('graph', 'figure'),
Input('intermediate-value', 'children'))
def update_graph_2(jsonified_cleaned_data):
datasets = json.loads(jsonified_cleaned_data)
dff = pd.read_json(datasets['df_2'], orient='split')
figure = create_figure_2(dff)
return figure
@app.callback(
Output('graph', 'figure'),
Input('intermediate-value', 'children'))
def update_graph_3(jsonified_cleaned_data):
datasets = json.loads(jsonified_cleaned_data)
dff = pd.read_json(datasets['df_3'], orient='split')
figure = create_figure_3(dff)
return figure
- Why is
Output('graph', 'figure')
used in 3 callbacks? I thought one output can only be used once. Otherwise I get an error. - Isn’t there a html element with the id
'intermediate-value'
necessary? Doesn’t work for me without a hidden element.
In general, the strategy with hidden html elements to trigger a callback from another one seems a bit dirty to me. Is there a possibility to trigger a callback which e.g. spawns a modal when another callback is finished without using a html element in between?
Thank you!