Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 tasks done
sfc-gh-dmatthews opened this issue Jan 7, 2025 · 4 comments
Labels
area:utilities type:enhancement Requests for feature enhancements or new features

Comments

@sfc-gh-dmatthews
Copy link
Collaborator

sfc-gh-dmatthews commented Jan 7, 2025

Checklist

  • I have searched the existing issues for similar feature requests.
  • I added a descriptive title and summary to this issue.

Summary

Call st.close() to immediately discard all existing elements that follow (within whatever container context the command was called). e.g. Call st.close() within st.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 using with notation, Streamlit will "close" the container when you leave the context manager.)

Additional Context

Related: #5044, #2820, #9239

@sfc-gh-dmatthews sfc-gh-dmatthews added the type:enhancement Requests for feature enhancements or new features label Jan 7, 2025
Copy link

github-actions bot commented Jan 7, 2025

To help Streamlit prioritize this feature, react with a 👍 (thumbs up emoji) to the initial post.

Your vote helps us identify which enhancements matter most to our users.

Visits

@sfc-gh-dmatthews sfc-gh-dmatthews changed the title st.clear() to forcibly and immediately discard all elements that follow (like a frontend version of st.stop()) st.clear() or st.end() to forcibly and immediately discard all elements that follow (like a frontend version of st.stop()) Jan 7, 2025
@lukasmasuch
Copy link
Collaborator

In theory, that could work to allow users to fix stale issues themselves. The main issue causing stale elements is that we don't know when the last element will be inserted into the container. That's why we must wait until the full script finishes to clean up removed elements. If we had something like this utility, it would be possible for the user to tell the frontend sooner that a container has been "closed" (no more elements will be inserted) -> this would allow an optimized rendering. A good name here could also be close().

@sfc-gh-dmatthews sfc-gh-dmatthews changed the title st.clear() or st.end() to forcibly and immediately discard all elements that follow (like a frontend version of st.stop()) 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()) Jan 7, 2025
@lukasmasuch
Copy link
Collaborator

It is a bit related to this feature request (but a bit different mechanic to achieve a similar result): #6063

@sfc-gh-dmatthews
Copy link
Collaborator Author

I updated the issue to say "close" instead of "clear." Good name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:utilities type:enhancement Requests for feature enhancements or new features
Projects
None yet
Development

No branches or pull requests

2 participants