Streamlit Text Area

st.text_area displays a multi-line text input widget

It is used when we want users to enter large amounts of text such as feedback, descriptions , comments, e,t,c
Need a Streamlit developer? Click here

Syntax

st.text_area(label, value=””, height=None, max_chars=None, key=None, help=None, on_change=None, args=None, kwargs=None, *, placeholder=None, disabled=False, label_visibility=”visible”, width=”stretch”)
 
ParameterTypeDefaultDescription
labelstrRequiredThe label or title displayed above the text area.
valuestr""Default text that pre-fills the text area.
heightintNoneSets the height of the text area in pixels.
max_charsintNoneMaximum number of characters a user can enter.
keystr or intNoneA unique key to identify the widget, useful when using multiple widgets.
helpstrNoneTooltip text displayed when hovering over the widget.
on_changecallableNoneA function or callback triggered when the text changes.
argstupleNoneA tuple of positional arguments passed to the on_change callback function.
kwargsdictNoneA dictionary of keyword arguments passed to the on_change callback function.
placeholderstrNoneShows a hint inside the text area until the user starts typing.
disabledboolFalseIf True, the text area becomes read-only and users cannot type.
label_visibilitystr"visible"Controls the label display: "visible", "hidden", or "collapsed".
widthstr"stretch"Defines the widget’s width. Options: "stretch" (default) or a custom CSS width like "400px".

Basic Example

Here, we display a text area
we store user input in “input”
Then we display it
				
					import streamlit as st

st.title("Basic Text area")

input = st.text_area("Enter your comments:")

st.write("You wrote")
st.write(input)
				
			

Handling Input from users

Of course we can perform actions based on what the user types.
				
					import streamlit as st

st.title("Feedback Form")

feedback = st.text_area("Leave a feedback:")


if st.button("Submit feedback"):
    if feedback:
        st.success("Thanks for the feedback!")
        st.write("Your feedback:", feedback)
    else:
        st.warning("Please enter a feedback before submitting")

				
			

Controlling Parameters

we can control the size, default values, placeholders, label e.t.c of the st.text_area
value: Pre-fills the text area.
height: Makes the box taller.
placeholder: Shows hint text.
				
					import streamlit as st

st.title("Customizing Text Area")

bio = st.text_area(
    label="Say something about yourself",
    value = "Hi, i'm learning streamlit!",
    height = 200,
    placeholder = "Write something interesting about yourself..."
)


st.write("Bio")
st.write(bio)

				
			

Multi-Line Text

By default, st.text_area supports multiple lines, which makes it ideal for writing  paragraphs, notes, or code.
				
					import streamlit as st

st.title("Code Snippet Input")

code = st.text_area(
    "Paste your Python code here",
    height=200,
    placeholder="Type or paste your Python code..."
)

if st.button("Run Code"):
    st.write("You entered the following code:")
    st.code(code, language="python")

				
			

Character Counters and Limits

st.text_area has the max_chars parameter, which restricts the number of characters.
Note you might have to press ctrl + enter after typing in the text area in other to trigger the follow up code
				
					import streamlit as st

st.title("Limited Text Area")

description = st.text_area(
    "Write your short bio:",
    max_chars=100,
    placeholder="Maximum 100 characters allowed"
)

st.write(f"Characters used: {len(description)}/100")

				
			

Integrating with other widgets.

we can use st.text_area with many other widgets in streamlit.

:

				
					import streamlit as st

st.title("Text Analyzer")

text = st.text_area("Enter some text to analyze")
option = st.selectbox("Choose an analysis type:", ["Word Count", "Character Count"])

if st.button("Analyze"):
    if option == "Word Count":
        st.write(f"Word Count: {len(text.split())}")
    else:
        st.write(f"Character Count: {len(text)}")


				
			
st.text_area is powerful because:
It supports multi-line input.
It works well with real-time updates.
It integrates seamlessly with other Streamlit widgets.
 
 
Learn more about Streamlit: Click here
 
Watch videos on Streamlit:

Leave a Reply

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