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 is similar in function to st.selectbox(), except that:
It is built on streamlit-component-template-vue, styled with Bootstrap and with icons from bootstrap-icons
pip install streamlit-option-menu
The option_menu
function accepts the following parameters:
This option was added to allow the user to manually move to a specific option in the menu. This could be useful when the user wants to move to another option automatically after finishing with one option (for example, if settings are approved, then move back to the main option).
To use this option, you need to pass the index of the desired option as manual_select
. Notice: This option behaves like a button. This means that you should only pass manual_select
once when you want to select the option, and not keep it as a constant value in your menu creation call (see example below).
import streamlit as st
from streamlit_option_menu import option_menu
# 1. as sidebar menu
with st.sidebar:
selected = option_menu("Main Menu", ["Home", 'Settings'],
icons=['house', 'gear'], menu_icon="cast", default_index=1)
selected
# 2. horizontal menu
selected2 = option_menu(None, ["Home", "Upload", "Tasks", 'Settings'],
icons=['house', 'cloud-upload', "list-task", 'gear'],
menu_icon="cast", default_index=0, orientation="horizontal")
selected2
# 3. CSS style definitions
selected3 = option_menu(None, ["Home", "Upload", "Tasks", 'Settings'],
icons=['house', 'cloud-upload', "list-task", 'gear'],
menu_icon="cast", default_index=0, 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"},
}
)
# 4. Manual item selection
if st.session_state.get('switch_button', False):
st.session_state['menu_option'] = (st.session_state.get('menu_option', 0) + 1) % 4
manual_select = st.session_state['menu_option']
else:
manual_select = None
selected4 = option_menu(None, ["Home", "Upload", "Tasks", 'Settings'],
icons=['house', 'cloud-upload', "list-task", 'gear'],
orientation="horizontal", manual_select=manual_select, key='menu_4')
st.button(f"Move to Next {st.session_state.get('menu_option', 1)}", key='switch_button')
selected4
# 5. Add on_change callback
def on_change(key):
selection = st.session_state[key]
st.write(f"Selection changed to {selection}")
selected5 = option_menu(None, ["Home", "Upload", "Tasks", 'Settings'],
icons=['house', 'cloud-upload', "list-task", 'gear'],
on_change=on_change, key='menu_5', orientation="horizontal")
selected5