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

Rerun when dismissing st.dialog by clicking on X or pressing Escape #8507

Open
2 tasks done
sfc-gh-pkommini opened this issue Apr 15, 2024 · 9 comments
Open
2 tasks done
Labels
area:events feature:st.dialog type:enhancement Requests for feature enhancements or new features

Comments

@sfc-gh-pkommini
Copy link

sfc-gh-pkommini commented Apr 15, 2024

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.

@sfc-gh-pkommini sfc-gh-pkommini added the type:enhancement Requests for feature enhancements or new features label Apr 15, 2024
Copy link

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

@jrieke
Copy link
Collaborator

jrieke commented Apr 16, 2024

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.

@jrieke
Copy link
Collaborator

jrieke commented Apr 16, 2024

Alternatively, you could also use st.toast to show the success message after the dialog was closed.

@jrieke jrieke changed the title Capture x-button click or esc button hit when closing an st.dialog Rerun when dismissing st.dialog by clicking on X or pressing Escape Apr 16, 2024
@jrieke
Copy link
Collaborator

jrieke commented Apr 16, 2024

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.

@sfc-gh-pkommini
Copy link
Author

sfc-gh-pkommini commented Apr 16, 2024

Alternatively, you could also use st.toast to show the success message after the dialog was closed.

This seems like a good idea but st.toast messages were very small and easy to miss at the bottom of the screen.

@jrieke
Copy link
Collaborator

jrieke commented Apr 16, 2024

Yup, we just merged a PR to move them to the top right and make the font a bit bigger (see #8433). I think this should also go out in 1.34, maybe it works better then!

@sfc-gh-pkommini
Copy link
Author

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?

@sfc-gh-pkommini
Copy link
Author

sfc-gh-pkommini commented Apr 16, 2024

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!

@jrieke
Copy link
Collaborator

jrieke commented Apr 16, 2024

it doesn't seem to persist after the st.rerun() on dialog close button click. Is that not supposed to happen?

Ah yeah right, I think I've also seen this while testing. I mean it kinda makes sense since a rerun clears all elements from the screen. But I can see that it's annoying in this use case. Feel free to open another issue on this if you like!

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

No branches or pull requests

2 participants