I have done something similar to populate my layout component after page reload.
Thanks to the first callback, the state of the components are store in a dcc.Store component.
The second callback is to populate the layout components when their state changes or when you access the tab ( the layout is in a dcc.Tabs)
dash_layout_components = {
'time_slider_app2': 'value',
'backtest_choice_app2': 'values',
'asset_selection_app2': 'value',
'graph_selection_app2': 'values'
}
stored_layout_value_name = [key[:key.rfind('a')] + value for key, value in
dash_layout_components.items()]
set_back_and_display_graph_input = {
'store_layout_data': 'modified_timestamp',
'tabs': 'value'
}
@app.callback(
Output('store_layout_data', 'data'),
[Input(key, value) for key, value in dash_layout_components.items()])
def store_layout(time_slider_value, backtest_choice_values, assets_selection_values, graph_selection_values):
data_json = {
'time_slider_value': time_slider_value,
'backtest_choice_values': backtest_choice_values,
'asset_selection_value': assets_selection_values,
'graph_selection_values': graph_selection_values
}
return data_json
for component_id, component_property in dash_layout_components.items():
@app.callback(
Output(component_id, component_property),
[Input(key, value) for key, value in set_back_and_display_graph_input.items()],
[State('store_layout_data', 'data'),
State(component_id, 'id')]
)
def set_back_component(bouton_ts, tabs_value, layout_state_data, component): # dynamiser l'arrivée des paramètres. piste, en créer une liste entre le for et le callback
if tabs_value != '/app2':
raise PreventUpdate
if layout_state_data is None:
return []
else:
store_layout_component_name = stored_layout_value_name[list(dash_layout_components.keys()).index(component)]
return layout_state_data[store_layout_component_name]
Note that you won't have access to the iterated values (component_id and component_property) inside the function ( set_back_component(...) )