Here is a handy function I use to get timed alert messages to app users.
import streamlit as st
from time import sleep
MSG_ICONS = {
"success": "✅",
"info": "ℹ️",
"warning": "⚠️",
"error": "🚨",
"exception": "",
}
def alert_user(msg, level="info", hold_for=None):
messenger=st.empty()
func = None
sleep_time = None
if level not in MSG_ICONS:
e = RuntimeError(f"Could not alert the user using level '{level}', this is a bug!")
st.exception(e)
return
try:
func = getattr(messenger, level)
except:
e = RuntimeError(f"Could not alert the user using level '{level}', this is a bug!")
st.exception(e)
return
icon = MSG_ICONS[level]
if icon:
func(msg, icon=icon)
else:
func(msg)
if hold_for:
sleep_time = hold_for
else:
sleep_time = 2 if level in ["info", "success"] else 3
sleep(sleep_time)
messenger.empty()