I have a multi page app structured like this:
|-- src
| |-- components
| | |-- __init__.py
| | |-- navbar.py
| |-- pages
| | |-- __init__.py
| | |-- home.py
| |-- app.py
In navbar.py
I’m trying to create the link to every page dynamically like so (as shown in the example here https://github.com/AnnMarieW/dash-multi-page-app-demos/blob/main/multi_page_basics_pathname_prefix/app.py)
navbar = dbc.Navbar(
dbc.Container(
[
dbc.NavbarToggler(id='navbar-toggler', n_clicks=0),
dbc.Collapse(
dbc.Nav(
[
dbc.NavItem(
dbc.NavLink(page['name'], href=page["relative_path"])
) for page in dash.page_registry.values()
],
),
id='navbar-collapse',
navbar=True
),
]
),
)
The navbar
is included in the main app.py
quite simply like
def serve_layout():
return html.Div(
[
navbar,
dbc.Container(
dash.page_container,
class_name='my-2'
),
]
)
With this code the navbar does not show, probably because it cannot access dash.page_registry
. I’ve tried encapsulating it into a function that returns navbar
but this causes the layout to not load at all.
As workaround at the moment I’m creating the links manually, i.e.
navbar = dbc.Navbar(
dbc.Container(
[
dbc.NavbarToggler(id='navbar-toggler', n_clicks=0),
dbc.Collapse(
dbc.Nav(
[
dbc.NavItem(
dbc.NavLink(
'Home',
href=URL_BASE_PATHNAME
)
),
dbc.NavItem(
dbc.NavLink(
'Ensemble',
href=f'{URL_BASE_PATHNAME}ensemble/'
)
),
],
),
id='navbar-collapse',
navbar=True
),
]
),
)
Any idea how I could fix this?