Add datetime to moment

This commit is contained in:
Alek Petuskey 2024-11-13 19:40:33 -08:00
parent 853a9d8614
commit 7c857f9c1f
3 changed files with 41 additions and 6 deletions

View File

@ -1,7 +1,8 @@
"""Moment component for humanized date rendering.""" """Moment component for humanized date rendering."""
import dataclasses import dataclasses
from typing import List, Optional import datetime
from typing import List, Optional, Union
from reflex.components.component import NoSSRComponent from reflex.components.component import NoSSRComponent
from reflex.event import EventHandler, passthrough_event_spec from reflex.event import EventHandler, passthrough_event_spec
@ -19,7 +20,7 @@ class MomentDelta:
weeks: Optional[int] = dataclasses.field(default=None) weeks: Optional[int] = dataclasses.field(default=None)
days: Optional[int] = dataclasses.field(default=None) days: Optional[int] = dataclasses.field(default=None)
hours: Optional[int] = dataclasses.field(default=None) hours: Optional[int] = dataclasses.field(default=None)
minutess: Optional[int] = dataclasses.field(default=None) minutes: Optional[int] = dataclasses.field(default=None)
seconds: Optional[int] = dataclasses.field(default=None) seconds: Optional[int] = dataclasses.field(default=None)
milliseconds: Optional[int] = dataclasses.field(default=None) milliseconds: Optional[int] = dataclasses.field(default=None)
@ -78,7 +79,7 @@ class Moment(NoSSRComponent):
duration: Var[str] duration: Var[str]
# The date to display (also work if passed as children). # The date to display (also work if passed as children).
date: Var[str] date: Var[Union[str, datetime.datetime, datetime.date]]
# Shows the duration (elapsed time) between now and the provided datetime. # Shows the duration (elapsed time) between now and the provided datetime.
duration_from_now: Var[bool] duration_from_now: Var[bool]

View File

@ -262,6 +262,9 @@ class Cartesian(Recharts):
# The layout of bar in the chart, usually inherited from parent. 'horizontal' | 'vertical' # The layout of bar in the chart, usually inherited from parent. 'horizontal' | 'vertical'
layout: Var[LiteralLayout] layout: Var[LiteralLayout]
# The source data, in which each element is an object.
data: Var[List[Dict[str, Any]]]
# The key of a group of data which should be unique in an area chart. # The key of a group of data which should be unique in an area chart.
data_key: Var[Union[str, int]] data_key: Var[Union[str, int]]
@ -656,11 +659,35 @@ class Reference(Recharts):
# Defines how to draw the reference line if it falls partly outside the canvas. If set to 'discard', the reference line will not be drawn at all. If set to 'hidden', the reference line will be clipped to the canvas. If set to 'visible', the reference line will be drawn completely. If set to 'extendDomain', the domain of the overflown axis will be extended such that the reference line fits into the canvas. Default: "discard" # Defines how to draw the reference line if it falls partly outside the canvas. If set to 'discard', the reference line will not be drawn at all. If set to 'hidden', the reference line will be clipped to the canvas. If set to 'visible', the reference line will be drawn completely. If set to 'extendDomain', the domain of the overflown axis will be extended such that the reference line fits into the canvas. Default: "discard"
if_overflow: Var[LiteralIfOverflow] if_overflow: Var[LiteralIfOverflow]
# If set true, the line will be rendered in front of bars in BarChart, etc. Default: False
is_front: Var[bool]
# If set a string or a number, default label will be drawn, and the option is content. # If set a string or a number, default label will be drawn, and the option is content.
label: Var[Union[str, int]] label: Var[Union[str, int]]
# If set true, the line will be rendered in front of bars in BarChart, etc. Default: False # The customized event handler of click on the component in this chart
is_front: Var[bool] on_click: EventHandler[no_args_event_spec]
# The customized event handler of mousedown on the component in this chart
on_mouse_down: EventHandler[no_args_event_spec]
# The customized event handler of mouseup on the component in this chart
on_mouse_up: EventHandler[no_args_event_spec]
# The customized event handler of mouseover on the component in this chart
on_mouse_over: EventHandler[no_args_event_spec]
# The customized event handler of mouseout on the component in this chart
on_mouse_out: EventHandler[no_args_event_spec]
# The customized event handler of mouseenter on the component in this chart
on_mouse_enter: EventHandler[no_args_event_spec]
# The customized event handler of mousemove on the component in this chart
on_mouse_move: EventHandler[no_args_event_spec]
# The customized event handler of mouseleave on the component in this chart
on_mouse_leave: EventHandler[no_args_event_spec]
class ReferenceLine(Reference): class ReferenceLine(Reference):

View File

@ -289,7 +289,14 @@ def serialize_datetime(dt: Union[date, datetime, time, timedelta]) -> str:
Returns: Returns:
The serialized datetime. The serialized datetime.
""" """
return str(dt) if isinstance(dt, datetime):
return dt.isoformat()
elif isinstance(dt, date):
return dt.isoformat()
elif isinstance(dt, time):
return dt.isoformat()
else: # timedelta
return str(dt)
@serializer(to=str) @serializer(to=str)