Issue: Streamlit text_input with random placeholder not displaying user input
I am debugging on my local machine windows 10.
I am having an issue with my Streamlit app where the user input is not displaying correctly when using a random placeholder. If I use a static placeholder, it works fine. However, when I switch to a random placeholder, whatever input entered by user doesn’t get display after clicking button.
Here’s my code:
import streamlit as st
import random
placeholders = ["Enter your name", "Type your message", "Say something", "What's on your mind?"]
# Get user input with a random placeholder
user_input = st.text_input("Enter your input:", placeholder=random.choice(placeholders), key='name')
# Create a button to print the user input
if st.button("Greet"):
if user_input:
st.write(f"Hello, {user_input}")
else:
st.write("please enter input")
Current Output: please enter input
Expected Output: Hello, [user input]
I just want to use random placeholder and whatever user will enter it should display when user clicks button.
Has anyone faced a similar issue or can suggest a workaround to use random placeholders for user input in Streamlit?
Hello,
The issue you’re facing is likely due to how Streamlit handles state and re-runs. When you use random.choice(placeholders) directly in the st.text_input() function, it generates a new random placeholder every time the app reruns (which happens when the button is clicked). This can cause Streamlit to think the input widget has changed, potentially clearing or ignoring the user’s input.
placeholders = ["Enter your name", "Type your message", "Say something", "What's on your mind?"]
# Use session state to store the random placeholder
st.session_state.placeholder = random.choice(placeholders)
# Get user input with a random placeholder
user_input = st.text_input("Enter your input:",placeholder=st.session_state.placeholder, key='user_input')
# Create a button to print the user input
if st.button("Greet"):
if user_input:
st.write(f"Hello, {user_input}")
else:
st.write("Please enter input")
Thank you for your time @Faltawer
the code which you provided works fine if user interact like this :
step 1) entered “text” pressed button >> hello text is displayed
step 2) user pressed button without entering text >> please enter input displayed
upto this it’s fine
but if user interact like this :
step 1) entered “text” pressed button >> hello text is displayed
step 2) entered “another text” pressed button >> please enter input displayed
which we don’t want it should print “another text” right ?
I need a way to handle this unexpected behavior of input field properly.
no matter how many times user enters text and press button it should work.
I appreciate your help.
edit: even when I click on empty area it’s changing placeholder and removing the output from the screen which i don’t want.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.