How to add radio buttons in Streamlit

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

Recipe Objective: How to add radio buttons in streamlit?

Streamlit allows you to add interactivity directly into the app with the help of widgets. You can add radio buttons to the Streamlit app using "st.radio". It returns a string value.
 Syntax: st.radio(label, options, index, key, help, on_change, args, kwargs)
 Parameters:
   label -> A simple label that explains what this radio button is for
   options -> The labels for the radio options. Internally, this will be cast to str by default. The first column is chosen for pandas.DataFrame.
   index -> The index of the preselected option on the first render.
   format_func -> This function allows you to change how radio options are displayed. It takes the raw option as an argument and outputs the label that should be displayed for that option. The radio's return value is unaffected by this.
   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 gets displayed by the button
   on_change -> An optional callback invoked when this radio's value is changed
   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 radio button
ques = st.radio(
"Do you like coding?",
('Yes','No'))

#specifying what should be display when the radio button is selected
if ques == 'Yes':
st.write('You like coding.')
else:
st.write("You do not like coding.")

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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

NLP Project on LDA Topic Modelling Python using RACE Dataset
Use the RACE dataset to extract a dominant topic from each document and perform LDA topic modeling in python.

CycleGAN Implementation for Image-To-Image Translation
In this GAN Deep Learning Project, you will learn how to build an image to image translation model in PyTorch with Cycle GAN.

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

Build ARCH and GARCH Models in Time Series using Python
In this Project we will build an ARCH and a GARCH model using Python

End-to-End Snowflake Healthcare Analytics Project on AWS-2
In this AWS Snowflake project, you will build an end to end retraining pipeline by checking Data and Model Drift and learn how to redeploy the model if needed

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

Build a CNN Model with PyTorch for Image Classification
In this deep learning project, you will learn how to build an Image Classification Model using PyTorch CNN

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

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 Touch Attribution Machine Learning Model in Python
Identifying the ROI on marketing campaigns is an essential KPI for any business. In this ML project, you will learn to build a Multi Touch Attribution Model in Python to identify the ROI of various marketing efforts and their impact on conversions or sales..