I have a multipage app that looks like the following:
-index
-welcome
-summary
-[others]
-test`
In test.py, there is an interactive callback that i’ve taken from here.
When I call test.layout using tabs, test.layout gets loaded and the interactivity in that page works fine:
dcc.Tabs(
id="tabs",
style={"height":'autosize',"verticalAlign":"middle"},
children=[
dcc.Tab(id="welcome_tab", label="Welcome", value="welcome_tab"),
dcc.Tab(id="summary_tab", label="Summary", value="summary_tab"),
dcc.Tab(id="ppl_in_offences_tab", label="Defendents in", value="ppl_in_offences_tab"),
dcc.Tab(id="impositions_tab", label="Sentences Imposed", value="impositions_tab"),
dcc.Tab(id="sentences_served_tab", label="Sentences Served", value="sentences_served_tab"),
dcc.Tab(id="post_cc_tab", label="Post Crown Court", value="post_cc_tab"),
dcc.Tab(id="test_tab", label="test", value="test_tab"),
],
html.Div(id='tabs-content-example_0'),
@app.callback(Output('tabs-content-example_0', 'children'),
[Input('tabs', 'value')])
def render_content(tab):
if tab == 'welcome_tab':
return welcome.layout
elif tab == 'summary_tab':
return summary.layout
elif tab == 'ppl_in_offences_tab':
return ppl_in_offences.layout
elif tab == 'impositions_tab':
return impositions.layout
elif tab == 'sentences_served_tab':
return sentences_served.layout
elif tab == 'post_cc_tab':
return post_cc.layout
elif tab == 'test_tab':
return test.layout
else:
return '404 error'
However rather than tabs I want to use a navbar.
So I make the index looks as follows:
app.layout = html.Div([
html.Div([
dcc.Location(id="url"),
dbc.NavbarSimple(
children=[
dbc.NavLink("Welcome", href="/page-1", id="page-1-link"),
dbc.NavLink("Summary", href="/page-2", id="page-2-link"),
dbc.NavLink("Defendants", href="/page-3", id="page-3-link"),
dbc.NavLink("Sentences Imposed", href="/page-4", id="page-4-link"),
dbc.NavLink("Sentences Served", href="/page-5", id="page-5-link"),
dbc.NavLink("Post-Crown Court", href="/page-6", id="page-6-link"),
dbc.NavLink("test", href="/page-7", id="page-7-link"),
],
color="primary",
dark=True,
),
#dbc.Container(id="page-content", className="pt-4"),
]),
sidebar,
html.Div(id='page-content'),
])
@app.callback(
[Output(f"page-{i}-link", "active") for i in range(1, 8)],
[Input("url", "pathname")],
)
def toggle_active_links(pathname):
if pathname == "/":
# Treat page 1 as the homepage / index
return True, False, False, False, False, False, False
return [pathname == f"/page-{i}" for i in range(1, 8)]
@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
def render_page_content(pathname):
if pathname in ["/", "/page-1"]:
return welcome.layout
elif pathname == "/page-2":
return summary.layout
elif pathname == "/page-3":
return ppl_in_offences.layout
elif pathname == "/page-4":
return impositions.layout
elif pathname == "/page-5":
return sentences_served.layout
elif pathname == "/page-6":
return post_cc.layout
elif pathname == "/page-7":
return test.layout
# If the user tries to reach a different page, return a 404 message
return dbc.Jumbotron(
[
html.H1("404: Not found", className="text-danger"),
html.Hr(),
html.P(f"The pathname {pathname} was not recognised..."),
]
)
When I do this, test.layout loads, but the interactivity doesn’t work at all. It’s the same case with the interactivity callbacks in the other pages. How should I get this working?