TimeInput
Group: Input
Description
A TimeInput shows the browser's time picker. The visitor picks a
time of day (hours and minutes), and the chosen time arrives at the
route as a parameter with the field's name. Use it instead of a
TextBox whenever you need a clock time: the picker
cannot produce a 25th hour or a stray letter.
Syntax
TimeInput(name)
TimeInput(name, default_value)
Parameters
| Parameter | Type | Default | Meaning |
|---|---|---|---|
name |
str |
required | The field's name, matching a parameter of the receiving route. |
default_value |
str or datetime.time |
empty | The pre-filled time, written as "HH:MM" in 24-hour form (like "14:30") or given as a time object. |
Like every component, TimeInput also accepts
styling and attribute keywords. It
recognizes no HTML attributes beyond the ones every component shares,
so min, max, step, required, and disabled do not work here,
and the picker cannot be limited to a range of times. Drafter turns a
keyword it does not recognize into a CSS style, which the browser
ignores, and it reports no error. Check the time inside the route
instead. Plain HTML attributes
explains the keyword rule.
Examples
The route's parameter is annotated as datetime.time, so Drafter
converts the submitted text into a real time object with .hour
and .minute fields:
from drafter import *
from dataclasses import dataclass
from datetime import time
@dataclass
class State:
dinner: str
@route
def index(state: State) -> Page:
return Page(state, [
Header("Cat Feeding Schedule"),
"Captain eats dinner at: " + state.dinner + "\n",
"New dinner time:",
TimeInput("when", state.dinner),
"\n",
Button("Update", "update")
])
@route
def update(state: State, when: time) -> Page:
state.dinner = when.isoformat(timespec="minutes")
return index(state)
start_server(State("17:00"))
Notes
- Annotate the receiving parameter as
datetime.timeto get a time object, or asstrto get the raw"HH:MM"text. - With a
timeannotation, leaving the field empty submits the current time. With astrannotation, an empty field arrives as"". - Values always use 24-hour form (
"14:30", not"2:30 PM"), even when the picker displays AM and PM to the visitor.
Accessibility
Put a short label right before the field (the "New dinner time:" text above, or a Label) so it is clear what the time is for.
Related components
- DateInput: collect a calendar date instead.
- DateTimeInput: collect a date and time together.