streamlit: Rerun when dismissing `st.dialog` by clicking on X or pressing Escape

Checklist

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

Summary

@jrieke: Is there a way to do a rerun on x-button click or on esc key hit?

Our use case is as follows:

We use the pop up to display a form with some drop downs where user selects some options Once the user submits the form_submit_button, we execute a Snowpark query to update backend data Once the query is successfully executes we display a success message On seeing the message we expect the user to close the dialog (We don’t automatically close because we want the user to really see and digest the success message) At this point we want our results on the home screen refreshed with the new results after the updates from the 2. For this we need to be able to use the x-button submit. Is there a workaround for this use case? Since I have a form in the dialog I can’t introduce a second button that executes a rerun, hence my ask to be able to capture the x-button click in st.session_state.

Why?

I’m frustrated when I can’t rerun the main app after the dialog is closed due to the x-button not being present in the st.session_state

How?

Create a key for st.dialog. Use the key parameter to set st.session_state.{key}-closed = True after the dialog is closed until full execution of the app and then reset to False.

Additional Context

Use case is where we use the dialog to update backend state and then want the closing of the dialog to reflect that backend state in the page that is presented once the dialog is closed.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 86
  • Comments: 15

Most upvoted comments

Note to self: we could also turn this into an event and add something like on_dismiss="ignore"|"rerun"|callback. Or maybe we could even fold dismissable into this parameter by doing on_dismiss="close"|"ignore"|"rerun"|callback.

@jrieke Wanted to follow-up on this and see if it is on your radar?

Hi @jrieke

I think that’s a fair request in general! For your specific use case, couldn’t you simply show another button after the success message that closes the dialog? I.e. something like:

@st.experimental_dialog("Dialog title")
def show_dialog():
    with st.form("my_form"):
        ...
        submitted = st.form_submit_button("Submit form")
        
    if submitted:
        st.success("Yay!")
        if st.button("Close"):
            st.rerun()

That would still show the X on the dialog, so you’d have to tell your users not to click that. We have been thinking about adding a dismissable parameter to st.experimental_dialog, that would hide the X (and prevent clicking on the background or pressing Escape), which might be helpful for this case.

Is there any movement on this? I’ve run into the same scenario where I don’t want to let the user click on the X but instead funnel them to the st.button path to have the app rerun and reset all my session state. It’s the end of the funnel for this application, so it would really help.

I’m making an editing dialog. Without this feature, it is very confusing to user. Is there any work around?

Note to self: we could also turn this into an event and add something like on_dismiss="ignore"|"rerun"|callback. Or maybe we could even fold dismissable into this parameter by doing on_dismiss="close"|"ignore"|"rerun"|callback.

This is brilliant! This would perfectly fit my use case and then some.

Also, for now I think I should be good with this pattern:

@st.experimental_dialog("Dialog title")
def show_dialog():
    with st.form("my_form"):
        ...
        submitted = st.form_submit_button("Submit form")
        
    if submitted:
        st.success("Yay!")
        if st.button("Close"):
            st.rerun()

I tested it and it works well! I’m all set to use this feature for our use case. Thank you for engaging!

Oh yeah just tried it on the nightly, it looks better but it doesn’t seem to persist after the st.rerun() on dialog close button click. Is that not supposed to happen?