Sharing non serializable objects between callbacks

I’ve been reading over the documentation on sharing data: https://dash.plotly.com/sharing-data-between-callbacks

I have a few objects that I use to extract metadata information for things like axis labels. The objects make some small database calls to gather the metadata. Once created they’re static, I don’t want to re-create them in each callback because they do take a few seconds to initialize. The objects aren’t built to be serialized and it’d be a pain to make them serializable.

This seems like a good use case for global variables. I don’t mind if each subprocess initializes its own set of objects because they aren’t large. But I wanted to ask if there is a better way to store setup variables like this.

Most of the options I see don’t appear to work because the objects aren’t serializable.

1 Like

I am in a similar situation, I have also relied on using global variables since asyncua objects are complex and non serializable. As of now this used to be my workflow:
In app.py:

from pages import control
...
control.get_callbacks(app, complex_global_variable)

And in control.py:

def get_callbacks(app, complex_global_variable):
    # Normal callback 
    @app.callback(Output(...), Input(...))
    def function(...):
         complex_global_variable ✅

    # Pattern-matching callback 
    @app.callback(Output(...), Input(dict(...), 'value'))
    def function(...):
         complex_global_variable ❌

The problem is that pattern-matching callbacks don’t seem to have access to the input variables of the parent function. Right now my solution is to store the output value from the callback in a dcc.Store and use that as an input of a normal callback that has access to complex_global_variable:

def get_callbacks(app, complex_global_variable):
    # Normal callback 
    @app.callback(Output(...), Input('store-write_value'))
    def function(data):
         data = json.loads(data)
         complex_global_variable ✅
         # Perform action using complex_global_variable and stored data
         complex_global_variable.method(data['node'], data['value'])
 
    # Pattern-matching callback 
    @app.callback(Output('store-write_value', 'data'), Input(dict(...), 'value'))
    def function(...):
         output = {'node':..., 'value':...}
         return json.dumps(output)

But this does not seem like a clean solution, any ideas would be appreciated

One option would be to use a ServersideOutputTransform. Depending on your infrastructure, you might need to setup a non-default backend though