How can I use time.sleep() only for "success", "warning" and "info"

I just want the st.success mark to go away after 1-2 seconds.

if st.button("Click"):
       if len(list) > 0:
              st.succes("found")
              time.sleep(2)
              st.experimental_rerun()
              for i in range(len(list)):
                      st.write(list[i])

When I use “st.experimental rerun()”, my outputs (list[i] values) are also disappear

Hi @KANZ :wave:

In this case, you don’t want to use st.experimental_rerun. You can instead:

  1. Use st.empty() to insert a single-element container that can be used to hold a single element.
  2. Create the success alert in the container from step 1.
  3. Wait for 1-2 seconds using time.sleep()
  4. Clear the container by calling it’s .empty() method.

Here’s a working example:

Solution

import time
import streamlit as st
mylist = [1, 2, 3, 4, 5]

if st.button("Click"):
    if len(mylist) > 0:
        # Insert a single-element container
        # that can be used to hold a single element (e.g. st.success)
        container = st.empty()
        container.success("found")  # Create a success alert
        time.sleep(2)  # Wait 2 seconds
        container.empty()  # Clear the success alert
        for i in range(len(mylist)):
            st.write(mylist[i])

Output

clear-alert-elements

You can find similar examples in the st.empty docs:

Happy Streamlit-ing!
Snehan :balloon:

1 Like

Thank you for this information :raised_hands::green_heart: