Want to add a navigation sidebar to your multi-page Plotly Dash app with custom collapsible menus? Whilst Streamlit allows you to add a sidebar with a line of code, Dash requires a little more effort to get it right.
Plotly Dash is fast becoming my new favourite tool for turning ideas into functional applications. Even though I still enjoy using Streamlit, I find that using Plotly Dash gives me much more flexibility when it comes to building multi-page apps and setting up custom layouts. However, that extra functionality often comes at the cost of simplicity.
Lately, I have been developing multi-page Dash apps as part of my ongoing petrophysics and data science research projects. As a result I needed several custom layouts that were beyond the capability of Streamlit.
One of these features was an expanding menu bar, something that Streamlit comes with out of the box, is the ability to add a sidebar to your app using st.sidebar(). This functionality is not as straightforward to create and apply with Dash and requires some HTML and CSS knowledge to get working.
Within this article, we will go over how to add a simple sidebar to a multi-page Dash app, that also allows for expanding and contracting sub-menus.
Setting Up the Basic Dash App
Before we create our sidebar, we first have to create a simple Plotly Dash app. The code below will create two columns within our app, one reserved for our sidebar, and the the other for our main content.
To make styling simpler, we will use Dash Bootstrap Components. This library contains a bunch of HTML & CSS components that immediately help style your app without having to write them from scratch.
import dash
from dash import Dash, dcc, html
import dash_bootstrap_components as dbc
app = Dash(external_stylesheets=[dbc.themes.BOOTSTRAP])
app.layout = dbc.Container([
dbc.Row([
dbc.Col(children=[
html.Div(sidebar),
], width=2),
dbc.Col(children=[
html.H2("Main Content Area"),
dcc.Location(id="url"),
html.Div(id="page-content")
], width=10)
])
], fluid=True)
if __name__ == '__main__':
app.run(debug=True)We can then run this app using our terminal by using the followingc command
python app.pyOnce this line has been run, we need to navigate to 127.0.0.1:8050 in our web browser.
When we view the page, we will see our app with a big label of ‘Main Content Area’. in the centre.
Adding the Sidebar to the Dash App
To add the sidebar, we can use the simple code below.
First, we create a div, and add in our navigation menu items inside of dbc.Nav . This will create a very simple navigation bar on the left of the app with 3 navigation entities: Home, Upload and Explore
sidebar = html.Div(
[
html.H4("My App", className="p-3"),
dbc.Nav(
[
dbc.NavLink("Home", href="/", active="exact"),
dbc.NavLink("Upload", href="/upload", active="exact"),
dbc.NavLink("Explore", href="/explore", active="exact"),
],
vertical=True,
pills=True,
),
],
style={"backgroundColor": "#f8f9fa", "height": "100vh"},
)When we return to our Dash app and refresh it, we will now see a menu has appeared on the left hand side of our app.

Linking a Dash Sidebar Menu to Other Pages
As our app is not linked to other pages yet, clicking on the links will not work, but we won’t be exploring the full multi-page setup in this article.
However, to hook this menu items up is a relatively easy process thanks to Dash’s multi-page ability. I highly recommend checking out “Charming Data’s Multipage App series” which covers the whole multi-page setup process:
How to Make a Python Multi Page Application with Plotly Dash
Adding a Collapsible Menu Item to a Plotly Dash Sidebar
Sometimes when building Dash apps, we may want to group together similar items within a submenu rather than having a long list of page links in our sidebar.
We can do this by creating a collapsible menu.
To add a collapsible menu, we need to use two Dash Bootstrap Components:
dbc.Button— which will provide a button to hide our menu behind, when it is toggled the sub menu will appeardbc.Collapse— this is where our content that we show and hide is placed
In the code below, you will see I have added a dbc.Nav component with two dbc.NavLink components. These will point to a Scatter Plots and Histograms page (these can be changed to whatever you need for your app).
# Button to allow menu to expand/contract
dbc.Button(
"Data Visualisation",
id="collapse-button",
className="mb-3",
color="primary",
),
# Collapsible menu section for data visualisation
dbc.Collapse(
dbc.Nav([
dbc.NavLink("Scatter Plots", href="/scatter", active="exact", className="ms-3"),
dbc.NavLink("Histograms", href="/histo", active="exact", className="ms-3"),
],
vertical=True),
id="collapse-menu-1",
is_open=False
)We also need to set how we want the dbc.Collapse to behave.
As we are using a vertical menu, we first need to set vertical=True and when we land on the page we want to have the menu closed by default, so we need to set is_open=False .
Finally, to avoid any issues with other callbacks we need to make sure that our button and collapse elements have unique id’s.
These id’s can then be used as part of a callback that toggles the menu to open or close. Plotly Dash callbacks can be simple or as complex as you need.
In order for the menu to expand and collapse, we need to setup a new callback like so:
@app.callback(
Output("collapse-menu-1", "is_open"),
Input("collapse-button", "n_clicks"),
State("collapse-menu-1", "is_open"),
)
def toggle_collapse(n, is_open):
if n:
return not is_open
return is_openWhen we apply a callback, we can define what our output is, inputs are and a state.
Output("collapse-menu-1", "is_open")tells Dash that we want to update theis_openproperty of the menu (i.e. whether it’s expanded or collapsed).Input("collapse-button", "n_clicks")listens out for clicks on a button that are present within our menu.State("collapse-menu-1", "is_open")grabs the current state of the menu, in other words, whether it is currently open or not. And it will do so without triggering the callback on its own.
The function: toggle_collapse is relatively simple. It will detect whether the button has been clicked, if it has then it flips the is_open value and will open the menu if it is closed, and vice-versa.
If we refresh our app, we can now see the menu has a new blue Data Visualisation button.
When we click on that button, we reveal the sub-menu items.
Summary
Adding sidebar menus to a Plotly Dash multi-page app doesn’t have to be complicated. Whilst Dash lacks a built-in sidebar() function like Streamlit, it gives you full control over the layout and behaviour of your app through components and callbacks.
Creating a sidebar in Plotly Dash requires a few lines of code, which can allow us to create a more customised menu, which not only enhances our app visually, but it also allows us to make navigation for ourselves or the end user much smoother.





