Streamlit Text Input

The st.text_input() widget is used to accept single line text input from users in Streamlit apps.
It displays a single-line text input widget
It is commonly used for:
  • Usernames and passwords
  • Search bars
  • Form entries
  • Dynamic filtering

 

Are you looking for a streamlit developer? Click Here

Parameters

let’s quickly talk about some of the parameters in st.text_input() method
st.text_input(label, value=””, max_chars=None, key=None, type=”default”, help=None, autocomplete=None, on_change=None, args=None, kwargs=None, *, placeholder=None, disabled=False, label_visibility=”visible”, icon=None, width=”stretch”)
label (str): Short label (supports basic Markdown).

value (str/None): Default text.

max_chars (int): Limit character count.

key (str/int): Unique key for widget.

type (“default”/”password”): Normal or masked input.

help (str): Tooltip (supports Markdown).

autocomplete (str): HTML autocomplete property.

on_change (func): Callback when value changes.

args / kwargs: Extra args for callback.

placeholder (str): Hint text when empty.

disabled (bool): Disable input (default False).

label_visibility (“visible”/”hidden”/”collapsed”): Show/hide label.

icon (str): Emoji or Material icon inside input.

width (“stretch”/int): Widget width.

Basic Usage

we input a value and st.text_input() returns it as a string.
if we do not input anything, it returns an empty string
				
					import streamlit as st


st.title("📝 Basic Text Input Example")

name = st.text_input("Enter your name")

if name:
    st.write(f"Hello, {name} 👋")


				
			

Making use of Placeholder

Placeholder guides users with hints
				
					import streamlit as st

username = st.text_input("Username", placeholder="Please enter your user name")
st.write("You typed: ", username)
				
			

Password input

				
					import streamlit as st
password = st.text_input("Password", type="password")
st.write("Password hidden for security")

				
			

Default Values

This prefills a field with default text
				
					import streamlit as st

city = st.text_input("City", value="New York")
st.write("Your city:", city)
 
				
			

Text input inside a Form

				
					import streamlit as st

with st.form("Login form"):
    user = st.text_input("Username")
    password = st.text_input("Password", type="password")
    submitted = st.form_submit_button("Login")

if submitted:
    st.success(f"Welcome {user}")
				
			

Using Callbacks

A callback function ins basically a function called inside another function
				
					import streamlit as st

def handle_change():
    st.write("Value overridden")


query = st.text_input("value", on_change=handle_change)

				
			

Restricting Input (Validation)

Here we perform real time validation.
we check if user input has “@” in it.
				
					import streamlit as st

email = st.text_input("Email", placeholder="example@email.com")

if email and "@" not in email:
    st.error("❌ Invalid email address")
else:
    if email:
        st.success("✅ Valid email")

				
			

Multiple Inputs

We can also collect structure user information
				
					
import streamlit as st

name = st.text_input("Full Name")
email = st.text_input("Email")
phone = st.text_input("Phone Number")

if name and email and phone:
    st.write("### Your Details")
    st.write(f"👤 {name}")
    st.write(f"📧 {email}")
    st.write(f"📞 {phone}")


				
			

Search bar with filter

we can turn st.text_input into a search engine
				
					import streamlit as st
import pandas as pd

data = {"Name": ["Alice", "Bob", "Charlie", "David"],
        "City": ["Alberta", "Ottawa", "New York", "los angeles"]}

df = pd.DataFrame(data)

search = st.text_input("Search by name")

if search:
    results = df[df["Name"].str.contains(search, case=False)]
    st.write("Results: ", results)
else:
    st.write(df)

				
			

Want to learn more about streamlit? Click here

 

 

Watch videos on Streamlit:

Leave a Reply

Your email address will not be published. Required fields are marked *