tempestweb.html¶
The static HTML renderer — a leaf renderer that produces markup from the same widget tree, with no browser involved. It is what backs SSR and build-time page generation.
Guide with examples: Static SSR.
tempestweb.html ¶
tempestweb.html — static server-side HTML renderer (a leaf renderer).
Turns a typed :class:~tempest_core.widgets.base.Widget tree into a static HTML
string, reusing :func:tempest_core.build. It is a sibling of the DOM-JS client
(client/dom.js): the same declarative tree renders to interactive DOM in the
browser and to plain HTML on the server ("one tree, N renderers").
- :func:
render_to_html— a widget tree to an HTML fragment. - :func:
render_document— a widget tree to a full<!doctype html>page. - :func:
style_to_css— a Style dump to a CSS declaration body (a Python port ofclient/style.js). - :func:
theme_css— an app's :class:~tempest_core.Themeas the--tw-*custom properties the base stylesheet reads (light plus dark). - :func:
escape_text/ :func:escape_attr— the HTML-escaping choke points.
See docs/ssr.md for the tutorial.
ROLE_BY_VARIABLE
module-attribute
¶
ROLE_BY_VARIABLE: dict[str, str] = {'--tw-primary': 'primary', '--tw-on-primary': 'on_primary', '--tw-primary-container': 'primary_container', '--tw-on-primary-container': 'on_primary_container', '--tw-secondary-container': 'secondary_container', '--tw-on-secondary-container': 'on_secondary_container', '--tw-surface': 'surface', '--tw-on-surface': 'on_surface', '--tw-on-surface-variant': 'on_surface_variant', '--tw-outline': 'outline', '--tw-error': 'error', '--tw-success': 'success', '--tw-warning': 'warning', '--tw-info': 'info', '--tw-neutral': 'on_surface_variant'}
Which Material 3 role each variable the base sheet reads comes from.
--tw-neutral has no role of its own: it tints the "nothing is claimed
here" state of an indicator, which is the same job on_surface_variant
does for text.
style_to_css ¶
Translate a Style dump into a CSS string (declarations joined by "; ").
A faithful port of styleToCss in client/style.js. Field order follows
the source model (flex, box model, paint, typography, dimensions, transition)
so the emitted declarations are stable and identical to the client's.
None/absent fields are skipped entirely — they mean "unset" and let the
browser default apply.
A Row/Column (and their lazy variants) becomes a flex container by
type even with no explicit direction in the style, so gap/justify/
align are never silently inert.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
style
|
dict[str, Any] | None
|
A Style dump ( |
required |
widget_type
|
str | None
|
The widget type ( |
None
|
Returns:
| Type | Description |
|---|---|
str
|
A |
Source code in tempestweb/html/css.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | |
escape_attr ¶
Escape a value for use as a double-quoted HTML attribute value.
Escapes &, <, > and both quote characters, so the result can be
safely placed inside attr="..." without breaking out of the attribute.
None becomes the empty string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
Any value to render as an attribute value; coerced with
:func: |
required |
Returns:
| Type | Description |
|---|---|
str
|
The escaped attribute value, or |
Source code in tempestweb/html/escape.py
escape_text ¶
Escape a value for use as HTML text content.
Escapes &, < and > (but not quotes — they are safe in text
nodes). None becomes the empty string.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
object
|
Any value to render as text; coerced with :func: |
required |
Returns:
| Type | Description |
|---|---|
str
|
The escaped text, or |
Source code in tempestweb/html/escape.py
render_document ¶
render_document(widget: Widget, *, title: str, lang: str = 'pt-BR', head: str = '', htmx: bool = False, css_reset: bool = True) -> str
Render a widget tree to a complete, self-contained HTML document.
Wraps :func:render_to_html in a <!doctype html> shell with a charset
meta, an escaped <title>, an optional CSS reset, any extra head
markup, and — when htmx is set — the htmx runtime script tag.
htmx delivery
With htmx=True the document currently links htmx from a public CDN
(unpkg.com). A later cycle's SDK will serve htmx locally; the URL is
kept parameter-driven (a plain string in the output) so that change is a
one-line swap and never a hard dependency here.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
widget
|
Widget
|
The typed widget tree to render as the document body. |
required |
title
|
str
|
The page title (escaped into |
required |
lang
|
str
|
The document language for |
'pt-BR'
|
head
|
str
|
Extra raw markup to inject into |
''
|
htmx
|
bool
|
When |
False
|
css_reset
|
bool
|
When |
True
|
Returns:
| Type | Description |
|---|---|
str
|
A complete HTML document string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any widget carries an |
Source code in tempestweb/html/renderer.py
render_to_html ¶
Render a widget tree to a static HTML fragment string.
Builds the widget with :func:tempest_core.build and walks the resulting IR
into HTML. The output is a fragment (no <html>/<body> wrapper); use
:func:render_document for a full page.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
widget
|
Widget
|
The typed widget tree to render. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The static HTML fragment. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If any widget carries an |
Source code in tempestweb/html/renderer.py
theme_css ¶
Render a theme as the CSS custom properties the client reads.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
theme
|
Theme
|
The app's theme, usually from
:meth: |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A CSS block for the document head — |
str
|
|
|
str
|
|
|
str
|
alone must not flip the page. |
Example
```python from tempest_core import Theme, ThemeMode from tempest_core import Color from tempestweb.html import theme_css
def head() -> str: """Build the head markup that rebrands every widget.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
A style element carrying the app's palette. |
"""
theme = Theme.from_seed(Color(r=39, g=58, b=79), mode=ThemeMode.SYSTEM)
return f"<style>{theme_css(theme)}</style>"
```