Cannot store data in callbacks

Hi all, I tried to assign something to a dict in a callback(update_units_graph), it succeeds and I can print it. But there is nothing in this dict when I try to print it in another function(ping), they are in the same file. Is there anything special in callbacks?

https://dash.plot.ly/sharing-data-between-callbacks

1 Like

Thanks for your sharing, it helps a lot. But I encountered some new problems.
What I’m gonna achieve is that user can download all the graphs in a pdf, and different users may make different changes to those graphs. So you can notice that the second snapshot is a downloading function that uses @server.route and convert a dictionary of graphs to a pdf.
I tried the following solutions.

  • Use Hidden Div
    I have several callbacks(like the first snapshot) for updating graphs, so all the changes should be saved into one hidden div, but actually callback doesn’t accept same Inputand Output. Even if I can store them in one hidden div, how can I access it in the @server.routefunction.
  • User-Based Session Data on the Server
    I don’t quite understand how this mechanism works, then just imitate some code and find it still rely on global variable.
obr_figures = dict()


def save_fig(session_id, graphs, fig, name):
    @cache.memoize()
    def save(session_id):
        graphs[name] = fig

    return save_fig(session_id)

@app.callback(
    Output('units-graph', 'figure'),
    [Input('dateframe', 'start_date'),
     Input('dateframe', 'end_date'),
     Input('marketplace', 'value'),
     Input('session_id', 'children')])
def update_units_graph(start_date, end_date, marketplace, session_id):
    fig = data_parsers['units'].create_figure('metric_units', start_date, end_date,
                                              marketplace, 'Ordered Units')
    save_fig(session_id, obr_figures, fig, 'units')
    return fig