Skip to main content

Accept User Input in Streamlit Using Text Boxes

Text Inputs: Accept User Input Using Text Boxes in Streamlit

In Streamlit, st.text_input() allows you to create text boxes where users can input text. This is useful for applications that require user input, such as search bars, forms, or customizable options.


1. Creating a Simple Text Input

To create a simple text input box, use the st.text_input() function. You can pass a prompt message that appears inside the box as a placeholder.

import streamlit as st

user_input = st.text_input("Enter your name:")
st.write("Hello, ", user_input)

This example displays a text input box where the user can enter their name, and the app greets the user once they submit their input.

output:

streamlit-simple-input-output

2. Text Input with Default Value

You can also specify a default value inside the text box by passing it as the second argument to st.text_input().

import streamlit as st

user_input = st.text_input("Enter your favorite color:", "Red")
st.write("Your favorite color is: ", user_input)

Here, the default text in the input box is "Red", but users can change it to any color they prefer.

output:

streamlit-input-set-Default-Value

3. Text Input with Customization

Streamlit allows you to customize the appearance of your text input fields using additional parameters such as max_chars to limit the number of characters or type to restrict input to certain data types.

user_input = st.text_input("Enter your age:", max_chars=3)
st.write("Your age is: ", user_input)

This example limits the number of characters to 3, ensuring users can only enter a 3-digit age.


4. Using Text Input for Form Submissions

You can combine st.text_input() with st.button() to create a form submission system where the text input value is submitted when the button is clicked.

user_input = st.text_input("Enter your email:")
if st.button("Submit"):
    st.write("Email submitted:", user_input)

In this example, the user can enter their email and click "Submit" to display the inputted email.


5. Difference Between st.text_input() and Other Input Functions

Streamlit provides different input methods for various use cases:

  • st.text_area() is used for longer text inputs, like descriptions or multi-line content.
  • st.number_input() is used when numeric values are expected, such as ages, prices, or quantities.
  • st.date_input() and st.time_input() are used for selecting dates and times.

Use st.text_input() for single-line text input, such as names, email addresses, or short responses.


Example 01

import streamlit as st
    
user_input = st.text_input("Enter your city:")
if st.button("Submit"):
    st.write("City submitted:", user_input)

Output:

user-input-with-button-in-streamlit

Example 02

import streamlit as st
from datetime import datetime

# Title
st.title("Input Functions Demonstration")

# Single-line text input
name = st.text_input("Enter your name:")
if name:
    st.write(f"Hello, {name}!")

# Multi-line text area
description = st.text_area("Enter a description about yourself:")
if description:
    st.write("Your description:", description)

# Numeric input
age = st.number_input("Enter your age:", min_value=1, max_value=120, step=1)
if age:
    st.write(f"Your age: {age}")

# Date input
selected_date = st.date_input("Select a date:")
if selected_date:
    st.write(f"You selected the date: {selected_date}")

# Time input
selected_time = st.time_input("Select a time:", datetime.now().time())
if selected_time:
    st.write(f"You selected the time: {selected_time}")

# Submit button to display all inputs
if st.button("Submit All"):
    st.write("Summary of Your Inputs:")
    st.write(f"Name: {name}")
    st.write(f"Description: {description}")
    st.write(f"Age: {age}")
    st.write(f"Date: {selected_date}")
    st.write(f"Time: {selected_time}")

Output:

streamlit-form-demonstration-using-various-type-input

Conclusion

The st.text_input() function is an essential tool for gathering user input in Streamlit applications. By creating simple, customizable text boxes, you can enhance the interactivity of your app and collect valuable data from users.

Comments