st.radio() method in Streamlit

The radio() method creates a radio button group that lets the user select one option from multiple choices. It is similar to a selectbox, but all options are visible at once.

Use it when

Syntax

st.radio(label, options, index=0, format_func=str, key=None,
         help=None, on_change=None, args=None, kwargs=None,
         horizontal=False)    

Parameters

Example: Basic Radio Button

Python

# Importing the Streamlit library
import streamlit as st

# Create a radio button
choice = st.radio("Select your favorite color:", ["Red", "Green", "Blue"])

st.write("You selected:", choice)    

Output:

Streamlit radio method example

Example: Default Selection using index

Python

import streamlit as st

language = st.radio(
    "Choose programming language:",
    ["Python", "Java", "C++"],
    index=0  # Default is "Python"
)

st.write("Your selected language:", language)    

Explanation

Example: Filtering Data with Radio Buttons

Python

import streamlit as st
import pandas as pd

# Sample DataFrame
df = pd.DataFrame({
    "City": ["Delhi", "Mumbai", "Kolkata", "Delhi", "Kolkata"],
    "Sales": [100, 200, 150, 120, 170]
})

# Choose filter option
selected_city = st.radio("Select city to view sales:", df["City"].unique())

# Filter based on selection
filtered_df = df[df["City"] == selected_city]

st.dataframe(filtered_df)    

Use Case: