Streamlit Option Menu
streamlit-option-menu is a simple Streamlit component that allows users to select a single item from a list of options in a menu. It provides a static list of options with configurable icons and extensive CSS styling capabilities, offering an alternative to `st.selectbox()` for navigation. The current stable version is 0.4.0, with regular updates and community engagement.
Warnings
- gotcha The `option_menu` UI may incorrectly refresh to its default state after `st.experimental_set_query_params` is called, even if the underlying selected value remains correct. The UI might not visually reflect the intended selection.
- gotcha When deploying Streamlit applications to platforms like Streamlit Cloud, having both `Pipfile` (and `Pipfile.lock`) and `requirements.txt` in your repository can lead to dependency resolution errors where `streamlit-option-menu` is not found. Streamlit prioritizes which dependency file to use.
- gotcha Like most Streamlit components, `streamlit-option-menu` triggers a full app rerun when its value changes. If the component is displayed conditionally or hidden, its value might reset on subsequent reruns if not properly managed.
- gotcha Custom CSS applied via the `styles` parameter might not always behave as expected or interact with the main Streamlit app's CSS. This is because custom components are often rendered within an iframe, isolating their styling from the parent document.
- gotcha The `manual_select` parameter is designed for one-time programmatic selection and behaves like a button. If `manual_select` is constantly set to an index, it can lead to unexpected behavior or prevent user interaction, as the menu will continuously try to re-select that option.
Install
-
pip install streamlit-option-menu
Imports
- option_menu
from streamlit_option_menu import option_menu
Quickstart
import streamlit as st
from streamlit_option_menu import option_menu
st.set_page_config(layout="wide")
with st.sidebar:
selected = option_menu(
menu_title="Main Menu", # required
options=["Home", "Projects", "Contact"], # required
icons=["house", "code-slash", "envelope"], # optional
menu_icon="cast", # optional
default_index=0, # optional
orientation="vertical"
)
if selected == "Home":
st.title(f"You selected: {selected}")
st.write("Welcome to the Home page!")
elif selected == "Projects":
st.title(f"You selected: {selected}")
st.write("Here are my projects.")
elif selected == "Contact":
st.title(f"You selected: {selected}")
st.write("Contact me here.")