Streamlit is a powerful Python library that allows developers to create interactive web applications with ease. One of its key features is the ability to handle user input efficiently, enabling the creation of dynamic and responsive apps. In this guide, we'll explore various methods of capturing user input in Streamlit and how to leverage them effectively.
Let's start with the most fundamental form of user input: text. Streamlit provides the st.text_input()
function for this purpose.
import streamlit as st user_name = st.text_input("Enter your name", "John Doe") st.write(f"Hello, {user_name}!")
In this example, we create a text input field with a default value of "John Doe". The entered value is stored in the user_name
variable, which we then use to greet the user.
For numerical inputs, Streamlit offers st.number_input()
. This function allows you to specify a range and step size for the input.
age = st.number_input("Enter your age", min_value=0, max_value=120, value=30, step=1) st.write(f"You are {age} years old.")
Here, we've set the minimum value to 0, maximum to 120, default value to 30, and step size to 1. This ensures that users can only enter valid ages.
Sliders are great for allowing users to select from a range of values. The st.slider()
function provides this functionality.
temperature = st.slider("Select temperature", min_value=-10.0, max_value=40.0, value=20.0, step=0.1) st.write(f"The selected temperature is {temperature}°C")
This creates a slider for temperature selection with a range from -10°C to 40°C, a default value of 20°C, and a step size of 0.1°C.
For yes/no questions or multiple-choice selections, Streamlit provides checkboxes and radio buttons.
# Checkbox agree = st.checkbox("I agree to the terms and conditions") if agree: st.write("Thank you for agreeing!") # Radio buttons fruit = st.radio("Choose your favorite fruit", ("Apple", "Banana", "Cherry")) st.write(f"You selected: {fruit}")
Checkboxes return a boolean value, while radio buttons return the selected option as a string.
For longer lists of options, a selectbox (dropdown menu) is more appropriate:
country = st.selectbox("Select your country", ["USA", "UK", "Canada", "Australia", "Other"]) st.write(f"You're from {country}")
This creates a dropdown menu with country options, allowing users to select one.
When users need to select multiple options, the st.multiselect()
function comes in handy:
languages = st.multiselect("Select the programming languages you know", ["Python", "JavaScript", "Java", "C++", "Ruby"]) st.write(f"You know {len(languages)} languages: {', '.join(languages)}")
This allows users to select multiple programming languages from the list.
For date selection, Streamlit provides a calendar widget:
import datetime date = st.date_input("Select a date", datetime.date(2023, 1, 1)) st.write(f"Selected date: {date}")
This creates a calendar widget with a default date of January 1, 2023.
Streamlit also supports file uploads, which is crucial for many data processing applications:
uploaded_file = st.file_uploader("Choose a CSV file", type="csv") if uploaded_file is not None: data = pd.read_csv(uploaded_file) st.write(data.head())
This code creates a file uploader that accepts CSV files. When a file is uploaded, it reads the CSV data and displays the first few rows.
Provide clear instructions: Always include clear labels and instructions for each input widget.
Use appropriate input types: Choose the right widget for the type of data you're collecting.
Set sensible defaults: Provide default values where possible to guide users.
Validate input: Always validate user input to prevent errors and ensure data integrity.
Responsive feedback: Use Streamlit's reactive nature to provide immediate feedback on user input.
Combine widgets: Create complex forms by combining different input widgets.
Use session state: For more advanced applications, leverage Streamlit's session state to persist user input across reruns.
By following these guidelines and exploring the various input options Streamlit offers, you can create rich, interactive web applications that effectively capture and utilize user input. Remember, the key to a great Streamlit app is creating an intuitive and responsive user experience.
06/12/2024 | Python
15/11/2024 | Python
06/10/2024 | Python
15/11/2024 | Python
26/10/2024 | Python
26/10/2024 | Python
15/11/2024 | Python
15/11/2024 | Python
17/11/2024 | Python
17/11/2024 | Python
25/09/2024 | Python
15/10/2024 | Python