Is there any way to use random placeholder for text_input without loosing variable data?

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?

Thanks in advance!


Debug info:

  • streamlit==1.37.0
  • Python 3.11.7

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.

You can add this to fix, for one user it will be always the same (random) choice.

if 'placeholder' not in st.session_state:
        st.session_state.placeholder = random.choice(placeholders)

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.