How to add multiselect option in Streamlit

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

Recipe Objective: How to add a multi select box in streamlit?

Streamlit allows you to add interactivity directly into the app with the help of widgets. You can add a multi-select box to the Streamlit app using "st.multiselect". It returns a list of strings containing the selected options.

 Syntax: st.multiselect(label, options, default, format_func, key, help,on_change, args, kwargs)
 Parameters:
   label -> A simple label that explains what this multi select box is for
   options -> The labels for the select options. Internally, this will be cast to str by default. The first column is chosen for pandas.DataFrame.
   default -> A list of default values.
   format_func -> This function can change the display of the selectbox. It takes the raw option as an argument and outputs the label that should be displayed for that option. The selectbox'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 get displayed next to the multi select box.
   on_change -> An optional callback invoked when there is a change in this multi select box'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 multiselect box
options = st.multiselect('Which beverage do you like?', ['Tea', 'Coffee','Iced Tea' ,'Diet Coke', 'Lemonade'],['Tea', 'Coffee'])

#displaying the selected options
st.write('You have selected:', options)

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

Gautam Vermani

Data Consultant at Confidential
linkedin profile url

Having worked in the field of Data Science, I wanted to explore how I can implement projects in other domains, So I thought of connecting with ProjectPro. A project that helped me absorb this topic... Read More

Relevant Projects

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

Learn Hyperparameter Tuning for Neural Networks with PyTorch
In this Deep Learning Project, you will learn how to optimally tune the hyperparameters (learning rate, epochs, dropout, early stopping) of a neural network model in PyTorch to improve model performance.

Credit Card Default Prediction using Machine learning techniques
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

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

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.

Many-to-One LSTM for Sentiment Analysis and Text Generation
In this LSTM Project , you will build develop a sentiment detection model using many-to-one LSTMs for accurate prediction of sentiment labels in airline text reviews. Additionally, we will also train many-to-one LSTMs on 'Alice's Adventures in Wonderland' to generate contextually relevant text.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

Learn to Build an End-to-End Machine Learning Pipeline - Part 2
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, incorporating Hopsworks' feature store and Weights and Biases for model experimentation.

Image Segmentation using Mask R-CNN with Tensorflow
In this Deep Learning Project on Image Segmentation Python, you will learn how to implement the Mask R-CNN model for early fire detection.