Hi there,
am new to dash and pages and think there is something fundamental I have not understood yet.
I am trying to build an app struction with several tabs, whereas I can populate the tabs with content contained in seperate .py files. I want my app.py to be as sleek as possible. My code produces the app and the tabs, but each tab displays the error “Page ‘page1’ not found” respectively because the entry in the registry is not found.
Could you help me figure this out?
Below is my code:
app.py
import dash
from dash import html, dcc, callback, Input, Output
app = dash.Dash(__name__, use_pages=True)
app.layout = html.Div([
html.H1('Dashboard'),
dcc.Tabs(id="tabs", value='page1', children=[
dcc.Tab(label='Tab 1', value='page1'),
dcc.Tab(label='Tab 2', value='page2'),
dcc.Tab(label='Tab 3', value='page3'),
]),
dash.page_container
])
@callback(
Output(dash.page_container, 'children'),
Input('tabs', 'value')
)
def render_content(tab):
if tab in dash.page_registry:
return dash.page_registry[tab]['layout']
else:
return html.Div(f"Page '{tab}' not found")
def render_content(tab):
return dash.page_registry[tab]['layout']
if __name__ == '__main__':
app.run_server(debug=True)
page1.py (under folder ‘pages/’)
import dash
from dash import html, dcc
dash.register_page( __name__ , path='/', name='Page 1')
layout = html.Div([
html.H1('Page 1'),
html.Div('This is the content of page 1'),
dcc.Input(id='input-box'),
html.Div(id='output-text')
])
page2.py (under folder ‘pages/’)
import dash
from dash import html, dcc
dash.register_page( __name__, name='Page 2')
layout = html.Div([
html.H1('Page 2'),
html.Div('This is the content of page 2'),
dcc.Input(id='input-box'),
html.Div(id='output-text')
])
``