DataTable
DataTable is a higher-level table with sorting, search, and pagination built in. It accepts list[dict] and pandas or polars DataFrames.
Beta API
DataTable is currently marked @beta. It is stable enough for real apps, but query contracts and helper ergonomics may still evolve in minor releases.
Quick Start
| Name | Status | |
|---|---|---|
| Ada Lovelace | ada@example.com | Active |
| Alan Turing | alan@example.com | Invited |
Data Sources
DataTable accepts:
list[dict]- pandas
DataFrame - polars
DataFrame
DataTable(
records,
columns=["name", "email", "status"],
header_map={"name": "User", "email": "Email"},
include_index=False,
)
Sorting and Search
Enable sorting and search with flags. For interactive controls, provide endpoint or base_url so header links and search submits have a stable request target:
DataTable(
data,
sortable=True,
searchable=True,
search_placeholder="Search users...",
search_param="q",
search_debounce=300,
endpoint="/users",
)
Restrict sortable columns with a list:
If you want to control the current state (server-side mode), pass sort, direction, and search.
Pagination
Client-side pagination works for small sets when you render the full dataset locally. For server-side pagination, pass total_rows with endpoint or base_url.
Server-Side Contract
When you pass an endpoint, DataTable emits these query params:
sortdirectionpageper_pageq(or yoursearch_param)- any
filtersyou provide
DataTable(
data,
endpoint="/users",
sortable=True,
searchable=True,
pagination=True,
hx_target="#table",
hx_swap="outerHTML",
push_url=True,
)
Filters and Base URL
Use filters to preserve extra query params in pagination and sort links. Use base_url if you are not using HTMX. When you pass sort or search, DataTable applies that state to the rendered rows so the UI stays consistent with the current request.
Export Integration
Use the helpers to reuse the table query state for exports, links, and handlers. They are available both as top-level helpers and as convenience attributes on DataTable:
from faststrap import datatable_export_params, ExportButton
params = datatable_export_params(
sort="name",
direction="asc",
search="alice",
filters={"team": "ops"},
)
ExportButton("Export CSV", endpoint="/export", export_format="csv", extra_params=params)
Build the same query contract for non-export actions:
from faststrap import datatable_query_params
params = datatable_query_params(
sort="name",
direction="asc",
search="alice",
filters={"team": "ops"},
page=2,
per_page=25,
)
Build one page URL while preserving table state:
from faststrap import datatable_page_url
url = datatable_page_url(
"/users?view=active",
page=3,
per_page=25,
sort="name",
search="alice",
filters={"team": "ops"},
)
Theming and Layout
DataTable respects Bootstrap table styles:
striped=Truehover=Truebordered=Trueresponsive=Trueorresponsive="md"
You can also pass table_cls and table_attrs to control the inner table element.
Accessibility
DataTable renders semantic table markup (<table>, <thead>, <tbody>). Use short, descriptive column names for screen readers.
Security Notes
- Validate
sortanddirectionserver-side to avoid unsafe column injection. - Enforce
per_pagelimits to prevent excessive responses. - Sanitize search strings before using them in SQL or ORM queries.
API Reference
faststrap.components.display.data_table.DataTable(data, *, columns=None, header_map=None, max_rows=None, include_index=False, empty_text='No data available', none_as='', striped=True, hover=True, bordered=False, responsive=True, sortable=False, sort=None, direction='asc', searchable=False, search=None, search_param='q', search_placeholder='Search...', search_debounce=300, pagination=False, page=1, per_page=25, total_rows=None, endpoint=None, base_url=None, filters=None, hx_target=None, hx_swap='outerHTML', push_url=False, table_id=None, table_cls=None, table_attrs=None, **kwargs)
DataTable with optional sorting, search, and pagination.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
List of dicts or pandas/polars DataFrame. |
required |
columns
|
list[str] | None
|
Optional column order. |
None
|
header_map
|
dict[str, str] | None
|
Optional display name mapping for headers. |
None
|
max_rows
|
int | None
|
Optional max rows to render (pre-pagination). |
None
|
include_index
|
bool
|
Include index column for DataFrame or list data. |
False
|
empty_text
|
str
|
Text to display when no records exist. |
'No data available'
|
none_as
|
str
|
Substitute for None values. |
''
|
striped
|
bool
|
Enable zebra striping. |
True
|
hover
|
bool
|
Enable row hover styles. |
True
|
bordered
|
bool
|
Enable borders. |
False
|
responsive
|
bool | ResponsiveType
|
Wrap table in Bootstrap responsive container. |
True
|
sortable
|
bool | list[str]
|
True for all columns or a list of sortable columns. |
False
|
sort
|
str | None
|
Current sort column. |
None
|
direction
|
SortableDirection
|
Current sort direction. |
'asc'
|
searchable
|
bool
|
Render a search input. |
False
|
search
|
str | None
|
Current search value. |
None
|
search_param
|
str
|
Query param name for search. |
'q'
|
search_placeholder
|
str
|
Placeholder text for search input. |
'Search...'
|
search_debounce
|
int
|
Debounce (ms) for HTMX search input. |
300
|
pagination
|
bool
|
Enable pagination controls. |
False
|
page
|
int
|
Current page (1-indexed). |
1
|
per_page
|
int
|
Rows per page. |
25
|
total_rows
|
int | None
|
Total rows across all pages (server-side). |
None
|
endpoint
|
str | None
|
HTMX endpoint for server-side updates. |
None
|
base_url
|
str | None
|
Base URL for standard links (fallback). |
None
|
filters
|
dict[str, Any] | None
|
Extra query params to preserve in links. |
None
|
hx_target
|
str | None
|
HTMX target selector. |
None
|
hx_swap
|
str | None
|
HTMX swap strategy for links/search. |
'outerHTML'
|
push_url
|
bool
|
If True, enable hx-push-url for links. |
False
|
table_id
|
str | None
|
Explicit wrapper id (auto-generated if omitted). |
None
|
table_cls
|
str | None
|
Extra CSS classes for the table element. |
None
|
table_attrs
|
dict[str, Any] | None
|
Extra attributes applied to the table element. |
None
|
**kwargs
|
Any
|
Additional HTML attributes for the wrapper. |
{}
|
Source code in src/faststrap/components/display/data_table.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 | |
faststrap.components.display.data_table.datatable_export_params(*, sort=None, direction='asc', search=None, search_param='q', filters=None, include_pagination=False, page=None, per_page=None)
Build query params for exporting DataTable state.
This helper mirrors the DataTable query contract and is intended to be passed into ExportButton(extra_params=...).
Source code in src/faststrap/components/display/data_table.py
faststrap.components.display.data_table.datatable_query_params(*, sort=None, direction='asc', search=None, search_param='q', filters=None, page=None, per_page=None)
Build the common DataTable query contract for links and handlers.
Source code in src/faststrap/components/display/data_table.py
faststrap.components.display.data_table.datatable_page_url(base_url, *, page, per_page=None, sort=None, direction='asc', search=None, search_param='q', filters=None)
Build a URL for one DataTable page while preserving current state.