Skip to main content

A Streamlit component that provides toast notifications that persist across reruns

Project description

Streamlit-Notify

A Streamlit component that provides status elements that persist across reruns.

Installation

pip install streamlit-notify

Supported Status Elements

  • stn.toast: Toast notifications
  • stn.balloons: Balloon animations
  • stn.snow: Snow animations
  • stn.success: Success messages
  • stn.info: Info messages
  • stn.error: Error messages
  • stn.warning: Warning messages
  • stn.exception: Exception messages

How It Works

This package wraps standard Streamlit status element to enable queueing. Notifications are stored in Streamlit's session state and displayed during the next rerun cycle.

Basic Usage

import streamlit as st
import streamlit_notify as stn

# Display all queued notifications at the beginning of your app. This will also clear the list.
stn.notify_all()

# Add a notification that will be displayed on the next rerun
if st.button("Show Toast"):
    stn.toast("This is a toast message", icon="✅")
    st.rerun()

if st.button("Show Balloons"):
    stn.balloons()
    st.rerun()

if st.button("Show Success Message"):
    stn.success("Operation successful!")
    st.rerun()

Priority Support

# Higher priority notifications are displayed first
stn.info("High priority message", priority=10)
stn.info("Low priority message", priority=-5)

Passing User Data

# Higher priority notifications are displayed first
stn.info("High priority message", data="Hello World")
stn.info("Low priority message", data={'Hello': 'World'})

Getting all notifications

# returns a dict mapping notification types to list of notifications
notifications = stn.get_all_notifications()
error_notifications = notifications['error']
toast_notifications = notifications['toast']

# or you can get the notifications directly from the stn widget
error_notifications = stn.error.get_notifications()

Clearing notifications

# clears all notifications
stn.clear_all_notifications()

# clears notifications of only a specific type
stn.error.clear_notifications()

Checking if any notifications need to be shown

# check if any notifications exist across all types
stn.has_any_notifications()

# check only specific type
stn.error.has_notifications()

Manual Control

import streamlit as st
import streamlit_notify as stn

c1, c2 = st.columns(2)

with c1: # show only success messages in c1
    stn.success.notify()

with c2: # show only error messages in c2
    stn.error.notify()

if st.button("Show Error Message"):
    stn.error("Operation failed!")
    st.rerun()

if st.button("Show Success Message"):
    stn.success("Operation successful!")
    st.rerun()

Advanced Control

import streamlit as st
import streamlit_notify as stn

# loop over notifications and display those with valid data
for error_notification in stn.error.get_notifications():

    priority = error_notification.priority
    data = error_notification.data

    if data == True:
        error_notification.notify()

# will be shown
if st.button("Show Error Message1"):
    stn.error("Operation Error1!", data=True)
    st.rerun()

# will not be shown
if st.button("Show Error Message2"):
    stn.error("Operation Error2!", data=False)
    st.rerun()

StatusElementNotification Class

The StatusElementNotification class is the core data structure that represents a notification within the system:

Attributes:

  • base_widget (Callable): The original Streamlit widget function (e.g., st.success, st.error)
  • args (OrderedDict[str, Any]): Arguments to pass to the base widget when displayed
  • priority (int, optional): Priority of the notification, higher values displayed first (default: 0)
  • data (Any, optional): Custom data that can be attached to the notification (default: None)

Methods:

  • notify(): Displays the notification by calling the base widget with stored arguments
  • name (property): Returns the name of the base widget function

Example:

import streamlit as st
import streamlit_notify as stn

# Create custom StatusElementNotification
notification = stn.StatusElementNotification(
    base_widget=st.success,
    args={'body': 'My Message', 'icon': None},
    priority=1,
    data=None,
)
st.write(f"Displaying {notification.name} notification:")
notification.notify()

# Or create StatusElementNotification from a widget
notification = stn.error.create_notification(body='My Message', icon=None, priority=1, data=None)
st.write(f"Displaying {notification.name} notification:")
notification.notify()

RerunnableStatusElement Class

The RerunnableStatusElement class is a wrapper class that adds notification queueing functionality to standard Streamlit widgets. It inherits from NotificationQueue, extending it with widget-specific functionality:

Attributes:

  • base_widget (Callable): The original Streamlit widget function being wrapped
  • queue_name (str): Name of the queue (used as key in session state)
  • queue (StreamlitNotificationQueue): Underlying queue implementation

Methods:

  • __call__(*args, **kwargs): Creates and queues a notification when the widget is called
  • create_notification(*args, **kwargs): Creates a StatusElementNotification without adding it to the queue
  • notify(remove=True): Displays all queued notifications, optionally removing them from the queue
  • has_notifications(): Checks if there are any notifications in the queue
  • clear_notifications(): Clears all notifications from the queue
  • pop_notification(): Removes and returns the highest priority notification
  • get_notifications(): Gets all notifications in the queue
  • add_notification(notification): Adds a notification to the queue

Custom Notification Queues

The NotificationQueue class allows you to create custom notification queues for specialized use cases:

import streamlit as st
import streamlit_notify as stn

# Create a custom notification queue
custom_queue = stn.NotificationQueue(queue_name='my_notification_queue')

# Display all notifications in the custom queue
custom_queue.notify()

if st.button("Add Custom Notification"):
    # Create a notification using StatusElementNotification
    success_notification = stn.StatusElementNotification(
        base_widget=st.success,
        args={'body': 'My Message', 'icon': None},
        priority=1,
        data=None,
    )
    custom_queue.add_notification(success_notification)
    
    # You can add multiple notifications to the same queue
    error_notification = stn.StatusElementNotification(
        base_widget=st.error,
        args={'body': 'Error Message', 'icon': None},
        priority=2,
        data=None,
    )
    custom_queue.add_notification(error_notification)
    st.rerun()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

streamlit_notify-0.1.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

streamlit_notify-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file streamlit_notify-0.1.0.tar.gz.

File metadata

  • Download URL: streamlit_notify-0.1.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.22

File hashes

Hashes for streamlit_notify-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bcf39438eb8f0b17a2a4f3c246c62d83484bf9bf594491b9a4d195177d3acad4
MD5 7ab8d272327de9e783ce8d05ac22e23a
BLAKE2b-256 fb3bc494fd0fff63236922bb8294968efd79f4e1620ea051929d9f61ddaad58b

See more details on using hashes here.

File details

Details for the file streamlit_notify-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for streamlit_notify-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 18762f5aaba17e6b82d670b0eaa4a8448c279c6a7d47471f0b9f56c6721e28a1
MD5 7a584ac13febaaa1122106cc94fbd892
BLAKE2b-256 6d111c03e5325f0286dc26f0ba7e1fc04cd5edfa516f1d742fa63b48196bb1f6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page