3 2 Number Input Explained
Key Concepts
- st.number_input: A widget that allows users to input numeric values.
- Parameters: Understanding the parameters like
min_value
,max_value
, andstep
. - Interactive Use: How to use the number input to create interactive applications.
st.number_input
st.number_input
is a Streamlit widget that provides a numeric input field. Users can enter or adjust numeric values using this widget. It is particularly useful for applications that require precise numeric input, such as calculators, data entry forms, or interactive data analysis tools.
Parameters
The st.number_input
function accepts several parameters to customize its behavior:
label
: A string that provides a label for the input field.min_value
: The minimum allowable value for the input.max_value
: The maximum allowable value for the input.step
: The increment or decrement step for the input value.value
: The default value for the input field.
Interactive Use
Using st.number_input
, you can create interactive applications where users can input numeric values that affect the output. For example, you can create a simple calculator that performs operations based on user input.
Examples
Here are some examples to illustrate the use of st.number_input
:
import streamlit as st st.title("Number Input Example") # Basic number input number = st.number_input("Enter a number", min_value=0, max_value=100, step=1) st.write(f"You entered: {number}") # Advanced number input with default value age = st.number_input("Enter your age", min_value=18, max_value=100, step=1, value=25) st.write(f"Your age is: {age}") # Interactive calculator num1 = st.number_input("Enter the first number", min_value=0, max_value=1000, step=1) num2 = st.number_input("Enter the second number", min_value=0, max_value=1000, step=1) operation = st.selectbox("Select operation", ["Add", "Subtract", "Multiply", "Divide"]) if operation == "Add": result = num1 + num2 elif operation == "Subtract": result = num1 - num2 elif operation == "Multiply": result = num1 * num2 elif operation == "Divide": result = num1 / num2 st.write(f"Result: {result}")
In the first example, a basic number input is created with a range from 0 to 100. The second example demonstrates setting a default value and using a more specific range. The third example shows how to create an interactive calculator using multiple number inputs and a selectbox for operations.