I have made a simple sidebar according to example in https://dash-bootstrap-components.opensource.faculty.ai/examples/simple-sidebar/
Now I need change that navbar content when user changes pages. I would like to make it dynamic & hierarchical.
This means that I should change the sidebar layout when the page changes. That is quite easy when loading normal page content, but sidebar component does not change from page to page.
Therefore I believe the key question is how to refresh the app.layout once the user clicks a new page, and the script has modified (modify_test) in the link_list below, respectively?
Here is the essential part of the sidebar code (!NOT WORKING!):
link_list = [
dbc.NavLink("Home", href="/", active="exact"),
dbc.NavLink("Page 1", href="/page-1", active="exact"),
dbc.NavLink("Page 2", href="/page-2", active="exact"),
]
def modify_test ():
link_list.append(dbc.NavLink("Page 3", href="/page-3", active="exact"))
sidebar = html.Div(
[
html.H2("Sidebar", className="display-4"),
html.Hr(),
html.P(
"A simple sidebar layout with navigation links", className="lead"
),
dbc.Nav(
link_list,
vertical=True,
pills=True,
),
],
style=SIDEBAR_STYLE,
)
content = html.Div(id="page-content", style=CONTENT_STYLE)
app.layout = html.Div([dcc.Location(id="url"), sidebar, content])
@app.callback(Output("page-content", "children"), [Input("url", "pathname")])
def render_page_content(pathname):
if pathname == "/":
return html.P("This is the content of the home page!")
elif pathname == "/page-1":
modify_test ()
return html.P("This is the content of page 1. Yay!")
elif pathname == "/page-2":
return html.P("Oh cool, this is page 2!")
# 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..."),
]
)