Writer Reference ¶
The Writer (w) is your interface for sending HTTP responses. It handles one-shot responses, SSE streaming, and connection lifecycle.
One-Shot Responses ¶
Methods that send a full response and complete the connection.
| Method | Description | Example |
|---|---|---|
w.html(el) |
Send HTML element or string | w.html(Div("Hello")) |
w.json(data) |
Send JSON response | w.json({"ok": True}) |
w.text(text) |
Send plain text | w.text("OK") |
w.redirect(url) |
HTTP redirect (307 default) | w.redirect("/login") |
w.empty(status) |
Empty response (204 default) | w.empty() |
SSE & Datastar Streaming ¶
Methods for real-time DOM patches and signal syncing.
| Method | Description | Example |
|---|---|---|
w.patch(el) |
Update DOM element by ID | w.patch(Div({"id": "x"}, "New")) |
w.sync(data) |
Update client-side signals | w.sync({"count": 5}) |
w.navigate(url) |
Client-side navigation | w.navigate("/home") |
w.execute(js) |
Execute JavaScript | w.execute("alert('Hi')") |
w.remove(sel) |
Remove element from DOM | w.remove("#error") |
Connection Lifecycle: w.alive() ¶
w.alive() is an async iterator that exits cleanly when the client disconnects or the server shuts down.
# 1. Wrap a subscription
async for msg in w.alive(relay.subscribe("updates")):
w.patch(render(msg))
# 2. Infinite loop
async for _ in w.alive():
msg = await queue.get()
w.patch(render(msg))
# 3. Context manager (one-shot protection)
async with w.alive():
await slow_process()
w.patch(Div("Done"))
Cookies & Headers ¶
# Cookies
w.cookie("session", token, httponly=True, secure=True)
w.delete_cookie("session")
# Headers & Status
w.status(201).header("X-Custom", "val").json(data)
Compression ¶
Stario automatically uses zstd, brotli, or gzip based on the client's Accept-Encoding. No configuration needed for standard use.
