Passing dash app in flask.g

Hello,

I’m trying to insert a Dash application in a Flask environment. I’ve been using the App Factory pattern of Flask and I am initializing the app like this:

app = Flask(...)
...
with app.app_context():
    ...
    from my_dash_app import init_dash_app
    app = init_dash_app(app)
    return app

Then in the init file of my dash application I’m doing:

from flask import g
...
def init_dash_app(server):
    dash_app = dash.Dash(server=server, ...)
    g.dash_app = dash_app
    init_callbacks()

def init_callbacks():
    import all_my_dash_callbacks

Finally, in all files where the Dash @app.callback decorator is used, I do:

from flask import g
app = g.dash_app
...
@app.callback(...)
def my_callback():
    ...

It works fine and doesn’t change the original Dash app too much, but It feels a bit hacky passing the dash_app reference around through the flask.g object.
Is there a better way to initialize Dash callbacks inside a Flask environment ?

Thanks!

1 Like

Hey,

I am doing the same but a bit different execution. I’m following this:

Daniel

This is the pattern I use for running dash apps on flask. It works well and is scalable. GitHub - bw984/Flask-Dash-App-Factory-Template: Integrate multiple dash applications into Jinja2 templates within a Flask web app.

1 Like