Toaster
Session-backed toast notifications with HTMX injection for FastHTML apps
Queue toast notifications in the user session with add_toast and inject them into responses automatically via setup_toasts.
app, rt = fast_app()
setup_toasts(app)
@rt
def save(sess):
add_toast(sess, 'Saved', 'success')
return P('Done')
setup_toasts
def setup_toasts(
app, # FastHTML app instance
duration:int=5000, # Default auto-remove delay in ms
):
Store duration on app.state.toast_duration, append toast CSS/JS to app.hdrs, and register toast_after on app.after.
add_toast
def add_toast(
sess, # Session mapping
message:str, # Toast text
typ:str='info', # One of `'info'`, `'success'`, `'warning'`, `'error'`
dismiss:bool=False, # Include a dismiss `'x'` button?
):
Append (message, typ, dismiss) to sess['toasts'].
Toast
def Toast(
message:str, # Toast text
typ:str='info', # Style: `'info'`, `'success'`, `'warning'`, or `'error'`
dismiss:bool=False, # Include a dismiss `'x'` button?
duration:int=5000, # Auto-remove delay in ms
):
Build a .fh-toast.fh-toast-{typ} Div that removes itself after duration ms.
Helpers
toast_after
def toast_after(
resp, req, sess
):
FastHTML after hook draining queued session toasts into req.injects for empty, tuple, FT, or FtResponse results (uses Partial on HTMX 4 fragments).
render_toasts
def render_toasts(
sess
):
Drain sess['toasts'] into an out-of-band Div targeting beforeend:#fh-toast-container.