I am trying to use Dash multi-page feature for my project. But it seems like it will load all pages’ layouts during initialization. I found a way to prevent updating as following code.
Is there any better way to do this? Although it can work, it has other callbacks and components in page_2, it will throw errors for page_2 when accessing page_1.
dash_app.py
import dash
from dash import Dash, html, dcc
app = Dash(__name__, use_pages=True)
app.layout = html.Div([
dcc.Location(id='url', refresh='callback-nav' ),
html.Div(id='page-content'),
dash.page_container
])
if __name__ == '__main__':
app.run(debug=True)
pages/page_1.py
import dash
from dash import html, callback
from dash.dependencies import Output, Input
from dash.exceptions import PreventUpdate
dash.register_page(__name__)
def layout():
print('page_1 init')
return
def page_1_layout():
print('page_1 layout init')
return html.Div(children=[
html.H1(children='page_1'),
html.Div(children='''
This is page_1.
'''),
])
@callback(
Output('page-content', 'children', allow_duplicate=True),
Input('url', 'pathname'),
prevent_initial_call='initial_duplicate'
)
def update_page(path):
if path == '/page_1':
return page_1_layout()
else:
raise PreventUpdate
pages/page_2.py
import dash
from dash import html, callback
from dash.dependencies import Output, Input
from dash.exceptions import PreventUpdate
dash.register_page(__name__)
def layout():
print('page_2 init')
return
def page_2_layout():
print('page_2 layout init')
return html.Div(children=[
html.H1(children='page_2'),
html.Div(children='''
This is page_2.
'''),
])
@callback(
Output('page-content', 'children'),
Input('url', 'pathname')
)
def update_page(path):
if path == '/page_2':
return page_2_layout()
else:
raise PreventUpdate