Checkbox, Radio & Switch
These components allow users to select one or more options from a list. Faststrap provides Checkbox for multiple select, Radio for single select, Switch for toggles, and Range for slider inputs.
Quick Start
Checkbox("subscribe", label="Subscribe to newsletter")
Radio("plan", value="monthly", label="Monthly Plan")
Switch("notifications", label="Enable Notifications")
Usage Scenarios
Grouping Radios
Div(
Radio("shipping", value="standard", label="Standard Shipping (Free)", checked=True),
Radio("shipping", value="express", label="Express Shipping ($10)"),
Radio("shipping", value="overnight", label="Overnight ($25)")
)
Inline Layout
Div(
Checkbox("tag", value="python", label="Python", inline=True),
Checkbox("tag", value="javascript", label="JavaScript", inline=True),
Checkbox("tag", value="rust", label="Rust", inline=True)
)
Switches
Switch("wifi", label="Wi-Fi", checked=True)
Switch("bluetooth", label="Bluetooth")
Switch("airplane", label="Airplane Mode", disabled=True)
Button Style (Toggle Buttons)
Div(
Radio("view", value="list", label="List View", btn_style=True, variant="outline-primary", checked=True),
Radio("view", value="grid", label="Grid View", btn_style=True, variant="outline-primary"),
cls="btn-group"
)
Range Slider
Range("volume", label="Volume", value=50)
Range("price", label="Price", min_val=10, max_val=500, step=10)
Parameter Reference
Checkbox
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str |
Required | Input name attribute |
label |
str \| None |
None |
Label text |
value |
str |
"1" |
Value when checked |
checked |
bool |
False |
Whether initially checked |
disabled |
bool |
False |
Disable the checkbox |
required |
bool |
False |
Mark as required |
inline |
bool |
False |
Display inline |
reverse |
bool |
False |
Put checkbox on right side of label |
checkbox_id |
str \| None |
None |
ID for the input |
size |
str \| None |
None |
Control size (sm, lg) |
input_cls |
str |
"" |
Additional classes for input |
label_cls |
str |
"" |
Additional classes for label |
help_text |
str \| None |
None |
Help text below input |
Radio
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str |
Required | Input name attribute (same name groups radios) |
label |
str \| None |
None |
Label text |
value |
str |
"" |
Value when selected |
checked |
bool |
False |
Whether initially selected |
disabled |
bool |
False |
Disable the radio |
required |
bool |
False |
Mark as required |
inline |
bool |
False |
Display inline |
reverse |
bool |
False |
Put radio on right side of label |
radio_id |
str \| None |
None |
ID for the input |
input_cls |
str |
"" |
Additional classes for input |
label_cls |
str |
"" |
Additional classes for label |
Switch
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str |
Required | Input name attribute |
label |
str \| None |
None |
Label text |
value |
str |
"1" |
Value when checked |
checked |
bool |
False |
Whether initially on |
disabled |
bool |
False |
Disable the switch |
required |
bool |
False |
Mark as required |
reverse |
bool |
False |
Put switch on right side of label |
switch_id |
str \| None |
None |
ID for the input |
input_cls |
str |
"" |
Additional classes for input |
label_cls |
str |
"" |
Additional classes for label |
Range
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str |
Required | Input name attribute |
label |
str \| None |
None |
Label text |
value |
int \| float \| None |
None |
Initial value |
min_val |
int \| float |
0 |
Minimum value |
max_val |
int \| float |
100 |
Maximum value |
step |
int \| float \| None |
None |
Step increment |
disabled |
bool |
False |
Disable the range |
range_id |
str \| None |
None |
ID for the input |
input_cls |
str |
"" |
Additional classes for input |
label_cls |
str |
"" |
Additional classes for label |
Accessibility
- Labels are associated with inputs via
forandidattributes. - Required fields use the
requiredattribute. - Switch inputs include
role="switch"for proper screen reader semantics.
API Reference
faststrap.components.forms.checks.Checkbox(name, *, label=None, value='1', checked=False, disabled=False, required=False, inline=False, reverse=False, checkbox_id=None, size=None, input_cls='', label_cls='', help_text=None, **kwargs)
Bootstrap Checkbox component.
A styled checkbox form control with optional label.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Input name attribute |
required |
label
|
str | None
|
Label text for the checkbox |
None
|
value
|
str
|
Value when checked (default: "1") |
'1'
|
checked
|
bool
|
Whether checkbox is initially checked |
False
|
disabled
|
bool
|
Disable the checkbox |
False
|
required
|
bool
|
Mark as required field |
False
|
inline
|
bool
|
Display inline with other checkboxes |
False
|
reverse
|
bool
|
Put checkbox on the right side of label |
False
|
checkbox_id
|
str | None
|
ID for the input (auto-generated from name if not provided) |
None
|
size
|
SizeType | None
|
Control size (sm, lg) |
None
|
input_cls
|
str
|
Additional classes for input element |
''
|
label_cls
|
str
|
Additional classes for label element |
''
|
help_text
|
str | None
|
Help text displayed below |
None
|
**kwargs
|
Any
|
Additional HTML attributes |
{}
|
Returns:
| Type | Description |
|---|---|
Div
|
FastHTML Div element with form-check structure |
Examples:
Basic checkbox:
Checked by default:
Inline checkboxes:
>>> Checkbox("opt1", label="Option 1", inline=True)
>>> Checkbox("opt2", label="Option 2", inline=True)
See Also
Bootstrap docs: https://getbootstrap.com/docs/5.3/forms/checks-radios/
Source code in src/faststrap/components/forms/checks.py
16 17 18 19 20 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 | |
faststrap.components.forms.checks.Radio(name, *, label=None, value='', checked=False, disabled=False, required=False, inline=False, reverse=False, radio_id=None, input_cls='', label_cls='', **kwargs)
Bootstrap Radio button component.
A styled radio button form control. Group multiple radios with same name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Input name attribute (same name groups radios together) |
required |
label
|
str | None
|
Label text for the radio |
None
|
value
|
str
|
Value when selected |
''
|
checked
|
bool
|
Whether radio is initially selected |
False
|
disabled
|
bool
|
Disable the radio |
False
|
required
|
bool
|
Mark as required field |
False
|
inline
|
bool
|
Display inline with other radios |
False
|
reverse
|
bool
|
Put radio on the right side of label |
False
|
radio_id
|
str | None
|
ID for the input (auto-generated if not provided) |
None
|
input_cls
|
str
|
Additional classes for input element |
''
|
label_cls
|
str
|
Additional classes for label element |
''
|
**kwargs
|
Any
|
Additional HTML attributes |
{}
|
Returns:
| Type | Description |
|---|---|
Div
|
FastHTML Div element with form-check structure |
Examples:
Radio group:
>>> Radio("color", label="Red", value="red", checked=True)
>>> Radio("color", label="Blue", value="blue")
>>> Radio("color", label="Green", value="green")
Inline radios:
>>> Radio("size", label="Small", value="sm", inline=True)
>>> Radio("size", label="Large", value="lg", inline=True)
See Also
Bootstrap docs: https://getbootstrap.com/docs/5.3/forms/checks-radios/
Source code in src/faststrap/components/forms/checks.py
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 | |
faststrap.components.forms.checks.Switch(name, *, label=None, value='1', checked=False, disabled=False, required=False, reverse=False, switch_id=None, input_cls='', label_cls='', **kwargs)
Bootstrap Switch (toggle) component.
A toggle switch styled as an alternative to checkboxes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Input name attribute |
required |
label
|
str | None
|
Label text for the switch |
None
|
value
|
str
|
Value when checked (default: "1") |
'1'
|
checked
|
bool
|
Whether switch is initially on |
False
|
disabled
|
bool
|
Disable the switch |
False
|
required
|
bool
|
Mark as required field |
False
|
reverse
|
bool
|
Put switch on the right side of label |
False
|
switch_id
|
str | None
|
ID for the input (auto-generated from name if not provided) |
None
|
input_cls
|
str
|
Additional classes for input element |
''
|
label_cls
|
str
|
Additional classes for label element |
''
|
**kwargs
|
Any
|
Additional HTML attributes |
{}
|
Returns:
| Type | Description |
|---|---|
Div
|
FastHTML Div element with form-check form-switch structure |
Examples:
Basic switch:
Pre-toggled:
See Also
Bootstrap docs: https://getbootstrap.com/docs/5.3/forms/checks-radios/#switches
Source code in src/faststrap/components/forms/checks.py
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 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 | |
faststrap.components.forms.checks.Range(name, *, label=None, value=None, min_val=0, max_val=100, step=None, disabled=False, range_id=None, input_cls='', label_cls='', **kwargs)
Bootstrap Range (slider) component.
A styled range slider input control.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Input name attribute |
required |
label
|
str | None
|
Label text for the range |
None
|
value
|
int | float | None
|
Initial value |
None
|
min_val
|
int | float
|
Minimum value (default: 0) |
0
|
max_val
|
int | float
|
Maximum value (default: 100) |
100
|
step
|
int | float | None
|
Step increment (default: 1) |
None
|
disabled
|
bool
|
Disable the range |
False
|
range_id
|
str | None
|
ID for the input (auto-generated from name if not provided) |
None
|
input_cls
|
str
|
Additional classes for input element |
''
|
label_cls
|
str
|
Additional classes for label element |
''
|
**kwargs
|
Any
|
Additional HTML attributes |
{}
|
Returns:
| Type | Description |
|---|---|
Div
|
FastHTML Div element with form-range structure |
Examples:
Basic range:
With custom range:
See Also
Bootstrap docs: https://getbootstrap.com/docs/5.3/forms/range/
Source code in src/faststrap/components/forms/checks.py
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 | |