Sharing data between callbacks

Hello!

I am looking at this tutorial page Part 5. Sharing data between callbacks, namely Example 1 - Storing Data in the Browser with a Hidden Div

https://dash.plotly.com/sharing-data-between-callbacks

I wonder why is the json-ing of dataframes preferred over filtering the df multiple times in respective callbacks?

.to_json and .from_json methods seem to be extremely slow and they work much worse than actually filtering the df multiple times in the respective callbacks.

Why is the approach of storing json-ed df in a hidden div advised in the first place? It is way worse than the multiple df filter.

Is there a way to share data between callbacks which is faster than .to_json, .from_json methods?

Thanks!

The slow performance is not (only) due to the serialization to JSON, in most cases it’s mainly due to the transfer of data between the server and the client. You can avoid it all by caching the data on the server. I made a small library to ease the pain,

but you could also implement the caching manually.

Many thanks, Emil, I will try it out.

Cool. Let me know how it goes :). In short, the requires changes should be something like,

  1. Replace the Dash object by
from dash_extensions.enrich import DashProxy, ServersideOutputTransform

app = DashProxy(transforms=[ServersideOutputTransform()])  # enable use of ServersideOutput objects
  1. Change the Output object that stores the data to a ServersideOutput (also imported from dash_extensions.enrich)

You should then be able to save the data frame directly without serialization to JSON :slight_smile: