How to add checkboxes in Streamlit

In this recipe, we will learn how to add checkboxes in Streamlit. We will also take a look at a Streamlit application which consists of checkboxes.

Recipe Objective: How to add checkboxes in streamlit?

Streamlit allows you to add interactivity directly into the app with the help of widgets. You can add a check box to the Streamlit app using "st.checkbox". It returns a boolean value.

 Syntax: st.checkbox(label, value, key, help, on_change, args, kwargs)
 Parameters:
   label -> A simple label that explains what this checkbox is for
   value -> Displays a preselected checkbox on the first render. Internally, this will be cast to bool.
   key -> An optional string or int to be used as a unique key for this widget. If omitted, a key will be generated for the widget based on its content. Multiple widgets of same types cannot share the same key.
   help -> An optional tooltip that get displayed next to the checkbox.
   on_change -> An optional callback invoked when there is a change in this checkbox's value
   args -> An optional tuple of args that can be passed to the callback
   kwargs -> An optional dictionary of kwargs that can be passed to the callback

Code:

#importing required libraries
import streamlit as st

#adding a checkbox
agree = st.checkbox('I agree to the given conditions.')

#specifying what should be displayed if the check box is selected
if agree:
st.write('Welcome Aboard!!')

To run the app, either create an appname.py file with the above code using any text editor, or if you are using a jupyter notebook, you need to download your .ipynb notebook as a Python (.py) file and run the same using the "streamlit run appname.py" command. Once you run the command, the app will automatically open in your default browser.

What Users are saying..

profile image

Abhinav Agarwal

Graduate Student at Northwestern University
linkedin profile url

I come from Northwestern University, which is ranked 9th in the US. Although the high-quality academics at school taught me all the basics I needed, obtaining practical experience was a challenge.... Read More

Relevant Projects

Build Real Estate Price Prediction Model with NLP and FastAPI
In this Real Estate Price Prediction Project, you will learn to build a real estate price prediction machine learning model and deploy it on Heroku using FastAPI Framework.

Learn to Build a Polynomial Regression Model from Scratch
In this Machine Learning Regression project, you will learn to build a polynomial regression model to predict points scored by the sports team.

Build a Multi Class Image Classification Model Python using CNN
This project explains How to build a Sequential Model that can perform Multi Class Image Classification in Python using CNN

Loan Default Prediction Project using Explainable AI ML Models
Loan Default Prediction Project that employs sophisticated machine learning models, such as XGBoost and Random Forest and delves deep into the realm of Explainable AI, ensuring every prediction is transparent and understandable.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.

Build Multi Class Text Classification Models with RNN and LSTM
In this Deep Learning Project, you will use the customer complaints data about consumer financial products to build multi-class text classification models using RNN and LSTM.

Model Deployment on GCP using Streamlit for Resume Parsing
Perform model deployment on GCP for resume parsing model using Streamlit App.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

FEAST Feature Store Example for Scaling Machine Learning
FEAST Feature Store Example- Learn to use FEAST Feature Store to manage, store, and discover features for customer churn prediction machine learning project.