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
Quick Start
| ID | Name | Role |
|---|---|---|
| 1 | Alice | Admin |
| 2 | Bob | User |
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.
| Header |
|---|
| Dark Striped Content |
| Header |
|---|
| Borderless Content |
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
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | |