I try to build a multi-page -app with the new Dash 2.5 and included SQL-Alchemy. The setup of the app is in my init.py file.
When I include a new page I did so with dash.register_page(name, path=‘/add_new_action’) in the according add_new_action.py file. But I don’t succeed to access db, session or engine (from init.py) from this file. When I normally import db etc. then I run into a circular import. In the dash-help (Multi-Page Apps and URL Support | Dash for Python Documentation | Plotly) I read “In cases where you need access to the app in one of your page layouts, you can access it with dash.get_app”. But what about db, session or engine?
How I have to correctly organize my app to work with db on all pages of my multi-page-app?
Thanks for your help
my ‘init.py’
from dash import Dash
from eva_app.navigation import layout
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from eva_app.config import DB_CONNECTION_STRING
app = Dash(__name__, use_pages=True,
prevent_initial_callbacks=True)
app.server.config["SQLALCHEMY_DATABASE_URI"] = DB_CONNECTION_STRING
app.server.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config.suppress_callback_exceptions = True
db = SQLAlchemy(app.server)
engine = create_engine(app.server.config["SQLALCHEMY_DATABASE_URI"],
connect_args={},
echo=False)
Session = sessionmaker(bind=engine)
session = Session()
db.create_all()
app.layout = layout
Ok, I think there is one way to use pages with flask-sqlalchemy.
You need to create new module e.g. server.py.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
server = Flask(__name__)
# here goes other other setting of sqlalchemy
# ...
db = SQLAlchemy(server)
And then in app.py you need to:
from dash import Dash
from server import server
app = Dash(__name__, use_pages=True, server=server)
I think it should work the same as using direct app.server instance. If there are any difference let me know.
It is me again. It is also possible to use app.init_app() method to delay pages initialization. If you want to import anything from app.py you need start your app from other module e.g. index.py.
You need to create app like: app = Dash(__name__, use_pages=True, server=False)
And then in index.py
from app import app, server
app.init_app(server)
I was also trying to use app.enable_pages method. I think it should not be public method or it should be modified, since it is not possible to use it directly and its name is misleading. Nevertheless it is possible to use it in this way: