Server reactivity
Once a handler has changed the state (cf.Server actions), here is what re-renders on screen, and how to control it.
The automatic re-render
A `@refreshable` zone follows the states it reads: every `MyState()` it obtains while rendering is recorded. When a handler mutates one of them, the zone is re-rendered automatically — no list to write, no refresh to call.@refreshable
def cart_summary() -> None:
ui.text(f"{len(Cart().items)} items")
def add_to_cart(product_id: str) -> None:
Cart().items.append(product_id)
# cart_summary re-renders: it read Cart
Mutation = re-render
At the end of an action, the server compares the state before and after — including in-place modifications (`items.append(...)`, `d[k] = v`). Every zone that read a changed state is re-rendered and sent back to the browser.Conversely, if a handler changes no state that a zone read, no zone is re-rendered — the round trip brings back nothing to refresh.What is not a state — Topic
A database write touches no state, so no zone notices it. Declare a `Topic` and attach the functions that touch the data: every zone calling a `@reads` function redraws once a `@writes` function has returned without an error. Bretzel never looks at the storage, so a database, a file or an API all work. `refresh(topic)` redraws them when nothing was written. Outside a zone, `@reads` does nothing.from bretzel.state import Topic, reads, writes
CONTACTS = Topic("contacts")
@reads(CONTACTS)
def search_contacts(needle: str) -> list[dict]:
return query("SELECT ...")
@writes(CONTACTS)
def rename(contact_id: int, name: str) -> None:
execute("UPDATE ...")
Triggering by hand — refresh()
`refresh(zone)` forces a zone's re-render. Useful when the change does not come from a state the zone reads, or from somewhere that is not an action handler. It can also be addressed by its `name`.from bretzel import refresh
def reload_prices() -> None:
fetch_latest()
refresh(price_table) # by the zone
Realtime between tabs
A zone is redrawn in every open tab showing it, as far as the scope of what it read allows. Nothing to declare.• `PageState` → this tab only, in the action's response: one round trip, one swap.• `SessionState` → the other tabs of this session; `UserState` → the other tabs of this user.• `AppState` and `Topic` → every tab. An SSE signal, then a refetch: two round trips, but the tab that did nothing follows.
class Queue(AppState):
people: list[str] = field(default_factory=list)
@refreshable
def waiting_room() -> None: # every tab showing it follows
for name in Queue().people:
ui.text(name)
What crosses is only a SIGNAL: every client refetches in its own context, no data passes from one client to another.What next
The reactivity that lives in the browser, with no round trip:Client reactivity →