st.close()
or st.end()
to close or mark the end of a container and remove stale elements (like a frontend version of st.stop()
)
#10126
Labels
Checklist
Summary
Call
st.close()
to immediately discard all existing elements that follow (within whatever container context the command was called). e.g. Callst.close()
withinst.chat_message()
to discard any remaining (usually stale) elements beyond that point.Why?
With dynamic pages and long computation times come stale elements. Especially in LLM apps that include streamed content, app developers need to be mindful of transient elements that may linger in stale form while a script run completes. When you reduce the total number of elements in a container from one script run to the next, the extra elements do not get discarded until the end of the script run. This necessitates extra boilerplate code to handle a decrease in total elements, especially when run times are longer than a second (the fade time on stale elements).
How?
MVP:
st.close()
could discard all following frontend elements within whatever container context its called (instead of Streamlit waiting for the end of the script run to know it's done writing to the container).Optional: A parameter could modify how widgets are discarded if they exist (to fully clean them up and make the next call "like new" or merely discard the frontend as if it was replaced by something else, holding off until the end of the script run to discard its state).
I imagine you could still "reopen" the container by writing to it again. Also, a configuration option could be set to implicitly utilitize
st.close()
when leaving a container's context. (e.g. When usingwith
notation, Streamlit will "close" the container when you leave the context manager.)Additional Context
Related: #5044, #2820, #9239
The text was updated successfully, but these errors were encountered: