{"version":"v1.0","client_state":{"DocsNavigation.default":{"fields":{"query":""},"persist":"memory","send_to_server":true},"ColorScheme.default":{"fields":{"mode":"system"},"persist":"local","send_to_server":true}},"endpoints":{"action":"/_bretzel/action","sse":"/_bretzel/sse","refetch":"/_bretzel/refetch"},"csrf":"175be7451706a71c143725dc960bb8da","page_id":"8fa53d2e2e24409cbde20dc92d6a22cf","address":""}

Cheat sheet

The whole of Bretzel condensed onto one page. Ctrl-F what you need.

What one types — the package's surface

Grouped by the need each name answers, not by its Python nature. A helper callable from a handler has exactly one access point: this one.
mount the application5
NameNatureWhat it does
BretzelclassThe Bretzel application instance.
BretzelConfigclassThe resolved, validated configuration of a :class:`Bretzel` app.
FeatureclassOne feature's contract. Declared once per feature package ::
PWAclassDeclare the metadata that makes an application installable.
PWAIconclassDescribe one icon in the web application manifest.
declare a route5
NameNatureWhat it does
downloadfunctionMark a function as the producer for a file served at ``GET path``.
error_pagedecoratorMark a function as the custom page for an HTTP status code.
layoutdecoratorMark a function as a layout. Free decorator — needs no app.
pagedecoratorMark ``fn`` as the handler for ``GET path`` (or other methods).
refreshabledecoratorWrap ``fn`` as a refreshable page region.
act from a handler7
NameNatureWhat it does
abortfunctionRaise an HTTP error from inside a request handler.
backgrounddecoratorMark ``fn`` as an after-response background task.
idempotentdecoratorMark a handler idempotent for ``ttl`` seconds.
push_urlfunctionChange the displayed URL without navigating or reloading the page.
redirectfunctionNavigate the browser to ``url`` after the current request.
refreshfunctionForce a refresh of a zone — the single imperative trigger.
reloadfunctionReload the page displayed by the browser after the current request.
act in the browser5
NameNatureWhat it does
copyfunctionCopy ``value`` to the clipboard when the action runs.
fullscreenfunctionEnter fullscreen mode when the action runs.
print_pagefunctionOpen the browser print dialog when the action runs.
sharefunctionOpen the native share sheet, or copy the URL when sharing is unavailable.
vibratefunctionVibrate the device when the action runs.
render5
NameNatureWhat it does
ColorSchemeclassFramework-owned color mode (singleton via the ``default`` key).
LanguageclassRead or select the language for the current request.
LiveConnectionclassExpose the state of the framework's real-time connection.
ScreenclassPer-request viewport shape read from the ``bz_screen`` cookie.
uivalueLowercased aliases for the shipped components.
declare state1
NameNatureWhat it does
statemoduleTyped state for Bretzel applications.
identify the current user2
NameNatureWhat it does
authmoduleAuthentication helpers for Bretzel applications.
oauthmoduleOAuth and OpenID Connect authentication providers.
handle an error3
NameNatureWhat it does
AuthRequiredErrorclassRaised when a ``user``-scoped state is resolved with no user.
BretzelErrorclassGeneric runtime error from Bretzel internals.
FeatureErrorclassRaised when a feature contract is malformed, or a set of features
package metadata1
NameNatureWhat it does
__version__value'0.1.0a2'

State — server (4 scopes)

from bretzel.state import PageState, SessionState, UserState, AppState
from bretzel.state import field, validator, computed

class Cart(SessionState):          # page / session / user / app
    items: list[dict] = field(default_factory=list)
    coupon: str = ""

    @validator("coupon")           # receives (self, value), RETURNS the value
    def _norm(self, v: str) -> str:
        return v.strip().upper()

    @computed                       # derived, auto-tracked
    def total(self) -> float:
        return sum(i["price"] for i in self.items)

State — client (3 persistence modes)

from bretzel.state import ClientState

class FilterUI(ClientState, persist="local"):
    #  "memory"  → throwaway (lost on reload)
    #  "session" → survives a reload, not a close
    #  "local"   → survives everything
    sort_by: str = "date"

Actions — server vs client

# Server: on_click = callable → hx-post, HMAC signed
def save() -> None:
    Cart().coupon = ""          # mutation → automatic re-render
ui.button("Save", on_click=save)

# Client: on_click = an expression (string) → no round trip
ui.button("Top", on_click="window.scrollTo(0, 0)")

# Imperative: the method RETURNS the client string
with ui.dialog() as d:
    ui.text("Are you sure?")
ui.button("Open", on_click=d.open())

# Binding: driving a ClientState
panel = PanelUI()
ui.button("Toggle", on_click=panel.open.toggle())

Reactivity — mutate → re-render

from bretzel import refreshable, ui

@refreshable(deps=[Cart])          # declarative: re-render when Cart changes
def cart_view() -> None:
    for it in ui.each(Cart().items, key="id"):
        ui.text(it["label"])

from bretzel import refresh
refresh(cart_view)                 # imperative, from a handler
# refresh on a broadcast=[…] zone → SSE fan-out to the other tabs

⚠️ Trap no. 1

Every file declaring a State MUST start with `from __future__ import annotations` (PEP 649) — otherwise the fields do not register, silently.
from __future__ import annotations   # ← FIRST, always
The other traps:Traps →