I have a multi-tab / page application and I’d like to pass the data (a dictionary) returned by a function call in one callback to another callback. Here’s some code:
import dash
from dash import dcc
import dash_bootstrap_components as dbc
#app layout
layout = html.Div([
dcc.input(id="comp"),
dcc.Store(id="id1")
html.Div(id='dummy-div')
])
# callbacks
app.callback1([
output('comp','value'),
output('dummy-div','value'),
],
[
input('in1','value'),
input('in2','value')
],
):
def update(in1, in2):
# function call
res = func(in1, in2)
# for reproducibility
res = {'name':'xyz', 'addr':'123'}
return (in2, res)
app.callback(output('id1', 'data'),
[
input('comp','value'),
input('dummy-div','value)
],
):
def store(store, dummy):
# save to store component
# for reproducibility
res = {'name':'xyz', 'addr':'123'}
return {'res': res,
'comp': 12
}
I get an dash exception when I run the above code:
raise exceptions.InvalidCallbackReturnValue(
dash.exceptions.InvalidCallbackReturnValue: The callback for `[<Output `dummy-div.value`>]`
returned a value having type `list`
which is not JSON serializable.
The value in question is either the only value returned,
or is in the top level of the returned list,
and has string representation
`[{'Name':'xyz','Address':'123'........}]`
In general, Dash properties can only be
dash components, strings, dictionaries, numbers, None,
or lists of those.
Basically, I’d like to save res
which is dictionary returned by the function invoked to dcc.Store
component. I’d need to have two separate callbacks because of the way app is structured. How do I pass res
to the callback that saves to dcc.Store
component?