streamlit: st.dialog Does Not Close Immediately After User Action if Followed by Blocking Operation (e.g. time.sleep(15))
Checklist
- I have searched the existing issues for similar issues.
- I added a very descriptive title to this issue.
- I have provided sufficient information below to help reproduce this issue.
Summary
When using st.dialog to collect feedback from a user, if the feedback submission is followed by a blocking operation (e.g., time.sleep(15)), the dialog’s title remains visible on the screen until the blocking operation is complete. This creates a poor user experience because the user may expect the dialog to disappear immediately after submitting their input, but the dialog remains visible until the blocking operation finishes.
Reproducible Code Example
import time
import streamlit as st
@st.dialog("Cast your vote")
def vote(item):
st.write(f"Why is {item} your favorite?")
reason = st.text_input("Because...")
if st.button("Submit"):
st.session_state.vote = {"item": item, "reason": reason}
st.rerun()
if "vote" not in st.session_state:
st.write("Vote for your favorite")
if st.button("A"):
vote("A")
if st.button("B"):
vote("B")
else:
time.sleep(15)
f"You voted for {st.session_state.vote['item']} because {st.session_state.vote['reason']}"
Steps To Reproduce
- Use the provided code below to mimic collecting user feedback using
st.dialog. - After the user submits their feedback, the code simulates a blocking operation using
time.sleep(15). - Notice that the dialog’s title (“Cast your vote”) remains visible for the full 15 seconds while
time.sleep(15)is running.
Expected Behavior
The dialog should close immediately after the user submits their input, regardless of any blocking operation that follows, so that the UI responds promptly to the user’s action.
Current Behavior
The dialog remains open until the blocking operation (time.sleep(15)) is completed. This means the title “Cast your vote” is displayed on the screen for 15 seconds.
Is this a regression?
- Yes, this used to work in a previous version.
Debug info
- Streamlit version: 1.37.1
- Python version: 3.9
- Operating System: Mac OS / Linux Ubuntu
- Browser: Chrome
Additional Information
GIF:
Attach a GIF that shows the issue occurring.
Additional Context:
I plan to use st.dialog to collect user confirmation before running a long operation. However, due to this behavior, the dialog title remains visible until the operation completes, making the user experience confusing. Ideally, the dialog should close immediately after the user interaction, regardless of subsequent operations.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 16
- Comments: 17
If this issue affects you, please react with a 👍 (thumbs up emoji) to the initial post.
Your feedback helps us prioritize which bugs to investigate and address first.
Yes, please expose the ‘close’ function. We have an AI Chat App using the dialog to get some user input but the the modal won’t close until we have finished streaming the AI response
@cfarmer-fearless Try this brother. It works for me:
If it works for you, this should be fine for now 🙂 👍 It might not work in more strict environments / deployments when for example a CSP is used and, thus, the iframe used by
components.htmlis not accessible like this, but other than that it should be ok!@raethlein Do you see anything wrong with this approach?
@riyadparvez Try this working example that clearly illustrates both the problem and its solution.
It that doesn’t work for you, then it’s probably a browser issue.
Hey @pranay-tibco, thanks for opening the issue! I agree that the flow in the example is not great. For some background, right now we clean up stale nodes (e.g. the dialog after triggering st.rerun) after a
finished sessionmessage. This means that the cleanup is only triggered after the rerun is finished (here after the sleep), not when a new session / run starts.I believe for handling this in a nicer way, we probably would need to expose the
closefunction to the dialog so that you could manually indicate to the web app that you want to close it. However, here is a (little bit hacky) way of doing this right now:I have marke the issue as enhancement and not bug, because theoretically this is expected behavior in the current Streamlit execution model. cc @jrieke