serializers.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. """Serializers used to convert Var types to JSON strings."""
  2. from __future__ import annotations
  3. import json
  4. import types as builtin_types
  5. import warnings
  6. from datetime import date, datetime, time, timedelta
  7. from enum import Enum
  8. from typing import Any, Callable, Dict, List, Set, Tuple, Type, Union, get_type_hints
  9. from reflex.base import Base
  10. from reflex.constants.colors import Color, format_color
  11. from reflex.utils import exceptions, format, types
  12. # Mapping from type to a serializer.
  13. # The serializer should convert the type to a JSON object.
  14. SerializedType = Union[str, bool, int, float, list, dict]
  15. Serializer = Callable[[Type], SerializedType]
  16. SERIALIZERS: dict[Type, Serializer] = {}
  17. def serializer(fn: Serializer) -> Serializer:
  18. """Decorator to add a serializer for a given type.
  19. Args:
  20. fn: The function to decorate.
  21. Returns:
  22. The decorated function.
  23. Raises:
  24. ValueError: If the function does not take a single argument.
  25. """
  26. # Get the global serializers.
  27. global SERIALIZERS
  28. # Check the type hints to get the type of the argument.
  29. type_hints = get_type_hints(fn)
  30. args = [arg for arg in type_hints if arg != "return"]
  31. # Make sure the function takes a single argument.
  32. if len(args) != 1:
  33. raise ValueError("Serializer must take a single argument.")
  34. # Get the type of the argument.
  35. type_ = type_hints[args[0]]
  36. # Make sure the type is not already registered.
  37. registered_fn = SERIALIZERS.get(type_)
  38. if registered_fn is not None and registered_fn != fn:
  39. raise ValueError(
  40. f"Serializer for type {type_} is already registered as {registered_fn.__qualname__}."
  41. )
  42. # Register the serializer.
  43. SERIALIZERS[type_] = fn
  44. # Return the function.
  45. return fn
  46. def serialize(value: Any) -> SerializedType | None:
  47. """Serialize the value to a JSON string.
  48. Args:
  49. value: The value to serialize.
  50. Returns:
  51. The serialized value, or None if a serializer is not found.
  52. """
  53. # Get the serializer for the type.
  54. serializer = get_serializer(type(value))
  55. # If there is no serializer, return None.
  56. if serializer is None:
  57. return None
  58. # Serialize the value.
  59. return serializer(value)
  60. def get_serializer(type_: Type) -> Serializer | None:
  61. """Get the serializer for the type.
  62. Args:
  63. type_: The type to get the serializer for.
  64. Returns:
  65. The serializer for the type, or None if there is no serializer.
  66. """
  67. global SERIALIZERS
  68. # First, check if the type is registered.
  69. serializer = SERIALIZERS.get(type_)
  70. if serializer is not None:
  71. return serializer
  72. # If the type is not registered, check if it is a subclass of a registered type.
  73. for registered_type, serializer in reversed(SERIALIZERS.items()):
  74. if types._issubclass(type_, registered_type):
  75. return serializer
  76. # If there is no serializer, return None.
  77. return None
  78. def has_serializer(type_: Type) -> bool:
  79. """Check if there is a serializer for the type.
  80. Args:
  81. type_: The type to check.
  82. Returns:
  83. Whether there is a serializer for the type.
  84. """
  85. return get_serializer(type_) is not None
  86. @serializer
  87. def serialize_type(value: type) -> str:
  88. """Serialize a python type.
  89. Args:
  90. value: the type to serialize.
  91. Returns:
  92. The serialized type.
  93. """
  94. return value.__name__
  95. @serializer
  96. def serialize_str(value: str) -> str:
  97. """Serialize a string.
  98. Args:
  99. value: The string to serialize.
  100. Returns:
  101. The serialized string.
  102. """
  103. return value
  104. @serializer
  105. def serialize_primitive(value: Union[bool, int, float, None]) -> str:
  106. """Serialize a primitive type.
  107. Args:
  108. value: The number/bool/None to serialize.
  109. Returns:
  110. The serialized number/bool/None.
  111. """
  112. return format.json_dumps(value)
  113. @serializer
  114. def serialize_base(value: Base) -> str:
  115. """Serialize a Base instance.
  116. Args:
  117. value : The Base to serialize.
  118. Returns:
  119. The serialized Base.
  120. """
  121. return value.json()
  122. @serializer
  123. def serialize_list(value: Union[List, Tuple, Set]) -> str:
  124. """Serialize a list to a JSON string.
  125. Args:
  126. value: The list to serialize.
  127. Returns:
  128. The serialized list.
  129. """
  130. # Dump the list to a string.
  131. fprop = format.json_dumps(list(value))
  132. # Unwrap var values.
  133. return format.unwrap_vars(fprop)
  134. @serializer
  135. def serialize_dict(prop: Dict[str, Any]) -> str:
  136. """Serialize a dictionary to a JSON string.
  137. Args:
  138. prop: The dictionary to serialize.
  139. Returns:
  140. The serialized dictionary.
  141. Raises:
  142. InvalidStylePropError: If the style prop is invalid.
  143. """
  144. # Import here to avoid circular imports.
  145. from reflex.event import EventHandler
  146. prop_dict = {}
  147. for key, value in prop.items():
  148. if types._issubclass(type(value), Callable):
  149. raise exceptions.InvalidStylePropError(
  150. f"The style prop `{format.to_snake_case(key)}` cannot have " # type: ignore
  151. f"`{value.fn.__qualname__ if isinstance(value, EventHandler) else value.__qualname__ if isinstance(value, builtin_types.FunctionType) else value}`, "
  152. f"an event handler or callable as its value"
  153. )
  154. prop_dict[key] = value
  155. # Dump the dict to a string.
  156. fprop = format.json_dumps(prop_dict)
  157. # Unwrap var values.
  158. return format.unwrap_vars(fprop)
  159. @serializer
  160. def serialize_datetime(dt: Union[date, datetime, time, timedelta]) -> str:
  161. """Serialize a datetime to a JSON string.
  162. Args:
  163. dt: The datetime to serialize.
  164. Returns:
  165. The serialized datetime.
  166. """
  167. return str(dt)
  168. @serializer
  169. def serialize_enum(en: Enum) -> str:
  170. """Serialize a enum to a JSON string.
  171. Args:
  172. en: The enum to serialize.
  173. Returns:
  174. The serialized enum.
  175. """
  176. return en.value
  177. @serializer
  178. def serialize_color(color: Color) -> str:
  179. """Serialize a color.
  180. Args:
  181. color: The color to serialize.
  182. Returns:
  183. The serialized color.
  184. """
  185. return format_color(color.color, color.shade, color.alpha)
  186. try:
  187. from pandas import DataFrame
  188. def format_dataframe_values(df: DataFrame) -> List[List[Any]]:
  189. """Format dataframe values to a list of lists.
  190. Args:
  191. df: The dataframe to format.
  192. Returns:
  193. The dataframe as a list of lists.
  194. """
  195. return [
  196. [str(d) if isinstance(d, (list, tuple)) else d for d in data]
  197. for data in list(df.values.tolist())
  198. ]
  199. @serializer
  200. def serialize_dataframe(df: DataFrame) -> dict:
  201. """Serialize a pandas dataframe.
  202. Args:
  203. df: The dataframe to serialize.
  204. Returns:
  205. The serialized dataframe.
  206. """
  207. return {
  208. "columns": df.columns.tolist(),
  209. "data": format_dataframe_values(df),
  210. }
  211. except ImportError:
  212. pass
  213. try:
  214. from plotly.graph_objects import Figure
  215. from plotly.io import to_json
  216. @serializer
  217. def serialize_figure(figure: Figure) -> list:
  218. """Serialize a plotly figure.
  219. Args:
  220. figure: The figure to serialize.
  221. Returns:
  222. The serialized figure.
  223. """
  224. return json.loads(str(to_json(figure)))["data"]
  225. except ImportError:
  226. pass
  227. try:
  228. import base64
  229. import io
  230. from PIL.Image import MIME
  231. from PIL.Image import Image as Img
  232. @serializer
  233. def serialize_image(image: Img) -> str:
  234. """Serialize a plotly figure.
  235. Args:
  236. image: The image to serialize.
  237. Returns:
  238. The serialized image.
  239. """
  240. buff = io.BytesIO()
  241. image_format = getattr(image, "format", None) or "PNG"
  242. image.save(buff, format=image_format)
  243. image_bytes = buff.getvalue()
  244. base64_image = base64.b64encode(image_bytes).decode("utf-8")
  245. try:
  246. # Newer method to get the mime type, but does not always work.
  247. mime_type = image.get_format_mimetype() # type: ignore
  248. except AttributeError:
  249. try:
  250. # Fallback method
  251. mime_type = MIME[image_format]
  252. except KeyError:
  253. # Unknown mime_type: warn and return image/png and hope the browser can sort it out.
  254. warnings.warn(
  255. f"Unknown mime type for {image} {image_format}. Defaulting to image/png"
  256. )
  257. mime_type = "image/png"
  258. return f"data:{mime_type};base64,{base64_image}"
  259. except ImportError:
  260. pass