3 1 Text Input Explained
Key Concepts
Text input in Streamlit allows users to enter text data interactively. The primary function for this is st.text_input()
. This function creates a text box where users can type in their input, which can then be processed by your Streamlit app.
Explanation
1. st.text_input()
st.text_input()
is used to create a single-line text input field. Users can type any text into this field, and the input is returned as a string. This function takes several parameters, including a label to describe the input field and an optional default value.
2. Label
The label parameter in st.text_input()
is a string that describes the purpose of the text input field. This label is displayed next to the text box, helping users understand what kind of input is expected.
3. Default Value
The default value parameter allows you to specify a string that will be pre-filled in the text input field when the app is first loaded. This can be useful for providing hints or default options to the user.
Examples
Example 1: Basic Text Input
import streamlit as st st.title("Basic Text Input Example") user_input = st.text_input("Enter your name") st.write(f"Hello, {user_input}!")
Example 2: Text Input with Default Value
import streamlit as st st.title("Text Input with Default Value") default_text = "Type something here" user_input = st.text_input("Enter some text", default_text) st.write(f"You entered: {user_input}")
Analogies
Think of st.text_input()
as a digital form field where users can type in their responses. The label is like a prompt or question that guides the user on what to enter, while the default value is like a pre-filled suggestion that the user can either accept or modify.
Conclusion
Understanding and using st.text_input()
effectively can greatly enhance the interactivity of your Streamlit app. By providing clear labels and useful default values, you can guide users to enter the right kind of data, making your app more user-friendly and efficient.