Stuff written to st.empty() immediately gets overwritten

Summary

I’m trying to use a st.empty() placeholder at the bottom of my page, which I write to via callback. Unfortunately, anything I write to the st.empty() is immediately overwritten next app cycle. I was hoping to “reserve” a place at the bottom of the page, then write output there after doing some processing.

Steps to reproduce

Code snippet:

import streamlit as st

def process_user_input():
    user_input = st.session_state["user_input"]
    
    output = do_some_stuff_here()

    output_placeholder.text_area(label="Output", value=output)

st.text_area(label="User Input", placeholder="Enter stuff here", key="user_input", on_change=process_user_input)

output_placeholder = st.empty()

Expected behavior:

I thought this would let me write to the output_placeholder spot, replacing it with a st.text_area() once there was a user response.

Actual behavior:

The st.text_area() DOES show up for a split second, it’s just immediately overwritten.

I’m surprised this doesn’t throw an error, actually. The callback executes before the script (namely before your empty container is defined). In any case, your script always ends with output_placeholder = st.empty() so that 's always the last thing to execute.

Try storing your output in Session State then simply displaying it at the end.

import streamlit as st

def process_user_input():
    user_input = st.session_state["user_input"]
    output = f"Output from input: {user_input}"
    st.session_state["output"] = output

st.text_area(label="User Input", placeholder="Enter stuff here", key="user_input", on_change=process_user_input)
if "output" in st.session_state:
    st.write(st.session_state["output"])

If you only want to display your result briefly and then have it go away. You can delete the key from session state after displaying it:

del st.session_state["output"]

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