Bases: Component
A value under its label, with an optional unit.
An instant — a datetime or a cg.Time — shows in the reader's time zone;
an unrecorded value (None or Missing) leaves the stat out.
cgui.Stat("Sessions", 12)
cgui.Stat("Coverage", 0.8, unit="ratio")
cgui.Stat("Captured", session.time)
Source code in capturegraph-ui/capturegraph_ui/typography/stat.py
| class Stat(Component):
"""A ``value`` under its ``label``, with an optional ``unit``.
An instant — a ``datetime`` or a ``cg.Time`` — shows in the reader's time zone;
an unrecorded value (``None`` or ``Missing``) leaves the stat out.
```python
cgui.Stat("Sessions", 12)
cgui.Stat("Coverage", 0.8, unit="ratio")
cgui.Stat("Captured", session.time)
```
"""
label: str
value: float | str | datetime | cg.Time | None
unit: str | None = None
def html(self, render: Render) -> str:
"""The value large, its unit beside it, the label beneath."""
if isinstance(self.value, float):
shown = text(f"{self.value:g}")
else:
shown = label_html(self.value)
parts = [shown]
if self.unit is not None:
parts.append(element("span", {"class": "cg-stat-unit"}, text(self.unit)))
return element(
"div",
{"class": "cg-stat"},
element("span", {"class": "cg-stat-value"}, *parts),
element("span", {"class": "cg-stat-label"}, text(self.label)),
)
|
html(render)
The value large, its unit beside it, the label beneath.
Source code in capturegraph-ui/capturegraph_ui/typography/stat.py
| def html(self, render: Render) -> str:
"""The value large, its unit beside it, the label beneath."""
if isinstance(self.value, float):
shown = text(f"{self.value:g}")
else:
shown = label_html(self.value)
parts = [shown]
if self.unit is not None:
parts.append(element("span", {"class": "cg-stat-unit"}, text(self.unit)))
return element(
"div",
{"class": "cg-stat"},
element("span", {"class": "cg-stat-value"}, *parts),
element("span", {"class": "cg-stat-label"}, text(self.label)),
)
|