Skip to content

ui.tables

ui.tables

Table rendering helpers for the TapMap UI.

Provide reusable helpers for building HTML tables used across multiple UI views.

ColumnSpec dataclass

Describe one table column.

Source code in ui/tables.py
16
17
18
19
20
21
@dataclass(frozen=True)
class ColumnSpec:
    """Describe one table column."""

    header: str
    width: str | None = None

cell(text, *, title=None)

Render a table cell with truncation and tooltip.

Source code in ui/tables.py
24
25
26
27
28
29
def cell(text: str, *, title: str | None = None) -> html.Td:
    """Render a table cell with truncation and tooltip."""
    value = text or ""
    tooltip = title if title is not None else value
    tooltip = tooltip if tooltip else None
    return html.Td(html.Span(value, className="mx-cell-text", title=tooltip))

build_table(*, class_name, columns, header_cells, body_rows)

Build a Dash HTML table with colgroup, thead and tbody.

Source code in ui/tables.py
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
def build_table(
    *,
    class_name: str,
    columns: Sequence[ColumnSpec],
    header_cells: Sequence[str],
    body_rows: Iterable[Any],
) -> html.Table:
    """Build a Dash HTML table with colgroup, thead and tbody."""
    colgroup = html.Colgroup(
        [html.Col(style={"width": col.width}) if col.width else html.Col() for col in columns]
    )

    thead = html.Thead(html.Tr([html.Th(h) for h in header_cells]))

    tbody = html.Tbody(list(body_rows))

    return html.Table(
        className=class_name,
        children=[colgroup, thead, tbody],
    )

kv_table(rows)

Build a two column key/value table with tooltips.

Source code in ui/tables.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def kv_table(rows: Iterable[tuple[str, str]]) -> html.Table:
    """Build a two column key/value table with tooltips."""
    body: list[Any] = []
    for key, value in rows:
        v = "" if value is None else str(value)
        body.append(
            html.Tr(
                [
                    html.Td(key),
                    html.Td(html.Span(v, title=v if v else None)),
                ]
            )
        )

    colgroup = html.Colgroup(
        [
            html.Col(style={"width": "180px"}),
            html.Col(),
        ]
    )

    return html.Table(
        className="mx-table mx-info-table",
        children=[colgroup, html.Tbody(body)],
    )