Open dwisianto opened 7 months ago
Board with NavBar
#
#
#
class BoardNavAndBar:
uid_url, uid_page_content = 'url', 'page-content'
uid_url0, uid_url1, uid_url2 = '/', '/page-1', '/page-2'
@classmethod
def layout(cls):
return html.Div(
[
dcc.Location(id=cls.uid_url),
dbc.NavbarSimple(
children=[
dbc.NavLink("Home", href=cls.uid_url0, active="exact"),
dbc.NavLink("Page 1", href=cls.uid_url1, active="exact"),
dbc.NavLink("Page 2", href=cls.uid_url2, active="exact"),
],
brand="Navbar with active links",
color="primary",
dark=True,
),
dbc.Container(id=cls.uid_page_content, className="pt-4"),
]
)
@classmethod
def callback(cls, ap):
@ap.callback(Output(cls.uid_page_content, "children"), [Input(cls.uid_url, "pathname")])
def render_page_content(pathname):
if pathname == cls.uid_url0:
return html.P("This is the content of the home page!")
elif pathname == cls.uid_url1:
return html.P("This is page 1!")
elif pathname == cls.uid_url2:
return html.P("Oh cool, this is page 2!")
# If the user tries to reach a different page, return a 404 message
return cls.page_not_found__layout(path_name=pathname)
return ap
@classmethod
def page_not_found__layout(cls, path_name):
return html.Div([
html.H1("404: Not found", className="text-danger"),
html.Hr(),
html.P(f"The pathname {path_name} was not recognised..."),
],
className="p-3 bg-light rounded-3",)
@classmethod
def demo(cls):
app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP],
suppress_callback_exceptions=True)
app.layout = cls.layout()
app = cls.callback(app)
app.run_server(port=8888)