streamlit / streamlit

Streamlit — A faster way to build and share data apps.
https://streamlit.io
Apache License 2.0
36.01k stars 3.11k forks source link

Toasts are not preserved when page reloaded #7740

Open arogozhnikov opened 1 year ago

arogozhnikov commented 1 year ago

Checklist

Summary

My main use-case for toasts is providing some information about result when flushing the page. E.g. data entered and stored in the database. To prevent further interaction with page (to avoid duplicates or incorrect behavior), I call rerun. That's where toasts could be helpful, but unfortunately they do not appear if page is reloaded afterwards

Reproducible Code Example

import streamlit as st

if st.button("just make some toasts"):
    st.toast("Hi there, here is my toast-1")

if st.button("make some toasts and rerun"):
    st.toast("Hi there, here is my toast-2")
    st.experimental_rerun()

Steps To Reproduce

see expected code above. When hitting the second button, toast does not appear for user.

Expected Behavior

In both cases toast should be visible for 4 seconds.

Current Behavior

No response

Is this a regression?

Debug info

Additional Information

No response

github-actions[bot] commented 1 year ago

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

MaraHochstein commented 5 months ago

You could try a workaround and save the toast message & icon in st.session_state and display any saved data as toast. I use this to show a toast after st.switch_page (page 1 generates the toast data --> switch to page 2 --> page 2 displays toast data)

import streamlit as st
from streamlit import session_state as sst

# init sst variable
if 'infoToast' not in sst:
    sst['infoToast'] = {}

# show info toast if in sst & clear
def showInfoToast():
    if sst.infoToast != {}:
        toastTxt = sst.infoToast['txt']
        toastIco = sst.infoToast['ico']
        sst.infoToast = {}
        st.toast(toastTxt, icon=toastIco)

showInfoToast()    

if st.button('make a toast and rerun'):
    sst.infoToast['txt'] = 'Hi there, here is my toast after rerun'
    sst.infoToast['ico'] = ':material/info:'
    st.rerun()
arogozhnikov commented 5 months ago

@MaraHochstein this code wouldn't show things properly in case of two reloads, and can show it on new page even after timeout.

this should be a part of core functionality of widget (and that's very simple to implement on browser side), not some workarounds in python

MaraHochstein commented 5 months ago

@arogozhnikov Sure, this is a workaround since it is not implemented in streamlit. And, to achieve your desired functionality with this, you would have to do some extra modifications to the sst variable, e.g. save multiple toast infos in the dict (by time of creation) and loop through, save the timing, save the reference site or whatever…

RubenVanEldik commented 3 months ago

This issue has become even more pertinent with the new dialogs, since st.rerun() is a core part of the dialog functionality.

I think one of the core features of the dialog is to let a user create/update/delete some data, and it would be perfect to use a st.toast() when closing the dialog to let the user know that the thing has been properly created/updated/deleted.

Could someone from the Streamlit team weigh in and tell if fixing this is feasible or if its core drawback of the way Streamlit works. If it would be feasible I am willing to look into it in the near future to figure out how to solve this.

FaediMichele commented 3 weeks ago

any news?