Black Lives Matter. Please consider donating to Black Girls Code today.

Create an instance outside app.callback and call it inside it using getattr(self,'str')

Is it possible to create an instance in a dash application code and call it inside the @app.callback?

I could call it inside, of course, but the way my class is made makes me need this specific feature.

To be more specific:

Assuming a class called Foo.

it would look like:

import dash
import dash_core_components as dcc
import dash_html_components as html
import Foo

f = Foo()

x = f.something()

app = dash.Dash()

app.layout = html.Div([…])

@app.callback( dash.dependencies.Output(id,property),
def callback(value):

data =getattr(f,‘specific_data’)

…any other lines of code…

if name == ‘main’:

This makes complete sense, and is a sensible way to share read-only data between callbacks. You just have to be careful not to mutate the value of this object, otherwise users will have inconsistent states.

1 Like

Hadn’t tested the way I wrote it, but it is actually working this way. Sorry for the request.