Event state
An event handler often needs values from the component it was called from: which project it shows, which page of results, what the user typed. It does not receive the component's original inputs. Keep those values in State: Citry sends them to the browser with the rendered component, and the browser sends them back with the next call.
Start with Server events if you have not called a Python handler from a component yet.
State and state_data()
When the component's inputs are already the values you need, make State inherit from Kwargs:
class Kwargs:
count: int = 0
class State(Kwargs):
pass
A handler receives them through its state parameter, and any change it makes is kept for the next call.
Keep State small: ids, filters, page numbers, flags such as "editing". The values must be JSON-serializable, or, for a Pydantic State, values Pydantic writes as JSON and reads back unchanged. When the inputs hold larger objects, declare a smaller State and fill it from the inputs with state_data():
class ProjectPanel(Component):
citry = citry_app
class Kwargs:
project: Project
show_archived: bool = False
class State:
project_id: int
show_archived: bool = False
def state_data(self, kwargs, slots):
return {
"project_id": kwargs.project.id,
"show_archived": kwargs.show_archived,
}
The handler then loads the project again from project_id, as the next section shows.
Pass every input
When a handler returns a component, Citry renders it from scratch, using only the inputs you pass. The original kwargs and slot fills are not kept.
The natural first attempt is to read the original inputs from self. That fails, because self in a handler is not the rendered component:
class Events:
def refresh(self):
# Wrong: self has no kwargs from the original render.
return ProjectPanel(project=self.kwargs.project)
Instead, keep the id in State, load the record for the current user, and pass every input explicitly:
class Events:
def refresh(self, state, request):
project = load_project_for_user(
project_id=state.project_id,
user=current_user(request),
)
return ProjectPanel(
project=project,
show_archived=state.show_archived,
)
The rule: if the new render needs a value, the handler must get it and pass it in.
:c-<field> on inputs
A :c-<field> attribute shows a State field in a form control. Give it a handler name, and edits update the field and call the handler. This live search sends the query 300 ms after the user stops typing:
class LiveSearch(Component):
citry = citry_app
class Kwargs:
query: str = ""
class State(Kwargs):
pass
class Events:
def refresh(self, state):
return LiveSearch(query=state.query)
def template_data(self, kwargs, slots):
if kwargs.query:
results = find_products(kwargs.query)
else:
results = []
return {"results": results}
template = """
<div>
<input
type="search"
placeholder="Search..."
:c-query.debounce.300ms="refresh"
>
<ul :class="{ searching: $loading() }">
<c-for each="item in results">
<li>{{ item.name }}</li>
</c-for>
</ul>
</div>
"""
refresh renders a new LiveSearch for the updated query. Vue updates the list in place, so the input keeps its focus and cursor position.
Bind events in templates lists every control you can bind and the Python type each one sends.
_public and _model
By default, browser code can read every State field and change it, through the $state object in templates or a :c-* binding. Two settings narrow that:
class State:
project_id: int
page: int = 1
can_delete: bool = False
_public = ("page", "can_delete")
_model = ("page",)
_publiclists the fields browser code can read._modellists the public fields browser code can change.
The browser can only replace a whole field. Changing an item in place, such as $state.tags.push(tag), throws an error. Copy the value, change the copy, and assign it back.
These settings limit what your templates can do. They do not hide anything. The signed State is visible to anyone who opens the page, and a field the browser can change may arrive with any value. Validate it in the handler like any other user input. See Security.
Choose where data lives
| Data | Put it in | How long it lives |
|---|---|---|
Small values a later server call needs, or values bound with :c-* | State | Kept across calls. |
| Larger or computed values the browser code reads | js_data() | Recomputed on each render. |
| Browser-only UI state, such as whether a panel is open | Vue data() | Kept by the Vue component in the browser. |
Each server render replaces the js_data() values, so do not use them for values that must survive a call. The server cannot read Vue data(), so a panel hidden with v-show shows open until Vue starts. Moving the value from data() to js_data() makes the page load closed, but each server render then resets it, so a panel the user opened closes on the next re-render. Before Vue starts covers the other bindings. See Where each value goes for how Python and browser code share a component.
The browser gets State changes even without a re-render
When a handler changes State, the response always carries the new values of the fields the browser can read (see $state). This holds when the handler returns None, returns data, or re-renders only part of the component.
Pydantic State
To have Pydantic check State, so a handler that sets a field to the wrong type fails at once, write State as a Pydantic v2 or v1 model:
from pydantic import BaseModel
class Filters(Component):
citry = citry_app
class State(BaseModel):
query: str = ""
page: int = 1
_public = ("query",)
A plain class State: becomes a model too when you pass a Pydantic model as Citry(model_base=...). A component from a library you install keeps the State its author wrote. Settings such as _public and _storage work as on any State; other underscore names are reserved for them.
A model State can also hold nested models, dates, and other values Pydantic writes as JSON and reads back unchanged. Browser code reads them as JSON, so a date is a string such as "2026-01-02", and the next call turns them back into dates and models.
Pydantic checks the values at three points, so your validators run at each one:
- When a call restores State. If the values in the page no longer fit the model, for example because you changed a field's type and deployed, the call fails with
stale_state(409) and Citry asks the user to reload the page (details). - When the browser writes a field. The call fails with
invalid_args(422) before the handler runs, with Pydantic's message for each field. - After a handler changes State. If a new value does not fit, such as
state.page = "two"or a wrong-typed item appended to a list, the call fails withhandler_error(500), the page keeps its old State, and the server log names the component and the handler. Anything else the handler did, such as a database write, stays done.
With validate_assignment=True, a wrong assignment raises at that line in the handler, and the call fails with handler_error.
Citry raises ValueError when the component class is defined if State, or a model nested in it, uses one of these, because it could not store and restore State with them:
- a frozen model (
frozen=True, orallow_mutation = Falsein v1), or a frozen field; - a field left out of the dump (
exclude=Trueorexclude_if), or a serializer (model_serializer,field_serializer,PlainSerializer,WrapSerializer); - a secret type such as
SecretStr, which Pydantic writes masked; - a field alias in v1 or in Pydantic older than 2.11, unless the model sets
allow_population_by_field_name = True(v1) orpopulate_by_name=True.
Citry stores and restores State by field name, so a field alias only affects input you pass the model yourself.
Pydantic decides what a browser write may hold. A field the browser may write (_model) with a type such as ImportString would import whatever module the browser names, so keep such types out of writable fields.
Subclass State
A subclass uses its parent's State until it declares its own. To add fields, name the parent's class as a base. The subclass keeps the parent's fields and settings, such as _public and _model:
class SortedProjectPanel(ProjectPanel):
class State(ProjectPanel.State):
sort: str = "name"
ProjectPanel is the component from State and state_data(). Because the subclass keeps any _public its parent sets, list a new field there too when browser code should read it.
A plain class State: in the subclass replaces the parent's fields and settings. It starts from the defaults: browser code can read and change every field, the values are stored, signed, in the page, and the size limit (_max_bytes) is the default. Citry warns when the class is defined if the new State drops a parent field, or does not set a _public, _model, or _max_age that the parent sets. _max_age is how long the values stay valid.
If the parent's State keeps its values on the server with _storage = "server" (see Security), a plain class State: that does not set _storage raises ValueError when the class is defined, because its values would be stored in the page, where anyone who opens the page can read them. Name the parent's class, or set _storage yourself.
Subclass components covers the same rule for Kwargs and the other nested classes.