Ir para o conteúdo

tempest_fastapi_sdk.ui.components

Componentes: as peças reutilizáveis de que as páginas são feitas.

« Web, UI e utilitários


Card

Bases: Component

A titled block of content.

Renders a <section> with an optional heading, the body content and an optional footer.

Attributes:

Name Type Description
title str

The heading text. Empty renders no heading.

children list[Widget]

The body content.

footer Widget | None

Optional footer content.

heading_tag str

Which heading element to use — pick the one that fits the page outline (h2 inside a page with an h1, h3 inside a section).

classes ComponentClasses

Class names to apply.

Example
from tempest_core import Text

from tempest_fastapi_sdk.ui.components import Card

card = Card(title="Vendas", children=[Text(content="R$ 12.400")])

render

render() -> Widget

Compose the card.

Returns:

Name Type Description
Widget Widget

A <section> holding the heading, body and

Widget

footer that were provided.

Alert

Bases: Component

A short message, coloured by severity.

warning and error render with role="alert" so assistive technology announces them immediately; the quieter variants use role="status".

Attributes:

Name Type Description
message str

The message text.

variant AlertVariant

The severity.

title str

Optional heading above the message.

classes ComponentClasses

Class names to apply.

Example
from tempest_fastapi_sdk.ui.components import Alert

alert = Alert(message="Conta criada com sucesso.", variant="success")

render

render() -> Widget

Compose the alert.

Returns:

Name Type Description
Widget Widget

A <div> carrying the variant modifier class and

Widget

the ARIA role matching its severity.

DataTable

Bases: Component

A table rendered from a list of schemas.

Attributes:

Name Type Description
rows list[Any]

The records — Pydantic models or mappings.

columns list[str | TableColumn]

Columns, in order — a field name for a text cell, or a :class:TableColumn for a custom renderer, alignment or class. Empty derives them from row_schema when given, otherwise from the first row.

row_schema Any | None

The Pydantic model class describing the rows. Supplying it means the header still renders when the list is empty, and column labels come from each field's title.

headers dict[str, str]

Header text overrides, per column.

caption str

Optional <caption> text.

empty_text str

Text of the single row shown when there is nothing to list.

bool_labels tuple[str, str]

Text for True and False cells.

none_text str

Text for None cells, and for a renderer that returns None.

classes ComponentClasses

Class names to apply.

Example
from pydantic import BaseModel

from tempest_fastapi_sdk.ui.components import DataTable


class UserResponseSchema(BaseModel):
    name: str
    email: str


table = DataTable(
    rows=[UserResponseSchema(name="Ana", email="ana@example.com")],
    row_schema=UserResponseSchema,
)

column_specs

column_specs() -> list[TableColumn]

Return the columns actually rendered, as :class:TableColumn.

Returns:

Type Description
list[TableColumn]

list[TableColumn]: The explicit columns (a bare name

list[TableColumn]

becomes a default text column) when given, otherwise one text

list[TableColumn]

column per schema field, otherwise per key of the first row.

list[TableColumn]

Empty when none of the three is available.

resolved_columns

resolved_columns() -> list[str]

Return the names of the columns actually rendered.

Returns:

Type Description
list[str]

list[str]: The name of every entry of :meth:column_specs,

list[str]

in order.

header_text

header_text(column: str) -> str

Return the header label of a column.

Parameters:

Name Type Description Default
column str

The column name.

required

Returns:

Name Type Description
str str

The override when given, else the schema field's

str

title, else the humanized column name.

cell_text

cell_text(value: object) -> str

Format one cell value as text.

Parameters:

Name Type Description Default
value object

The raw value read from the row.

required

Returns:

Name Type Description
str str

The rendered cell text. None becomes

str

attr:none_text, booleans use :attr:bool_labels, dates

str

render ISO, sequences join with ", ", and a nested model

str

renders its own field values.

render

render() -> Widget

Compose the table.

The <table> sits inside a <div> carrying :attr:ComponentClasses.table_scroll, which scrolls it horizontally: a table wider than a narrow viewport scrolls in place instead of widening the whole page. The wrapper is focusable (tabindex="0") so keyboard users can scroll it, and becomes a labelled region when :attr:caption is set.

Returns:

Name Type Description
Widget Widget

A scroll wrapper around a <table> with a header

Widget

row and one body row per record, or a single spanning row

carrying Widget

attr:empty_text when there is nothing to show.

TableColumn dataclass

TableColumn(
    name: str,
    header: str | None = None,
    render: CellRenderer | None = None,
    align: CellAlign | None = None,
    class_name: str = "",
)

One column of a :class:DataTable, when a field name is not enough.

Attributes:

Name Type Description
name str

The column name — the field read from each row for a text cell, and the key of :attr:DataTable.headers.

header str | None

Header text. None falls back to :attr:DataTable.headers, then the schema field's title, then the humanized name.

render CellRenderer | None

Builds the cell content from the row. None keeps the default: the field value formatted by :meth:DataTable.cell_text. Widgets go through the normal renderer, so nothing is ever inserted as raw HTML.

align CellAlign | None

Horizontal alignment of the header and every cell, applied as the :attr:ComponentClasses.table_align modifier ("right" also switches to tabular figures, for numbers).

class_name str

Extra class applied to the header and every cell of the column — for example one a media query hides on narrow screens.

Example
from pydantic import BaseModel
from tempest_core import Text, Widget

from tempest_fastapi_sdk.ui.components import DataTable, TableColumn


class FileResponseSchema(BaseModel):
    id: int
    name: str
    size: int


def name_link(row: FileResponseSchema) -> Widget:
    return Text(content=row.name, tag="a", attrs={"href": f"/files/{row.id}"})


table = DataTable(
    rows=[FileResponseSchema(id=1, name="a.txt", size=120)],
    columns=[
        TableColumn("name", render=name_link),
        TableColumn("size", align="right", class_name="col-optional"),
    ],
)

Pagination

Bases: Component

Numbered page links with previous / next controls.

Attributes:

Name Type Description
page int

The current page, 1-indexed.

pages int

The total number of pages. 0 or 1 renders nothing — a single page needs no control.

url str

Path the links point at. The page number is appended as a query parameter.

query_param str

Name of the page query parameter.

extra_query dict[str, str]

Query parameters carried over on every link, so active filters survive a page change.

window int

How many neighbours of the current page to list on each side.

previous_label str

Text of the previous-page link.

next_label str

Text of the next-page link.

classes ComponentClasses

Class names to apply.

Example
from tempest_fastapi_sdk.ui.components import Pagination

control = Pagination(page=3, pages=10, url="/users")

href

href(page: int) -> str

Build the URL of one page.

Parameters:

Name Type Description Default
page int

The target page number.

required

Returns:

Name Type Description
str str

{url}?{extra}&{query_param}={page}, with the extra

str

parameters preserved in insertion order.

visible_pages

visible_pages() -> list[int]

Return the page numbers rendered as links.

Returns:

Type Description
list[int]

list[int]: The current page plus :attr:window neighbours

list[int]

on each side, clamped to the available range.

render

render() -> Widget

Compose the pagination control.

Returns:

Name Type Description
Widget Widget

A <nav> holding the links, or an empty <nav>

Widget

when there is at most one page.

PaginationEnvelopeLike

Bases: Protocol

Any pagination envelope exposing page and pages.

Declared as read-only properties so :class:~tempest_fastapi_sdk.schemas.BasePaginationSchema, frozen dataclasses, and plain objects are all accepted.

page property

page: int

Return the 1-indexed current page.

pages property

pages: int

Return the total number of pages.

pagination_for

pagination_for(
    envelope: PaginationEnvelopeLike,
    *,
    url: str,
    query_param: str = "page",
    extra_query: dict[str, str] | None = None,
    window: int = 2,
    classes: ComponentClasses | None = None,
) -> Pagination

Build a :class:Pagination from a pagination envelope.

Parameters:

Name Type Description Default
envelope PaginationEnvelopeLike

A :class:~tempest_fastapi_sdk.schemas.BasePaginationSchema (or anything exposing page and pages).

required
url str

Path the links point at.

required
query_param str

Name of the page query parameter.

'page'
extra_query dict[str, str] | None

Query parameters carried over on every link.

None
window int

Neighbour pages listed on each side.

2
classes ComponentClasses | None

Class name overrides.

None

Returns:

Name Type Description
Pagination Pagination

The control matching the envelope.

Raises:

Type Description
AttributeError

When the envelope exposes no page / pages.

Example
from tempest_fastapi_sdk.schemas import BasePaginationSchema
from tempest_fastapi_sdk.ui.components import pagination_for

envelope: BasePaginationSchema[str] = BasePaginationSchema[str](
    items=["a"], total=30, page=2, page_size=10, pages=3
)
control = pagination_for(envelope, url="/users")

EmptyState

Bases: Component

A titled placeholder for an empty collection.

Collections return [] rather than a 404 (the SDK's convention), so "nothing here" is a normal, successful state and deserves a normal, informative screen.

Attributes:

Name Type Description
title str

The heading.

description str

Supporting text under the heading.

action Widget | None

Optional call to action — a link or a button leading to the screen that creates the first record.

classes ComponentClasses

Class names to apply.

Example
from tempest_fastapi_sdk.ui.components import EmptyState

state = EmptyState(
    title="Nenhum pedido ainda",
    description="Os pedidos aparecem aqui assim que o primeiro entrar.",
)

render

render() -> Widget

Compose the empty state.

Returns:

Name Type Description
Widget Widget

A <div> with the heading, the description that

Widget

was given, and the action when present.

FlashMessages

Bases: Component

The list of pending flash messages, one :class:Alert each.

Place it once in the base page's shell and feed it what :func:tempest_fastapi_sdk.ssr.get_flashes returned for the request. With no messages it renders an empty wrapper, which the bundled stylesheet hides.

Attributes:

Name Type Description
messages list[FlashMessage]

The messages to show, in order.

classes ComponentClasses

Class names to apply.

Example
from tempest_fastapi_sdk.ui.components import FlashMessage, FlashMessages

notices = FlashMessages(
    messages=[FlashMessage(message="Bucket criado.", variant="success")],
)

render

render() -> Widget

Compose the messages.

Returns:

Name Type Description
Widget Widget

A <div> holding one :class:Alert per message.

FlashMessage

Bases: BaseModel

One notice queued for the next screen the user sees.

Written by :func:tempest_fastapi_sdk.ssr.flash and read back by :func:tempest_fastapi_sdk.ssr.get_flashes, which travel it in a signed cookie — the text is always the server's, never read from the URL.

Attributes:

Name Type Description
message str

The notice text, rendered escaped.

variant AlertVariant

Its severity, which picks the colour and the ARIA role of the rendered :class:Alert.

NavBar

Bases: Component

A list of links, with the current one marked.

The active link renders aria-current="page" so assistive technology reports the location, not just the styling.

Attributes:

Name Type Description
items list[NavItem]

The entries, in order.

active_href str

The href of the current page.

label str

The aria-label of the <nav> element.

classes ComponentClasses

Class names to apply.

Example
from tempest_fastapi_sdk.ui.components import NavBar, NavItem

nav = NavBar(
    items=[
        NavItem(label="Início", href="/"),
        NavItem(label="Usuários", href="/users"),
    ],
    active_href="/users",
)

render

render() -> Widget

Compose the navigation bar.

Returns:

Name Type Description
Widget Widget

A <nav> holding one <a> per entry.

NavItem dataclass

NavItem(label: str, href: str)

One entry of a :class:NavBar.

Attributes:

Name Type Description
label str

The link text.

href str

The link target.

ComponentClasses dataclass

ComponentClasses(
    card: str = "tui-card",
    card_title: str = "tui-card__title",
    card_body: str = "tui-card__body",
    card_footer: str = "tui-card__footer",
    alert: str = "tui-alert",
    alert_variant: str = "tui-alert--",
    flash: str = "tui-flash",
    table: str = "tui-table",
    table_empty: str = "tui-table__empty",
    pagination: str = "tui-pagination",
    pagination_link: str = "tui-pagination__link",
    pagination_current: str = "tui-pagination__link--current",
    pagination_disabled: str = "tui-pagination__link--disabled",
    empty_state: str = "tui-empty",
    empty_state_title: str = "tui-empty__title",
    empty_state_text: str = "tui-empty__text",
    nav: str = "tui-nav",
    nav_link: str = "tui-nav__link",
    nav_active: str = "tui-nav__link--active",
    shell: str = "tui-shell",
    shell_header: str = "tui-shell__header",
    shell_main: str = "tui-shell__main",
    shell_footer: str = "tui-shell__footer",
    grid: str = "tui-grid",
    table_align: str = "tui-table__cell--",
    shell_main_full: str = "tui-shell__main--full",
    table_scroll: str = "tui-table-scroll",
)

Class names applied by :mod:tempest_fastapi_sdk.ui.components.

The defaults match the rules built by :func:~tempest_fastapi_sdk.ui.components.component_stylesheet.

Attributes:

Name Type Description
card str

The card wrapper.

card_title str

The card heading.

card_body str

The card content area.

card_footer str

The card footer.

alert str

The alert wrapper.

alert_variant str

Prefix of the per-variant alert modifier; "tui-alert--" yields tui-alert--error.

flash str

The wrapper of the flash messages.

table str

The data table.

table_scroll str

The wrapper around the table that scrolls it horizontally, so a table wider than the viewport scrolls in place instead of widening the whole page.

table_empty str

The row shown when a table has no rows.

table_align str

Prefix of the per-column alignment modifier; "tui-table__cell--" yields tui-table__cell--right. Applied to the header and every body cell of the column.

pagination str

The pagination wrapper.

pagination_link str

A page link.

pagination_current str

The current-page marker.

pagination_disabled str

A disabled previous/next link.

empty_state str

The empty-state wrapper.

empty_state_title str

The empty-state heading.

empty_state_text str

The empty-state description.

nav str

The navigation bar.

nav_link str

A navigation link.

nav_active str

The active navigation link.

shell str

The page shell wrapper.

shell_header str

The shell header.

shell_main str

The shell main region.

shell_main_full str

Modifier of the main region when the shell spans the full viewport width (Shell(width="full")).

shell_footer str

The shell footer.

grid str

The grid container.

component_stylesheet

component_stylesheet(
    *, classes: ComponentClasses | None = None, theme: ThemeTokens | None = None
) -> StyleSheet

Build the stylesheet that styles the bundled components.

Parameters:

Name Type Description Default
classes ComponentClasses | None

The class names to target. Defaults to the set the components apply.

None
theme ThemeTokens | None

The tokens whose custom properties the rules reference. Pass the same theme the application sheet emits so the prefixes line up.

None

Returns:

Name Type Description
StyleSheet StyleSheet

The component rules, with no reset and no token

StyleSheet

block of their own, ready to merge into the application sheet.