Skip to content

Table

The Table component enables you to display tabular data efficiently. FastStrap's implementation decomposes the table into semantic sub-components (THead, TBody, TRow, TCell) for maximum flexibility, while providing high-level arguments for common styles like striping and hover effects.

For sorting, search, and pagination, see DataTable.

Naming update in v0.6.1

Table, THead, TBody, TRow, and TCell are unchanged and remain the primary API. Faststrap v0.6.1 adds optional aliases BsTable, BsTHead, BsTBody, BsTRow, and BsTCell for projects that mix Faststrap with FastHTML's native table elements.

  • Existing code can keep using Table / THead / TBody / TRow / TCell
  • New mixed-import code may prefer the Bs* aliases for clarity

Bootstrap Reference

Bootstrap 5 Tables


Quick Start

Live Preview
ID Name Role
1 Alice Admin
2 Bob User
Table(
    THead(TRow(TCell("ID"), TCell("Name"), TCell("Role"))),
    TBody(
        TRow(TCell("1"), TCell("Alice"), TCell("Admin")),
        TRow(TCell("2"), TCell("Bob"), TCell("User")),
    ),
    striped=True, hover=True
)

Styling Options

FastStrap exposes Bootstrap's powerful table modifiers as simple boolean arguments.

1. Variants & Themes

Use variant to color the entire table, or set striped / hover for readability.

Live Preview (Variants)
Header
Dark Striped Content
Header
Borderless Content
# Dark Mode Table
Table(..., variant="dark", striped=True)

# Borderless
Table(..., borderless=True)

2. Responsiveness

Tables can overflow on small screens. Wrap them in a responsive container automatically using the responsive argument.

# Enables horizontal scrolling on small devices
Table(..., responsive=True) # or responsive="sm", "md", "lg"

3. DataFrame Builder (Beta)

Generate a table directly from pandas/polars or list-of-dict records:

import pandas as pd
from faststrap import Table

df = pd.DataFrame([
    {"name": "Alice", "age": 25},
    {"name": "Bob", "age": 30},
])

table = Table.from_df(
    df,
    striped=True,
    include_index=False,
    max_rows=100,
)

Supported inputs: - pandas DataFrame - polars DataFrame (if installed) - list[dict]

Optional Aliases for Mixed Imports

from faststrap import BsTable, BsTHead, BsTBody, BsTRow, BsTCell

BsTable(
    BsTHead(BsTRow(BsTCell("ID", header=True), BsTCell("Name", header=True))),
    BsTBody(BsTRow(BsTCell("1"), BsTCell("Alice"))),
    striped=True,
)

API Reference

faststrap.components.display.table.Table(*children, striped=False, striped_columns=False, bordered=False, borderless=False, hover=False, small=False, variant=None, responsive=False, caption_top=False, **kwargs)

Bootstrap Table component.

A responsive, styled table with support for striped rows, hover effects, borders, and color variants.

Parameters:

Name Type Description Default
*children Any

Table content (THead, TBody, or direct Tr elements)

()
striped bool

Add zebra-striping to rows

False
striped_columns bool

Add zebra-striping to columns

False
bordered bool

Add borders on all sides

False
borderless bool

Remove all borders

False
hover bool

Enable hover state on rows

False
small bool

Make table more compact

False
variant TableVariantType | None

Bootstrap color variant for table background

None
responsive bool | Literal['sm', 'md', 'lg', 'xl', 'xxl']

Make table horizontally scrollable. True for all breakpoints, or specify breakpoint (sm, md, lg, xl, xxl)

False
caption_top bool

Place caption at top of table

False
**kwargs Any

Additional HTML attributes (cls, id, hx-, data-, etc.)

{}

Returns:

Type Description
Table | Div

FastHTML Table element, wrapped in Div if responsive

Example

Basic table:

Table( ... THead(TRow(TCell("Name", header=True), TCell("Age", header=True))), ... TBody(TRow(TCell("Alice"), TCell("25"))) ... )

Striped and hoverable:

Table( ... THead(...), ... TBody(...), ... striped=True, ... hover=True ... )

Responsive with variant:

Table(..., responsive=True, variant="dark")

Responsive at breakpoint:

Table(..., responsive="lg")

See Also

Bootstrap docs: https://getbootstrap.com/docs/5.3/content/tables/

Source code in src/faststrap/components/display/table.py
@register(category="display")
@stable
def Table(
    *children: Any,
    striped: bool = False,
    striped_columns: bool = False,
    bordered: bool = False,
    borderless: bool = False,
    hover: bool = False,
    small: bool = False,
    variant: TableVariantType | None = None,
    responsive: bool | Literal["sm", "md", "lg", "xl", "xxl"] = False,
    caption_top: bool = False,
    **kwargs: Any,
) -> FTTable | Div:
    """Bootstrap Table component.

    A responsive, styled table with support for striped rows, hover effects,
    borders, and color variants.

    Args:
        *children: Table content (THead, TBody, or direct Tr elements)
        striped: Add zebra-striping to rows
        striped_columns: Add zebra-striping to columns
        bordered: Add borders on all sides
        borderless: Remove all borders
        hover: Enable hover state on rows
        small: Make table more compact
        variant: Bootstrap color variant for table background
        responsive: Make table horizontally scrollable. True for all breakpoints,
                   or specify breakpoint (sm, md, lg, xl, xxl)
        caption_top: Place caption at top of table
        **kwargs: Additional HTML attributes (cls, id, hx-*, data-*, etc.)

    Returns:
        FastHTML Table element, wrapped in Div if responsive

    Example:
        Basic table:
        >>> Table(
        ...     THead(TRow(TCell("Name", header=True), TCell("Age", header=True))),
        ...     TBody(TRow(TCell("Alice"), TCell("25")))
        ... )

        Striped and hoverable:
        >>> Table(
        ...     THead(...),
        ...     TBody(...),
        ...     striped=True,
        ...     hover=True
        ... )

        Responsive with variant:
        >>> Table(..., responsive=True, variant="dark")

        Responsive at breakpoint:
        >>> Table(..., responsive="lg")

    See Also:
        Bootstrap docs: https://getbootstrap.com/docs/5.3/content/tables/
    """
    # Build table classes
    classes = ["table"]

    if striped:
        classes.append("table-striped")

    if striped_columns:
        classes.append("table-striped-columns")

    if bordered:
        classes.append("table-bordered")

    if borderless:
        classes.append("table-borderless")

    if hover:
        classes.append("table-hover")

    if small:
        classes.append("table-sm")

    if variant:
        classes.append(f"table-{variant}")

    if caption_top:
        classes.append("caption-top")

    # Merge with user classes
    user_cls = kwargs.pop("cls", "")
    all_classes = merge_classes(" ".join(classes), user_cls)

    # Build attributes
    attrs: dict[str, Any] = {"cls": all_classes}
    attrs.update(convert_attrs(kwargs))

    # Create table
    table = FTTable(*children, **attrs)

    # Wrap in responsive container if needed
    if responsive:
        if responsive is True:
            responsive_cls = "table-responsive"
        else:
            responsive_cls = f"table-responsive-{responsive}"
        return Div(table, cls=responsive_cls)

    return table