How to add a select slider widget in streamlit

In this recipe, we will learn how to add a select slider widget in Streamlit. We will also take a look at a Streamlit application consisting of a select slider widget.

Recipe Objective: How to add a select slider widget in streamlit?

Streamlit allows you to add interactivity directly into the app with the help of widgets. You can add a select slider to your Streamlit app using "st.select_slider" to select items from a list.

 Syntax: st.select_slider(label, options, value, format_func, key, help, on_change, args, kwargs)
 Parameters:
   label: A simple label that explains what this select slider is for
   options: Labels for the slider options. Internally, all choices will be cast to str by default.
   value: It is the value that is displayed on the select slider on its first render. A range slider with lower and upper boundaries is rendered if a tuple/list of two values is given here. When set to (1, 10) for example, the slider will have a configurable range of 1 to 10. The first choice is selected by default.
   format_func: This function modifies the display of the labels from the options. parameter, The option is passed as an argument, and the output is cast to str.
   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 select slider.
   on_change -> An optional callback invoked when there is a change in this select slider'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 select slider
no = st.select_slider('Select a number',options=[1,2,3,4,5,6,7,8,9,10])

#displaying the selected option
st.write('The number chosen is', no)

#adding a range select slider
start_hour, end_hour= st.select_slider('Please select your working hours',
options=['9:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00'],
value=('10:00', '18:00'))
#displaying the selected options
st.write('Your working hours are ', start_hour, 'to', end_hour)

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

Isolation Forest Model and LOF for Anomaly Detection in Python
Credit Card Fraud Detection Project - Build an Isolation Forest Model and Local Outlier Factor (LOF) in Python to identify fraudulent credit card transactions.

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.

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

Build Deep Autoencoders Model for Anomaly Detection in Python
In this deep learning project , you will build and deploy a deep autoencoders model using Flask.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Deploy Transformer-BART Model on Paperspace Cloud
In this MLOps Project you will learn how to deploy a Tranaformer BART Model for Abstractive Text Summarization on Paperspace Private Cloud

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

PyCaret Project to Build and Deploy an ML App using Streamlit
In this PyCaret Project, you will build a customer segmentation model with PyCaret and deploy the machine learning application using Streamlit.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python