component
component
#
Component — one node of a page; a subclass's annotated fields are its props.
- Declaring a subclass freezes it as a dataclass over its annotations; building
an instance checks every prop against its annotation (
props). - A page renders by asking each component for its HTML (
html), handing it the page's render context to stage files and resolve links through. - A component given a
Missingfor any prop is absent: its props go unchecked and it renders as nothing (absent). AMissingitem inside a list prop is left out instead (check_prop). - Under a
PageBuilder, a freshly built component attaches itself to the innermost open stack (Build.attach), so the body of a page function reads as the page, top to bottom.
Component
#
Base of every page component; subclass it, annotate the props, implement html.
A prop is a JSON scalar, a list or dict of them, a cg value, or another
component; each is checked against its annotation when the component is
built, and a path given for a file prop becomes that file scalar. A
component given Missing for any prop is absent and renders as nothing,
so cg.ui.Stat("Wind", session.weather.wind_speed_mps) simply leaves
the page when no weather was recorded; a Missing item in a list prop
is left out.
class Badge(cg.ui.Component):
text: str
def html(self, render: cg.ui.Render) -> str:
return cg.ui.element("span", {"class": "badge"}, cg.ui.text(self.text))
Source code in capturegraph-lib/capturegraph/ui/component.py
absent
property
#
Whether any prop is Missing, in which case the component renders as nothing.
__init_subclass__(**kwargs)
#
Freeze cls over its props and make its html render nothing when absent.
Source code in capturegraph-lib/capturegraph/ui/component.py
__post_init__()
#
Check every prop against its annotation, coercing where a scalar admits it.
Raises:
| Type | Description |
|---|---|
TypeError
|
If a prop does not fit its annotation; the message names the prop. |
Source code in capturegraph-lib/capturegraph/ui/component.py
html(render)
#
This component as HTML, its files staged and its links resolved through render.
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
Always, on the base class. |