Hello Dash Team!
I’m trying to make a Multi-Page Dash app as a page of a Flask App. And I am getting an error: "raise Exception("`dash.register_page()` must be called after app instantiation")"
This is the structure of my app:
dash_in_flask
│ config.py
│ index.py
│
├───app
│ __init__.py
│
├───my_dashapp
│ │ assets
│ │ __init__.py
│ │
│ ├───pages
| | __index.py
| ├─────home
│ | __init__.py
│ | layout.py
│ | callbacks.py
|
├───main
│ | routes.py
│ | __init__.py
│
├───static
|
├───templates
|
├───Flask_Page1
|
├───Flask_Page2
This is the code in app/__init__.py
, where I register my_dashapp
def create_app(config_class):
app = Flask(__name__)
app.config.from_object(config_class)
register_dataview(app, 'my_dashapp', 'my_dashapp', login_required=False)
return app
def register_dataview(app, title, base_pathname, login_required=False):
meta_viewport={"name":"viewport", "content":"width=device-width, initial-scale=1, shrink-to-fit=no"}
my_dashapp = dash.Dash(
__name__,
use_pages=True,
server=app,
url_base_pathname=f'/',
assets_folder=base_pathname+'/assets/',
external_stylesheets=[dbc.themes.SLATE, FONT_AWESOME],
suppress_callback_exceptions=True,
meta_tags=[meta_viewport]
)
with app.app_context():
my_dashapp.title = title
my_dashapp.layout = dbc.Container(
[dash.page_container],
fluid=True
)
my_dashapp.clientside_callback(
"""
function(n) {
const local_time_str = new Date().toLocaleString();
const time_str = new Date().toTimeString();
const offset = new Date().getTimezoneOffset();
return offset/60
}
""",
Output('time_zone_offset', 'data'),
Input('fake_div', 'children'),
)
This is the code in app/my_dashapp/pages/home/__init__.py
, where I hope to register home as the homepage of my_dashapp
import dash
dash.register_page(__name__, path="/")
from app.DVDash.pages.home import layout
from app.DVDash.pages.home import callbacks
My app works fine when I didn’t make my_dashapp multi-page. However, it reports error after I tried to do so. I’m wondering if it’s possible to make a Multi-Page Dash app as a page of a Flask App? If so, could you please let me know how should I fix this? Thank you in advance!