Skip to content

Toast

ui.toast draws a notification with a title, a message, actions and a close button; kind is "info", "success", "warning" or "error". ui.toasts(base, items) stacks them in the bottom-right corner over your view.

ui.toast · Category: Overlays · Full reference →

ToastToast
  • To confirm something that happened in the background (saved, copied, connected).
  • Messages that need an answer belong in a Dialog.
import pyzed as pz
from pyzed import ui
import threading
class Notes(pz.App):
def __init__(self):
self.messages = []
def notify(self, title):
self.messages.append(title)
threading.Timer(3, pz.post, args=(("expire", title),)).start() # hide it after 3 s
def update(self, message):
if message[0] == "expire":
self.messages.remove(message[1])
def view(self):
items = [ui.toast(m, kind="success", on_dismiss=lambda m=m: self.messages.remove(m))
for m in self.messages]
return ui.toasts(self.main_view(), items)
Toast: kind="info"Toast: kind="info"
kind="info"
Code
ui.toast("Update available", "Pyzed 0.5 is on PyPI.")
Toast: kind="success"Toast: kind="success"
kind="success"
Code
ui.toast("Saved", "main.py was saved.", kind="success")
Toast: kind="warning"Toast: kind="warning"
kind="warning"
Code
ui.toast("Low disk space", "2 GB left.", kind="warning")
Toast: kind="error"Toast: kind="error"
kind="error"
Code
ui.toast("Could not connect", "Check your network.", kind="error")
Toast: Title onlyToast: Title only
Title only
Code
ui.toast("Copied to clipboard", kind="success")
Toast: actions=, on_dismiss=Toast: actions=, on_dismiss=
actions=, on_dismiss=
Code
ui.toast("Update available", "Restart to install it.",
actions=[ui.button("Restart", on_restart, size="sm")], on_dismiss=on_dismiss)
Toast: icon="music"Toast: icon="music"
icon="music"
Code
ui.toast("Now playing", "Lo-fi beats", icon="music", width=280)
Toast: progress=0.6Toast: progress=0.6
progress=0.6
Code
ui.toast("Uploading", "notes.md", kind="info", progress=0.6, on_dismiss=on_close)
Toast: on_press=Toast: on_press=
on_press=
Code
ui.toast("Build failed", "Click to see the log", kind="error", on_press=on_press)

Toast has 2 events of its own, listed below. Like every widget it also has the 18 common events (click, double_click, right_click, mouse_enter, drag, key_down, resize…), attached with .on(...): see Events and Events of every component.

Event Handler gets When
on_dismiss () Message or callable sent on close.
on_press () Makes the card clickable.

Handler gets is what Pyzed passes to your callable: () means nothing. Any handler can also be a message (a value that is not callable), which goes to update().

The same events written three ways. Imperative: methods of the app change self. Declarative: the handlers produce messages and one update() decides. Functional: a pure function draws an immutable state and asks for changes with set_state(...). See Imperative, declarative and functional.

class Notes(pz.App):
def __init__(self):
self.messages = ["Saved"]
self.opened = 0
def view(self):
items = [ui.toast(m, kind="success",
on_dismiss=lambda m=m: self.messages.remove(m), # its ✕
on_press=lambda: setattr(self, "opened", self.opened + 1)) # the card
for m in self.messages]
return ui.toasts(main_view(), items)
toast(
title,
message=None,
*,
kind='info',
icon=None,
on_dismiss=None,
actions=(),
width=340,
progress=None,
on_press=None,
theme=None,
)

A notification.

Property Default Description
title required Title.
message None Secondary text.
kind "info" "info", "success", "warning" or "error".
icon None Name of a Lucide icon (pyzed icons lists them).
actions () Extra widgets (usually buttons) shown as actions.
width 340 Width: pixels, "fill", "shrink" or "fill:N" (proportion).
progress None 0 to 1: a thin bar at the bottom (time left, a download).
theme None Theme to use instead of the current one (pyzed.theme.current()).

Events: on_dismiss, on_press (events).

Full reference →

toasts(base, items, *, spacing=8)

Shows items (usually toast(...)) stacked in the bottom-right corner over base.

Property Default Description
base required The UI underneath.
items required List of toasts.
spacing 8 Space between children, in pixels.

Full reference →