I have an app I’ve been working on for a while. Prior to the release of Pages, it was structured as a multi-page app using the ‘old way’ using dcc.Tabs. More specifically, ‘app.py’ creates app instance and imports all of the other pages (e.g. hello.py, world.py, etc), all of which had a ‘layout’ variable. I then used a callback to determine which layout to display. E.g. (in app.py):
@app.callback(Output('tabs-content', 'children'),
[Input('tabs', 'value')],
)
def render_tab(tab):
if tab == 'tab-1':
return hello.layout
elif tab == 'tab-2':
return world.layout
The app.py layout is composed of the navigation and a div for the content:
app.layout = html.Div(
[
html.Div(
[
dcc.Tabs(
id='tabs',
value='tab-1',
children=[
dcc.Tab(label='Hello', value='tab-1'),
dcc.Tab(label='World', value='tab-2')
],
),
],
html.Div(id='tabs-content'),
])
This all worked fine. However, I wanted to refactor the app to use the new Pages functionality.
So I removed the individual page imports and added added ‘use_pages=True’
to app.py. I also added ‘dash.register_page(name)’, changed @app.callback to @callback, and deleted the “if name == ‘main’: app.run_server(debug=True)” line in each individual page.
However, I wasn’t quite sure how to integrate the dcc.Tabs functionality into Pages. For example, where does dash.page_container go? And how should I modify my tab callback?
Right now my modified layout looks like this:
app.layout = html.Div(
[
html.Div(
[
dcc.Tabs(
id='tabs',
value='tab-1',
children=[
dcc.Tab(label='Hello', value='tab-1'),
dcc.Tab(label='World', value='tab-2')
],
),
],
html.Div(id='tabs-content'),
dash.page_container,
])
and my modified Tab callback looks like this:
@app.callback(Output('tabs-content', 'children'),
[Input('tabs', 'value')],
)
def render_tab(tab):
if tab == 'tab-1':
return dcc.Location(pathname=dash.page_registry['pages.hello']['path'], id="registry-hello")
elif tab == 'tab-2':
return dcc.Location(pathname=dash.page_registry['pages.world']['path'], id="registry-world")
But this does not work. On load, everything seems fine. the Navigation loads and the ‘hello’ page (tab 1) displays just fine. However, when I click on Tab 2 (world.py), it appears to load for a split second, but then the entire app updates and everything resets back to tab 1.
I tried moving dash.page_container inside the ‘tab-content’ div, but that just resulted in no tab displaying. I am convinced (I hope) that this is an easy fix, but I’ve been looking for a couple of hours now and I cannot seem to figure it out.
Thanks for any help!