From 7c857f9c1fc5f6b0e1a57dab640214dfec1530a3 Mon Sep 17 00:00:00 2001 From: Alek Petuskey Date: Wed, 13 Nov 2024 19:40:33 -0800 Subject: [PATCH] Add datetime to moment --- reflex/components/moment/moment.py | 7 +++--- reflex/components/recharts/cartesian.py | 31 +++++++++++++++++++++++-- reflex/utils/serializers.py | 9 ++++++- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/reflex/components/moment/moment.py b/reflex/components/moment/moment.py index d5d6d8f7c..da14cf6f6 100644 --- a/reflex/components/moment/moment.py +++ b/reflex/components/moment/moment.py @@ -1,7 +1,8 @@ """Moment component for humanized date rendering.""" import dataclasses -from typing import List, Optional +import datetime +from typing import List, Optional, Union from reflex.components.component import NoSSRComponent from reflex.event import EventHandler, passthrough_event_spec @@ -19,7 +20,7 @@ class MomentDelta: weeks: Optional[int] = dataclasses.field(default=None) days: 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) milliseconds: Optional[int] = dataclasses.field(default=None) @@ -78,7 +79,7 @@ class Moment(NoSSRComponent): duration: Var[str] # 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. duration_from_now: Var[bool] diff --git a/reflex/components/recharts/cartesian.py b/reflex/components/recharts/cartesian.py index 028bcb4e4..6c0410d45 100644 --- a/reflex/components/recharts/cartesian.py +++ b/reflex/components/recharts/cartesian.py @@ -262,6 +262,9 @@ class Cartesian(Recharts): # The layout of bar in the chart, usually inherited from parent. 'horizontal' | 'vertical' 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. 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" 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. label: Var[Union[str, int]] - # If set true, the line will be rendered in front of bars in BarChart, etc. Default: False - is_front: Var[bool] + # The customized event handler of click on the component in this chart + 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): diff --git a/reflex/utils/serializers.py b/reflex/utils/serializers.py index b87909aec..93fadd5db 100644 --- a/reflex/utils/serializers.py +++ b/reflex/utils/serializers.py @@ -289,7 +289,14 @@ def serialize_datetime(dt: Union[date, datetime, time, timedelta]) -> str: Returns: 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)