Skip to content

Message

oldmangan edited this page Oct 6, 2026 · 2 revisions

wapyt.message shows toasts and alert, confirm and prompt dialogs. Use it instead of a hand-rolled toast or the browser's window.confirm(), which can't be styled, blocks the whole page, and is suppressed in some embedded browsers.

from wapyt import message

message.toast("Saved.", kind="success")

async def drop_collection(self, db, coll):
    if not await message.confirm(
        f"Drop {db}.{coll} and all its documents and indexes?\n\nThis cannot be undone.",
        title="Drop collection", ok_text="Drop", danger=True,
    ):
        return
    await Store().drop_async(db, coll)
    message.toast(f"Dropped {coll}.", kind="success")

Like File Transfer, it's a module of functions rather than a widget you mount: toasts and dialogs live on <body>, outside any layout.

Every string is shown as text, never markup, so server and model output can be passed straight in. A blank line (\n\n) starts a new paragraph.

Toasts

message.toast(text, kind="info", timeout_ms=4000) -> id

Option
kind "info", "success", "warning" or "error": a coloured accent and icon
timeout_ms How long it stays; 0 keeps it until dismissed
  • Toasts stack at the bottom centre, at most four at once; the oldest makes way.
  • Hovering pauses the timer, and each has a close button.
  • Errors are announced to screen readers immediately (role="alert"); the others wait their turn (role="status").
  • message.dismiss(id) removes one early; message.dismiss_all() clears them, on sign-out say.

Dialogs

The dialogs are coroutines: await them from an async handler, scheduled with asyncio.ensure_future (see Events and Async).

Function Returns
await message.alert(text, title=None, ok_text="OK") None once acknowledged
await message.confirm(text, title=None, ok_text="OK", cancel_text="Cancel", danger=False) True only for OK
await message.prompt(text, title=None, value="", placeholder=None, ok_text="OK", cancel_text="Cancel", password=False) the text, "" for an empty answer, or None when cancelled
  • Cancel, Escape and a click outside never confirm. For an alert they acknowledge it.
  • danger=True styles OK as destructive and puts the initial focus on Cancel, so pressing Enter can't destroy anything.
  • In a prompt, the text starts selected and Enter submits.
  • password=True masks a prompt's input and sets autocomplete="new-password", so the browser doesn't fill a saved password into a reset field.
  • Tab stays inside the dialog, and focus returns to whatever had it before. Dialogs can stack; only the topmost one handles keys.
  • Validate a prompt's answer yourself, and again on the server if it matters.

Look

Toasts and dialogs follow set_theme() (dark mode applies even to a dialog that's already open) and use --wapyt-font-family. See Theming and Icons.

Moving off window.confirm()

# before -- blocks the page, unstyled
if not js.confirm(f"Delete the connection “{name}”?\n\nThe server is not touched."):
    return

# after -- inside an async method
if not await message.confirm(f"Delete the connection “{name}”?\n\nThe server is not touched.",
                             ok_text="Delete", danger=True):
    return

The method making the call has to become async and be scheduled, since a dialog waits for the person instead of freezing the page.

Clone this wiki locally