Handy alert_user() function

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()

Hi @info-rchitect

Thanks for sharing the code snippet, would you also like to share a link to the demo app?

Hi,

I canโ€™t share the app, it is proprietary, but I do have a quick demo video I can share. Not sure how to share videos here.

thx