tempest_fastapi_sdk.ui.css¶
CSS tipado: stylesheets, design tokens e a rota que os serve.
Rule
dataclass
¶
Rule(
selector: str,
style: Any | None = None,
layout: Layout | None = None,
declarations: Mapping[str, str] = dict(),
)
One CSS rule: a selector and the declarations it applies.
Attributes:
| Name | Type | Description |
|---|---|---|
selector |
str
|
Any CSS selector — |
style |
Any | None
|
A |
layout |
Layout | None
|
Treat the rule as a flex container of
that direction, adding |
declarations |
Mapping[str, str]
|
Raw CSS declarations, emitted after the style ones and therefore winning on conflicts. |
to_css
¶
Render the rule as CSS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indent
|
str
|
Indentation prepended to every line, used when the rule sits inside an at-rule block. |
''
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The rule as |
str
|
when it carries no declaration at all. |
class_names
¶
Return the class names mentioned in the selector.
Returns:
| Type | Description |
|---|---|
set[str]
|
set[str]: Every |
set[str]
|
dot. Empty for element- or attribute-only selectors. |
Media
dataclass
¶
Media(query: str, rules: Sequence[Rule] = ())
A group of rules wrapped in an at-rule (usually a media query).
Attributes:
| Name | Type | Description |
|---|---|---|
query |
str
|
The at-rule condition, without |
rules |
Sequence[Rule]
|
The rules the condition guards. |
min_width
classmethod
¶
max_width
classmethod
¶
dark
classmethod
¶
reduced_motion
classmethod
¶
to_css
¶
Render the at-rule and everything it guards.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
|
str
|
rule produced a declaration. |
class_names
¶
Return the class names mentioned by the nested rules.
Returns:
| Type | Description |
|---|---|
set[str]
|
set[str]: The union of every nested rule's class names. |
StyleSheet
dataclass
¶
StyleSheet(
rules: Sequence[Rule | Media] = (),
theme: Any | None = None,
reset: bool = True,
extra_css: str = "",
)
A whole stylesheet, written in typed Python.
Attributes:
| Name | Type | Description |
|---|---|---|
rules |
Sequence[Rule | Media]
|
The rules, in cascade order. |
theme |
ThemeTokens | None
|
Design tokens emitted as custom
properties before every rule. |
reset |
bool
|
Whether to prepend the minimal base reset
( |
extra_css |
str
|
Raw CSS appended at the end, for the rare thing
no rule expresses ( |
to_css
¶
Render the whole sheet.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Reset (when enabled), token block (when a theme is set), |
str
|
every rule in order, then |
|
str
|
by a blank line; empty blocks are dropped. |
class_names
¶
Return every class name the sheet defines.
Returns:
| Type | Description |
|---|---|
frozenset[str]
|
frozenset[str]: The union of the class names of all rules, |
frozenset[str]
|
used by :meth: |
cls
¶
Build a class attribute, rejecting names the sheet lacks.
Same output as the module-level :func:cls, with a lookup
against the sheet so a typo fails loudly at render time instead
of silently producing an unstyled element.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*names
|
str
|
Class names to apply, without the leading dot. |
()
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
dict[str, str]: |
Raises:
| Type | Description |
|---|---|
KeyError
|
When a name is not defined by any rule in the sheet. |
merge
¶
merge(other: StyleSheet) -> StyleSheet
Concatenate two sheets into one.
The receiver's rules come first, so other wins on equal
specificity — the normal cascade. The receiver's theme and reset
settings are kept unless it has none.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
StyleSheet
|
The sheet appended after this one. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
StyleSheet |
StyleSheet
|
A new sheet holding both rule lists. |
etag
¶
Return a strong ETag for the rendered sheet.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A quoted SHA-256 prefix of the CSS, stable for identical |
str
|
content so a conditional request can answer |
version
¶
Return a short content version of the rendered sheet.
Derived from the same SHA-256 as :meth:etag, so it depends on
the CSS text alone: two processes rendering the same rules get
the same value, and changing any declaration changes it.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The first 12 hex characters of the digest. |
url
¶
Return the content-versioned URL pages should link.
Pass the same path given to
:func:~tempest_fastapi_sdk.ui.css.make_css_router. The router
answers a request carrying the current version with a year-long
immutable cache policy, and a changed sheet yields a URL the
browser has never cached — so a deploy never leaves new HTML on
top of an old stylesheet.
Compute it once, next to the sheet, at import time: every call renders the whole sheet to hash it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Absolute route path the sheet is served from. |
'/static/app.css'
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When |
cls
¶
Build the class attribute mapping for a widget.
Widgets take an open attrs mapping; this turns a list of class
names into exactly the {"class": "..."} entry you would have
written, with the names joined and blanks dropped.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*names
|
str
|
Class names to apply, without the leading dot. |
()
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
dict[str, str]: |
dict[str, str]
|
non-blank name is given, so it is safe to splat unconditionally. |
ThemeTokens
dataclass
¶
ThemeTokens(
token_set: Any = None,
prefix: str = _DEFAULT_PREFIX,
dark_mode: bool = True,
font_family_body: str = SYSTEM_FONT_STACK,
_resolved: list[Any] = list(),
)
Design tokens rendered as CSS custom properties.
Attributes:
| Name | Type | Description |
|---|---|---|
token_set |
Any
|
The |
prefix |
str
|
Custom-property prefix, without dashes. |
dark_mode |
bool
|
Whether to emit the dark colour scheme blocks. |
font_family_body |
str
|
Value of |
var
¶
Return a var(...) reference to one token.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
group
|
str
|
Token group ( |
required |
name
|
str
|
Token name within the group ( |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
|
color
¶
Return a var(...) reference to a colour role.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role
|
str
|
A colour role of the scheme ( |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
|
space
¶
Return a var(...) reference to a spacing step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Spacing step ( |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
|
radius
¶
Return a var(...) reference to a shape radius.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Shape step ( |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
|
font_size
¶
Return a var(...) reference to a typography size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Typography token ( |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
|
font_family
¶
Return a var(...) reference to a font family.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Family token. Only |
'body'
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
|
breakpoint
¶
Return a breakpoint width in pixels.
Breakpoints are returned as numbers rather than custom
properties: CSS media queries cannot read var().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Breakpoint name ( |
required |
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
The breakpoint width in pixels. |
Raises:
| Type | Description |
|---|---|
KeyError
|
When the breakpoint name is unknown. |
to_css
¶
Render every token as CSS custom properties.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A |
str
|
scales, followed (when |
|
str
|
scheme under |
|
str
|
|
make_css_router
¶
make_css_router(
sheet: StyleSheet,
*,
path: str = "/static/app.css",
cache_control: str = _DEFAULT_CACHE_CONTROL,
versioned_cache_control: str = _DEFAULT_VERSIONED_CACHE_CONTROL,
) -> APIRouter
Build a router serving one stylesheet at a fixed path.
The CSS is rendered eagerly, at router-construction time, so no
request pays the cost of walking the rules. Link pages at
sheet.url(path) — the content-versioned URL — so the browser
keeps the sheet cached and fetches a new one exactly when a deploy
changes it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sheet
|
StyleSheet
|
The sheet to serve. |
required |
path
|
str
|
Absolute route path, including the leading slash.
Pass the same value to :meth: |
'/static/app.css'
|
cache_control
|
str
|
|
_DEFAULT_CACHE_CONTROL
|
versioned_cache_control
|
str
|
|
_DEFAULT_VERSIONED_CACHE_CONTROL
|
Returns:
| Name | Type | Description |
|---|---|---|
APIRouter |
APIRouter
|
A router exposing |
APIRouter
|
when the request's |
Raises:
| Type | Description |
|---|---|
ValueError
|
When |
Example
from fastapi import FastAPI
from tempest_fastapi_sdk.ui.css import Rule, StyleSheet, make_css_router
app: FastAPI = FastAPI()
sheet: StyleSheet = StyleSheet(
rules=[Rule(".card", declarations={"padding": "16px"})],
)
CSS_URL: str = sheet.url("/static/app.css")
app.include_router(make_css_router(sheet, path="/static/app.css"))
css_response
¶
css_response(
sheet: StyleSheet,
*,
cache_control: str = _DEFAULT_CACHE_CONTROL,
status_code: int = 200,
) -> Response
Render a stylesheet into a text/css response.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sheet
|
StyleSheet
|
The sheet to render. |
required |
cache_control
|
str
|
Value of the |
_DEFAULT_CACHE_CONTROL
|
status_code
|
int
|
HTTP status code. Defaults to |
200
|
Returns:
| Name | Type | Description |
|---|---|---|
Response |
Response
|
The rendered CSS with an |
Response
|
content and the given cache policy. |
stylesheet_links
¶
Build the <link rel="stylesheet"> tags for a document head.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*hrefs
|
str
|
Stylesheet URLs, in load order. |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The concatenated link tags, with |
str
|
so a crafted path cannot break out of the attribute. |