Streamlit Checkbox

st.checkbox display a checkbox widget.
st.checkbox() widget in Streamlit is used to get boolean input from users.
we can check True(checked) or False(unchecked).
 
Need a Streamlit developer?: Click here

Syntax

st.checkbox(label, value=False, key=None, help=None, on_change=None, args=None, kwargs=None, disabled=False)
 
Parameters
Parameter Type Description
label str Text displayed next to the checkbox
value bool Default state (False = unchecked, True = checked)
key str / int Unique identifier for the widget
help str Tooltip shown when hovering over the widget
on_change func Callback function executed when checkbox state changes
args/kwargs tuple/dict Arguments to pass to the callback function
disabled bool Disable the checkbox if True
 

Basic Example.

if checked, it shows the success message
if unchecked , it shows the info message
				
					import streamlit as st

st.title("Checkbox Demo")

subscribe = st.checkbox("Subscribe to Newsletter")

if subscribe:
    st.success("Thank you for subscribing")
else:
    st.info("You are not subscribed")

				
			

Multiple Checkboxes

We can create multiple independent checkboxes for different options
It’s great for multi-choice selections like hobbies, interests, or categories
				
					import streamlit as st
st.title("Choose your Powers")

super_strength = st.checkbox("Super strength")
super_speed = st.checkbox("Super speed")
invincible = st.checkbox("Invisible")
heat_vision = st.checkbox("Heat vision")



st.write("### Select your Super powers")
if super_strength:
    st.write("- Super strength")

if super_speed:
    st.write("- Super speed")

if invincible:
    st.write("- Invincible")

if heat_vision:
    st.write("- Heat vision")


				
			

Checkboxes in a loop

Instead of manually writing separate checkboxes manually, we can generate them dynamically using loops in python
Here we write a for loop that loops over the languages, then we use streamlit checkbox to select any of the programming languages
afterwards, we append the selected languages to the “selected_lang” and then we display the selected lang
				
					import streamlit as st
st.title("Favorite Programming language")

languages = ["Python", "JavaScript", "C++", "Java", "Go"]
selected_lang =[]

for lang in languages:
    if st.checkbox(lang):
        selected_lang.append(lang)
    

st.write("### You selected")
st.success(selected_lang)


				
			

Checkbox with Session State (Persistent State)

By default, when Streamlit reruns, all widget vales resets.
we use st.session_state to persist checkbox states. Read more about st.session_state: Click Here
				
					import streamlit as st

st.title("Session state checkbox example")

if "subscribe" not in st.session_state:
    st.session_state.subscribe = False

st.session_state.subscribe = st.checkbox("Subscribe to our Channel", value=st.session_state.subscribe)

if st.session_state.subscribe:
    st.success("You are subscribed")
else:
    st.warning("Please subscribe!")



				
			

Checkboxes with on_change Callback

We can triger a function whenever the checkbox state changes
This is useful when we want to update other widgets or trigger backend operations when the state changes.
 
				
					
def say():
    st.session_state.msg = "Welcome! Thanks for checking the box" if st.session_state.welcome else "Box unchecked"


st.title("Callback Example")
st.checkbox("Say Welcome", key="welcome", on_change=say)
st.write(st.session_state.get("msg", ""))

				
			

Checkboxes in Forms

CheckBoxes can also be used inside forms for grouped input submissions
 
Click here to learn more about Streamlit forms
				
					import streamlit as st

st.title("Newsletter Signup Form")

with st.form("Signup form"):
    accept_terms = st.checkbox("I accept the Terms & Conditions")
    submit = st.form_submit_button("Submit")

    if submit:
        if accept_terms:
            st.success("Thank you! You're signed up")
        else:
            st.error("You must accept the terms to proceed ")


				
			

Disable Checkboxes

we can also disable the checkbox, we set disabled=True, when we want to display but disable a checkbox
				
					import streamlit as st 
st.title("Disabled Checkbox Example")

st.checkbox("Already Subscribed", value=True, disabled=True)


				
			

Styling Checkboxes

By default Streamlit does not provide built-in CSS styling, but we can inject custom CSS using st.markdown
				
					import streamlit as st 
st.markdown(
     """
    <style>
    [data-baseweb="checkbox"] [data-testid="stWidgetLabel"] p {
        /* Styles for the label text */
        font-size: 1.2rem;
        color: #4CAF50; /* Green color for label */
    }
    [data-testid="stCheckbox"] label span {
        /* Styles for the checkbox itself */
        height: 1.5rem;
        width: 1.5rem;
        border: 2px solid #2196F3; /* Blue border */
        border-radius: 4px;
    }
    [data-testid="stCheckbox"] input:checked + div span {
        /* Styles for the checked state */
        background-color: #2196F3; /* Blue background when checked */
    }
    </style>
    """,
    unsafe_allow_html=True
    
)

st.checkbox("Styled Checkbox")

				
			
Best Practices
Always provide a label for better UI accessibility
Use st.session_state for persistent selections
Use on_change only when necessary
Use loops for dynamic checkbox generation
Disable checkboxes when necessary for better UX
 
 
 
Learn more about Streamlit: Click here
 
 
Watch Videos on Streamlit :

Leave a Reply

Your email address will not be published. Required fields are marked *