Pagination
The Pagination component creates Bootstrap page navigation for tables, search results, card feeds, and HTMX-powered partial updates.
Quick Start
Common Patterns
Compact Table Pagination
First, Previous, Next, Last Controls
Pagination(
current_page=2,
total_pages=100,
show_first_last=True, # adds << and >> controls
show_prev_next=True, # adds < and > controls
)
Preserve Search And Filter Query Params
Use query_params when pagination links need to keep the current filter/search state.
Pagination(
current_page=page,
total_pages=total_pages,
base_url="/products",
query_params={"q": query, "category": category},
page_param="page",
)
HTMX Pagination
Set htmx=True to render hx-get links while still keeping normal href values as a fallback.
Pagination(
current_page=page,
total_pages=total_pages,
base_url="/products",
query_params={"q": query},
htmx=True,
hx_target="#product-list",
hx_swap="outerHTML",
hx_push_url=True,
)
A matching handler can return the updated list and pagination fragment:
@app.get("/products")
def products(q: str = "", page: int = 1):
items, total_pages = search_products(q=q, page=page)
return Div(
product_grid(items),
Pagination(
current_page=page,
total_pages=total_pages,
base_url="/products",
query_params={"q": q},
htmx=True,
hx_target="#product-list",
hx_swap="outerHTML",
hx_push_url=True,
),
id="product-list",
)
Responsive Pattern
from faststrap import Div, Pagination
def ResponsivePagination(current: int, total: int):
return Div(
Pagination(
current_page=current,
total_pages=total,
max_pages=7,
show_first_last=True,
cls="d-none d-md-flex",
),
Pagination(
current_page=current,
total_pages=total,
max_pages=3,
show_first_last=False,
size="sm",
cls="d-md-none",
),
)
Defaults
from faststrap import Pagination, set_component_defaults
set_component_defaults(
"Pagination",
align="center",
show_first_last=True,
max_pages=7,
)
Pagination(current_page=5, total_pages=20)
Accessibility
Faststrap handles the important pagination semantics:
aria-labelon the outer<nav>aria-current="page"on the active pagearia-labelon first/previous/next/last controls- disabled links for unavailable controls
Use a more specific label when the page has more than one paginator:
Parameter Reference
| Parameter | Type | Default | Description |
|---|---|---|---|
current_page |
int |
Required | Current active page, 1-indexed |
total_pages |
int |
Required | Total number of pages |
size |
"sm" | "lg" | None |
None |
Bootstrap pagination size |
align |
"start" | "center" | "end" |
"start" |
Alignment of the pagination list |
max_pages |
int | None |
5 |
Maximum numbered page links to show |
base_url |
str | None |
"#" |
Base URL used for page links |
page_param |
str |
"page" |
Query parameter name for page numbers |
query_params |
dict[str, Any] | None |
None |
Extra query params to preserve in generated links |
show_first_last |
bool | None |
False |
Show << and >> controls |
show_prev_next |
bool | None |
True |
Show < and > controls |
htmx |
bool |
False |
Add HTMX attributes to page links |
hx_target |
str | None |
None |
Target for HTMX page updates |
hx_swap |
str | None |
"outerHTML" |
HTMX swap strategy |
hx_push_url |
bool |
False |
Push generated URLs into browser history |
**kwargs |
Any |
- | Additional HTML attributes such as cls, id, or aria_label |
faststrap.components.navigation.pagination.Pagination(current_page, total_pages, size=UNSET, align=UNSET, max_pages=UNSET, base_url=UNSET, show_first_last=UNSET, show_prev_next=UNSET, page_param='page', query_params=None, htmx=False, hx_target=None, hx_swap='outerHTML', hx_push_url=False, **kwargs)
Bootstrap Pagination component for page navigation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_page
|
int
|
Current active page (1-indexed) |
required |
total_pages
|
int
|
Total number of pages |
required |
size
|
SizeType | None
|
Pagination size (sm, lg) |
UNSET
|
align
|
AlignType | None
|
Alignment (start, center, end) |
UNSET
|
max_pages
|
int | None
|
Maximum page numbers to show |
UNSET
|
base_url
|
str | None
|
Base URL for page links |
UNSET
|
show_first_last
|
bool | None
|
Show first/last page buttons |
UNSET
|
show_prev_next
|
bool | None
|
Show previous/next buttons |
UNSET
|
page_param
|
str
|
Query-string parameter used for page links |
'page'
|
query_params
|
dict[str, Any] | None
|
Extra query parameters to preserve |
None
|
htmx
|
bool
|
Add hx-get attributes to generated page links |
False
|
hx_target
|
str | None
|
Optional HTMX target for nav and generated links |
None
|
hx_swap
|
str | None
|
Optional HTMX swap mode for generated links |
'outerHTML'
|
hx_push_url
|
bool
|
Push generated URLs when HTMX links are clicked |
False
|
**kwargs
|
Any
|
Additional HTML attributes |
{}
|
Source code in src/faststrap/components/navigation/pagination.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 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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |