selectbox method in Streamlit

The selectbox() method creates a dropdown menu in your Streamlit app where the user can select one option from a list. It is useful for filtering data, choosing models, selecting parameters, and more.

Syntax

st.selectbox(
    label,
    options,
    index=0,
    format_func=special_internal_function,
    key=None,
    help=None,
    on_change=None,
    args=None,
    kwargs=None,
    *,
    placeholder=None,
    disabled=False,
    label_visibility="visible",
    accept_new_options=False,
    width="stretch"
)    

The function has the following parameters:

Example: Basic Usage

Python

import streamlit as st

# List of options
fruits = ["Apple", "Banana", "Cherry", "Mango"]

# Create dropdown
selected_fruit = st.selectbox("Select your favorite fruit:", fruits)

# Display selected option
st.write("You selected:", selected_fruit)    

Output: A dropdown appears with the given fruits. The user selects one option, and Streamlit displays the selected value.

selectbox method in Streamlit

Example: Setting a Default Option

Python

import streamlit as st

colors = ["Red", "Green", "Blue"]

# Default selection (index=1 → "Green")
selected_color = st.selectbox("Choose a color:", colors, index=1)

st.write("Default selection:", selected_color)