The Easiest Way to Add Navigation to Your Streamlit App
The streamlit-option-menu package adds a navigation menu to a Streamlit app, in the sidebar or as a horizontal bar across the top, with a Bootstrap icon on each item. One call returns the selected option, so switching pages stays a simple if statement.
Install the package
streamlit-option-menu is a separate package, so install it first:
pip install streamlit-option-menu
Then import option_menu next to Streamlit:
import streamlit as st
from streamlit_option_menu import option_menu
A menu in the sidebar
option_menu() needs two arguments: a menu title and the list of options. It returns the option the user clicked, here stored in selected. Inside a with st.sidebar block, the menu appears in the sidebar.
The if statements below show the selected option as the page title. In a real app, each branch would draw its own page.
import streamlit as st
from streamlit_option_menu import option_menu
# 1. as sidebar menu
with st.sidebar:
selected = option_menu(
menu_title="Main Menu", # required
options=["Home", "Projects", "Contact"], # required
)
if selected == "Home":
st.title(f"You have selected {selected}")
if selected == "Projects":
st.title(f"You have selected {selected}")
if selected == "Contact":
st.title(f"You have selected {selected}")

Optional arguments change the look. icons sets one Bootstrap icon per option, by its name on the Bootstrap Icons site; I chose house, book and envelope. menu_icon sets the icon next to the title, and menu_title=None hides the title completely. default_index picks the option that is selected when the app opens, counting from 0, so 0 is Home.
# 1. as sidebar menu
with st.sidebar:
selected = option_menu(
menu_title=None, # required
options=["Home", "Projects", "Contact"], # required
icons=["house", "book", "envelope"], # optional
menu_icon="cast", # optional
default_index=0, # optional
)

With more than a few pages, the if statements get long. Put each page in its own function and map the options to them in a dictionary; the last line then calls the function of the selected page:
def home():
st.title("Home")
def projects():
st.title("Projects")
def contact():
st.title("Contact")
pages = {"Home": home, "Projects": projects, "Contact": contact}
pages[selected]()
A horizontal navigation bar
For a bar across the top of the page, call option_menu() outside the sidebar and add orientation=”horizontal”. The rest of the arguments stay the same, and a menu title is usually not needed here.
# 2. horizontal menu
selected = option_menu(
menu_title=None, # required
options=["Home", "Projects", "Contact"], # required
icons=["house", "book", "envelope"], # optional
menu_icon="cast", # optional
default_index=0, # optional
orientation="horizontal",
)
if selected == "Home":
st.title(f"You have selected {selected}")
if selected == "Projects":
st.title(f"You have selected {selected}")
if selected == "Contact":
st.title(f"You have selected {selected}")

The styles argument changes the colors, sizes and spacing of every part of the menu: the container, the icons, the links and the selected link. This example comes from the package documentation:
# 2. horizontal menu
selected = option_menu(
menu_title=None, # required
options=["Home", "Projects", "Contact"], # required
icons=["house", "book", "envelope"], # optional
menu_icon="cast", # optional
default_index=0, # optional
orientation="horizontal",
styles={
"container": {"padding": "0!important", "background-color": "#fafafa"},
"icon": {"color": "orange", "font-size": "25px"},
"nav-link": {
"font-size": "25px",
"text-align": "left",
"margin": "0px",
"--hover-color": "#eee",
},
"nav-link-selected": {"background-color": "green"},
},
)

All examples run with streamlit-option-menu 0.4.0 and Streamlit 1.67. I use this horizontal menu as the switch between the entry form and the chart in my income and expense tracker.
Native navigation
Streamlit now has navigation built in. st.navigation with st.Page builds the menu from your pages, and position=”top” puts it in a bar across the top. streamlit-option-menu still works and gives you more control over icons and colors.
The native approach fits apps where every page is its own file. I use it in a multi-page app with a contact form.
