Date range picker
Overview¶
Date range picker allows the user to enter free text or select a dates from a calendar widget. and is based on the Angular Material datapicker component.
Examples¶
from dataclasses import field
from datetime import date
import mesop as me
@me.stateclass
class State:
picked_start_date: date | None = field(
default_factory=lambda: date(2024, 10, 1)
)
picked_end_date: date | None = field(
default_factory=lambda: date(2024, 11, 1)
)
def on_load(e: me.LoadEvent):
me.set_theme_mode("system")
@me.page(path="/date_range_picker", on_load=on_load)
def app():
state = me.state(State)
with me.box(
style=me.Style(
display="flex",
flex_direction="column",
gap=15,
padding=me.Padding.all(15),
)
):
me.date_range_picker(
label="Date Range",
disabled=False,
placeholder_start_date="9/1/2024",
placeholder_end_date="10/1/2024",
required=True,
start_date=state.picked_start_date,
end_date=state.picked_end_date,
readonly=False,
hide_required_marker=False,
color="accent",
float_label="always",
appearance="outline",
on_change=on_date_range_change,
)
me.text("Start date: " + _render_date(state.picked_start_date))
me.text("End date: " + _render_date(state.picked_end_date))
def on_date_range_change(e: me.DateRangePickerChangeEvent):
state = me.state(State)
state.picked_start_date = e.start_date
state.picked_end_date = e.end_date
def _render_date(maybe_date: date | None) -> str:
if maybe_date:
return maybe_date.strftime("%Y-%m-%d")
return "None"
API¶
date_range_picker
¶
Creates a date range picker component.
PARAMETER | DESCRIPTION |
---|---|
label |
Label for date range picker input.
TYPE:
|
on_change |
Fires when valid date values for both start/end have been specified through Calendar date selection or user input blur.
TYPE:
|
appearance |
The form field appearance style.
TYPE:
|
style |
Style for date range picker input.
TYPE:
|
disabled |
Whether it's disabled.
TYPE:
|
placeholder_start_date |
Start date placeholder value.
TYPE:
|
placeholder_end_date |
End date placeholder value.
TYPE:
|
required |
Whether it's required.
TYPE:
|
start_date |
Start date initial value.
TYPE:
|
end_date |
End date initial value.
TYPE:
|
readonly |
Whether the element is readonly.
TYPE:
|
hide_required_marker |
Whether the required marker should be hidden.
TYPE:
|
color |
The color palette for the form field.
TYPE:
|
float_label |
Whether the label should always float or float as the user types.
TYPE:
|
subscript_sizing |
Whether the form field should reserve space for one line of hint/error text (default) or to have the spacing grow from 0px as needed based on the size of the hint/error content. Note that when using dynamic sizing, layout shifts will occur when hint/error text changes.
TYPE:
|
hint_label |
Text for the form field hint.
TYPE:
|
key |
The component key.
TYPE:
|
DateRangePickerChangeEvent
dataclass
¶
Bases: MesopEvent
Represents a date range picker change event.
This event will only fire if start and end dates are valid dates.
ATTRIBUTE | DESCRIPTION |
---|---|
start_date |
Start date
TYPE:
|
end_date |
End date
TYPE:
|
key |
key of the component that emitted this event.
TYPE:
|