How to add a slider in streamlit

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

Recipe Objective: How to add a slider in streamlit?

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

 Syntax: st.slider(label, min_value, max_value, value, step, format, key, help, on_change, args, kwargs)
 Parameters:
   label -> A simple label that explains what this slider is for
   min_value -> The minimum allowed value. It defaults to 0 if the value is an int, 0.0 if a float, value - timedelta(days=14) if a date/datetime, time.min if a time
   max_value -> The maximum allowed value. It defaults to 100 if the value is an int, 1.0 if a float, value + timedelta(days=14) if a date/datetime, time.max if a time
   value -> It is the value that is displayed on the slider on its first render. A range slider with those lower and upper boundaries is rendered if a tuple/list of two values is given here. It defaults to the minimum value.
   step -> The stepping interval. It defaults to 1 if the value is an int, 0.01 if a float, timedelta(days=1) if a date/datetime, timedelta(minutes=15) if a time (or if max_value - min_value < 1 day)
   format -> A printf-style format string that specifies how numbers should be displayed in the interface. This has no bearing on the return value.
   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 slider.
   on_change -> An optional callback invoked when there is a change in this 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

Explore the Real-World Applications of Recommender Systems


Code:

#importing required libraries
import streamlit as st
from datetime import time
from datetime import datetime

#adding a simple slider
age = st.slider('Please enter your age', 0, 130, 25)

#displaying the selected value
st.write("You are ",age," years old.")

#adding a range slider
values = st.slider('Please select a range of values',0.0, 100.0, (40.0, 80.0))

#displaying the selected values
st.write('Values selected: ', values)

#adding a time slider
appointment = st.slider("Choose a feasible time for your appointment: ",value=(time(10, 00), time(11, 00)))

#displaying the selected value
st.write("You are scheduled for:", appointment)

#adding a datetime slider
interview_time = st.slider("Enter your interview date and time: ",value=datetime(2021, 10, 23, 10, 30),format="MM/DD/YY - hh:mm")

#displaying the selected value
st.write("Interview time:", interview_time)

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

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Time Series Project to Build a Multiple Linear Regression Model
Learn to build a Multiple linear regression model in Python on Time Series Data

Recommender System Machine Learning Project for Beginners-3
Content Based Recommender System Project - Building a Content-Based Product Recommender App with Streamlit

Build an Image Segmentation Model using Amazon SageMaker
In this Machine Learning Project, you will learn to implement the UNet Architecture and build an Image Segmentation Model using Amazon SageMaker

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.

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.

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Build a Collaborative Filtering Recommender System in Python
Use the Amazon Reviews/Ratings dataset of 2 Million records to build a recommender system using memory-based collaborative filtering in Python.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.