Skip to content
ReferenceCoreCollect a yes/no.

CheckBox

Group: Input

Description

A CheckBox collects a yes/no answer. The receiving route's parameter should be annotated bool: the value arrives as True when the box is checked and False when it is not, including when the visitor leaves the box untouched.

Syntax

CheckBox(name)
CheckBox(name, default_value)

Parameters

Parameter Type Default Meaning
name str required The field's name, matching a parameter of the receiving route. Must be a valid Python parameter name.
default_value bool False Whether the box starts checked.

Like every component, CheckBox also accepts styling and attribute keywords.

Examples

from drafter import *
from dataclasses import dataclass


@dataclass
class State:
    walked: bool
    fed: bool


@route
def index(state: State) -> Page:
    return Page(state, [
        Header("Babbage's Day"),
        "Walked: " + str(state.walked) + "\n",
        "Fed: " + str(state.fed) + "\n",
        CheckBox("walked_today", state.walked),
        " Took Babbage for a walk\n",
        CheckBox("fed_today", state.fed),
        " Fed Babbage\n",
        Button("Save", "save")
    ])


@route
def save(state: State, walked_today: bool, fed_today: bool) -> Page:
    state.walked = walked_today
    state.fed = fed_today
    return index(state)


start_server(State(False, False))

Notes

  • An unchecked box still submits a value: the parameter receives False, not nothing. (Behind the scenes, Drafter adds a hidden companion field so the unchecked state is submitted too.)
  • Passing the current state value as default_value keeps the box reflecting what was last saved, as in the example.
  • For choosing one option among several, use RadioButtonGroup or SelectBox; for choosing several from a list, use RelatedCheckBox.

Accessibility

Put the describing text directly next to the box, as in the example, or use a Label associated with the box so clicking the words toggles it.