How to compare the outputs from successive callbacks?

This question is not about chained callbacks, but about somehow accessing the output from a previous execution of the same callback.

I am building a dashboard for our organization that fetches live data from a web API regularly using a dcc.Interval. Some of the metrics relevant to display are the mean, stdev, min, and max values for a handful of time series. These metrics will go into a go.Indicator, but I would like to also display the differences compared to the previously fetched data. In this way the users get an idea of how the data is changing in each update. I could simply double the amounts of API calls and get the previous data, but the API is a bit slow and it takes about 10 seconds to get the data once when done in an async way. We want the dashboard to update as often as possible, and I feel like there should be a way to reuse the already fetched data.

I have attempted several solutions:

  1. Write the data to file and load this later to compute the deltas. This does indeed work, but fails when the app is deployed to a kubernetes cluster with replicated pods. Each pod tries to access the same file, which messes up the app. I am sure this can be solved on the architecture side, but I am not so experiences with kubernetes deployment.

  2. I tried to chain two dcc.Store components together, where the first fetches the data and feeds it to the Input of another callback which feeds the data into the second dcc.Store. But this, perhaps obviously, just leaves me with two separate dcc.Stores with the same set of data, since the callbacks don’t follow the “normal” procedural execution of the code. I placed the second callback (the one which stores the data in the second dcc.Store after the I displayed the figures with the data from the first dcc.Store, hoping that this would force the second dcc.Store to store the “previous” data at the next iteration of the callbacks.

Is there a simple way to compare the outputs from successive callbacks?

All help much appreciated!

Hi,

What about using the DCC store as a State when you also use it as an output in the same step.

If the state / existing is null then it doesn’t exist. Anything else you could use previous data to make calculations from that data already in the store.

Reg,j.

Thanks for your reply! I am not sure I follow your train of thoughts. Could you write some dummy code to illustrate what you mean?

If I understood you correctly, it looks like you suggest storing a constant dataframe as a dash.dependencies.State in the callback for fetching data, so that i can always compare the current data to the constant one. But I may have misunderstood you. If so I apologize :slight_smile:

I want to compare the output of the current callback to the output of the previous callback - always “one callback earlier in time”.