-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add serializers for different var types #1816
Conversation
reflex/components/graphing/plotly.py
Outdated
@@ -25,7 +25,7 @@ class Plotly(PlotlyLib): | |||
is_default = True | |||
|
|||
# The figure to display. This can be a plotly figure or a plotly data json. | |||
data: Var[Figure] | |||
data: Var[Any] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data: Var[Any] | |
data: Var["Figure"] |
And then at the top of the file, do something like
try:
from plotly.graph_objects import Figure
except ImportError:
Figure = Any
That way the type checking at least works nicer when you do have plotly installed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea
reflex/components/media/image.py
Outdated
try: | ||
from PIL.Image import Image as Img | ||
|
||
# If the src is an image, convert it to a base64 string. | ||
if types._isinstance(self.src, Img): | ||
self.src = Var.create(serialize(self.src)) # type: ignore | ||
except ImportError: | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need the import guard here? Doesn't seem like this block is actually using Img
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I use it in the isinstance
check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i guess i was thinking we can just call self.src = Var.create(serialize(self.src))
and not care about what self.src
is... looks like serialize
has a default str
handler.
reflex/utils/serializers.py
Outdated
registered_fn = SERIALIZERS.get(type_) | ||
if registered_fn is not None and registered_fn != fn: | ||
raise ValueError( | ||
f"Serializer for type {type_} is already registered as {registered_fn}." |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
registered_fn.__qualname__
might be useful here, in case the name is ambiguous
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
reflex/utils/serializers.py
Outdated
Returns: | ||
The serialized value, or None if a serializer is not found. | ||
""" | ||
global SERIALIZERS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
global SERIALIZERS |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed
|
||
|
||
@serializer | ||
def serialize_dict(prop: Dict[str, Any]) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can the existing format.format_dict
function just be decorated as @serializer
and then moved into this file later? the duplication of such complex functionality makes me nervous.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed the original function
2539631
to
eff6dfa
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
killer!
reflex/components/media/image.py
Outdated
try: | ||
from PIL.Image import Image as Img | ||
|
||
# If the src is an image, convert it to a base64 string. | ||
if types._isinstance(self.src, Img): | ||
self.src = Var.create(serialize(self.src)) # type: ignore | ||
except ImportError: | ||
pass |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i guess i was thinking we can just call self.src = Var.create(serialize(self.src))
and not care about what self.src
is... looks like serialize
has a default str
handler.
@@ -39,14 +43,22 @@ class Plotly(PlotlyLib): | |||
# If true, the graph will resize when the window is resized. | |||
use_resize_handler: Var[bool] | |||
|
|||
def _render(self) -> Tag: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Turns out this wasn't actually doing anything... We can reinvestigate how to add this logic back in.
def _render(self) -> Tag: | ||
self.src.is_string = True | ||
|
||
# Render the table. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# Render the table. | |
# Render the image. |
😉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will fix this in the follow up
Factored out var serialization so that it's not hardcoded within the compiler. The user can now define a serializer using the
@rx.serializer
decorator.Added serializers for Plotly, dict, and date time.