Hello everybody
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
my ‘add_new_action.py’
import dash
from dash import html, dcc, callback, dash_table, ALL, no_update
dash.register_page(__name__, path='/add_new_action')
layout = dbc.Container(
[..]
)
@callback(Output("...", "..."), Input("...", "...")
def here_i_have_to_access_db(input):
query = db.select([Massnahme]).where(Massnahme.name == action_name)
...