How to add a number input widget in streamlit

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

Recipe Objective: How to add a number input widget in streamlit?

Streamlit allows you to add interactivity directly into the app with the help of widgets. You can number input widget to Streamlit app to accept numerical entries from user by using "st.number_input".

 Syntax: st.number_input(label, min_value, max_value, value, step, format, key, help, on_change, args, kwargs)
 Parameters:
   label -> A simple label that explains what this input widget is for
   min_value -> The minimum allowed value. There will be no minimum limit if None
   max_value -> The maximum allowed value. There will be no maximum limit if None
   value -> It is the value that is displayed on this widget on its first render. It defaults to min_value, or 0.0 if min_value is None
   step -> The stepping stride. If the value is an int, it defaults to 1; otherwise, it defaults to 0.01. The format argument will be used if the value is not defined.
   format -> A printf-style format string that specifies how numbers should be displayed in the interface. Numeric output is required. This has no bearing on the return value. Formatters that are acceptable: %d %e %f %g %i %u
   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 input.
   on_change -> An optional callback invoked when there is a change in this number input'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 number input widget
number = st.number_input('Enter your age: ')

#displaying the number entered by the user
st.write('Your age is ', number)

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

Mastering A/B Testing: A Practical Guide for Production
In this A/B Testing for Machine Learning Project, you will gain hands-on experience in conducting A/B tests, analyzing statistical significance, and understanding the challenges of building a solution for A/B testing in a production environment.

Classification Projects on Machine Learning for Beginners - 2
Learn to implement various ensemble techniques to predict license status for a given business.

Abstractive Text Summarization using Transformers-BART Model
Deep Learning Project to implement an Abstractive Text Summarizer using Google's Transformers-BART Model to generate news article headlines.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

LLM Project to Build and Fine Tune a Large Language Model
In this LLM project for beginners, you will learn to build a knowledge-grounded chatbot using LLM's and learn how to fine tune it.

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.

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

Multilabel Classification Project for Predicting Shipment Modes
Multilabel Classification Project to build a machine learning model that predicts the appropriate mode of transport for each shipment, using a transport dataset with 2000 unique products. The project explores and compares four different approaches to multilabel classification, including naive independent models, classifier chains, natively multilabel models, and multilabel to multiclass approaches.

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.

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.