|
| 1 | +""" |
| 2 | +This app creates an animated sidebar using the dbc.Nav component and some local |
| 3 | +CSS. Each menu item has an icon, when the sidebar is collapsed the labels |
| 4 | +disappear and only the icons remain. Visit www.fontawesome.com to find |
| 5 | +alternative icons to suit your needs! |
| 6 | +
|
| 7 | +dcc.Location is used to track the current location, setting the page content |
| 8 | +and the active menu item via callbacks. |
| 9 | +
|
| 10 | +For more details on building multi-page Dash applications, check out the Dash |
| 11 | +documentation: https://dash.plot.ly/urls |
| 12 | +""" |
| 13 | +import dash |
| 14 | +import dash_bootstrap_components as dbc |
| 15 | +import dash_core_components as dcc |
| 16 | +import dash_html_components as html |
| 17 | +from dash.dependencies import Input, Output |
| 18 | + |
| 19 | +FA = "https://use.fontawesome.com/releases/v5.15.1/css/all.css" |
| 20 | +PLOTLY_LOGO = "https://images.plot.ly/logo/new-branding/plotly-logomark.png" |
| 21 | + |
| 22 | +app = dash.Dash(external_stylesheets=[dbc.themes.BOOTSTRAP, FA]) |
| 23 | + |
| 24 | +sidebar = html.Div( |
| 25 | + [ |
| 26 | + html.Div( |
| 27 | + [ |
| 28 | + # width: 3rem ensures the logo is the exact width of the |
| 29 | + # collapsed sidebar (accounting for padding) |
| 30 | + html.Img(src=PLOTLY_LOGO, style={"width": "3rem"}), |
| 31 | + html.H2("Sidebar"), |
| 32 | + ], |
| 33 | + className="sidebar-header", |
| 34 | + ), |
| 35 | + html.Hr(), |
| 36 | + dbc.Nav( |
| 37 | + [ |
| 38 | + dbc.NavLink( |
| 39 | + [html.I(className="fas fa-home mr-2"), html.Span("Home")], |
| 40 | + href="/", |
| 41 | + id="home-link", |
| 42 | + ), |
| 43 | + dbc.NavLink( |
| 44 | + [ |
| 45 | + html.I(className="fas fa-calendar-alt mr-2"), |
| 46 | + html.Span("Calendar"), |
| 47 | + ], |
| 48 | + href="/calendar", |
| 49 | + id="calendar-link", |
| 50 | + ), |
| 51 | + dbc.NavLink( |
| 52 | + [ |
| 53 | + html.I(className="fas fa-envelope-open-text mr-2"), |
| 54 | + html.Span("Messages"), |
| 55 | + ], |
| 56 | + href="/messages", |
| 57 | + id="messages-link", |
| 58 | + ), |
| 59 | + ], |
| 60 | + vertical=True, |
| 61 | + pills=True, |
| 62 | + ), |
| 63 | + ], |
| 64 | + className="sidebar", |
| 65 | +) |
| 66 | + |
| 67 | +content = html.Div(id="page-content", className="content") |
| 68 | + |
| 69 | +app.layout = html.Div([dcc.Location(id="url"), sidebar, content]) |
| 70 | + |
| 71 | + |
| 72 | +# set the content according to the current pathname |
| 73 | +@app.callback(Output("page-content", "children"), [Input("url", "pathname")]) |
| 74 | +def render_page_content(pathname): |
| 75 | + if pathname == "/": |
| 76 | + return html.P("This is the home page!") |
| 77 | + elif pathname == "/calendar": |
| 78 | + return html.P("This is your calendar... not much in the diary...") |
| 79 | + elif pathname == "/messages": |
| 80 | + return html.P("Here are all your messages") |
| 81 | + # If the user tries to reach a different page, return a 404 message |
| 82 | + return dbc.Jumbotron( |
| 83 | + [ |
| 84 | + html.H1("404: Not found", className="text-danger"), |
| 85 | + html.Hr(), |
| 86 | + html.P(f"The pathname {pathname} was not recognised..."), |
| 87 | + ] |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +# sets the active property on the navlink corresponding to the current page |
| 92 | +@app.callback( |
| 93 | + [ |
| 94 | + Output(link_id, "active") |
| 95 | + for link_id in ["home-link", "calendar-link", "messages-link"] |
| 96 | + ], |
| 97 | + [Input("url", "pathname")], |
| 98 | +) |
| 99 | +def toggle_active_links(pathname): |
| 100 | + if pathname == "/": |
| 101 | + return True, False, False |
| 102 | + elif pathname == "/calendar": |
| 103 | + return False, True, False |
| 104 | + elif pathname == "/messages": |
| 105 | + return False, False, True |
| 106 | + return False, False, False |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + app.run_server(debug=True) |
0 commit comments