format.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. """Formatting operations."""
  2. from __future__ import annotations
  3. import base64
  4. import io
  5. import json
  6. import os
  7. import os.path as op
  8. import re
  9. import sys
  10. from typing import TYPE_CHECKING, Any, Type
  11. import plotly.graph_objects as go
  12. from plotly.io import to_json
  13. from reflex import constants
  14. from reflex.utils import types
  15. from reflex.vars import Var
  16. if TYPE_CHECKING:
  17. from reflex.components.component import ComponentStyle
  18. from reflex.event import EventChain, EventHandler, EventSpec
  19. WRAP_MAP = {
  20. "{": "}",
  21. "(": ")",
  22. "[": "]",
  23. "<": ">",
  24. '"': '"',
  25. "'": "'",
  26. "`": "`",
  27. }
  28. def get_close_char(open: str, close: str | None = None) -> str:
  29. """Check if the given character is a valid brace.
  30. Args:
  31. open: The open character.
  32. close: The close character if provided.
  33. Returns:
  34. The close character.
  35. Raises:
  36. ValueError: If the open character is not a valid brace.
  37. """
  38. if close is not None:
  39. return close
  40. if open not in WRAP_MAP:
  41. raise ValueError(f"Invalid wrap open: {open}, must be one of {WRAP_MAP.keys()}")
  42. return WRAP_MAP[open]
  43. def is_wrapped(text: str, open: str, close: str | None = None) -> bool:
  44. """Check if the given text is wrapped in the given open and close characters.
  45. Args:
  46. text: The text to check.
  47. open: The open character.
  48. close: The close character.
  49. Returns:
  50. Whether the text is wrapped.
  51. """
  52. close = get_close_char(open, close)
  53. return text.startswith(open) and text.endswith(close)
  54. def wrap(
  55. text: str,
  56. open: str,
  57. close: str | None = None,
  58. check_first: bool = True,
  59. num: int = 1,
  60. ) -> str:
  61. """Wrap the given text in the given open and close characters.
  62. Args:
  63. text: The text to wrap.
  64. open: The open character.
  65. close: The close character.
  66. check_first: Whether to check if the text is already wrapped.
  67. num: The number of times to wrap the text.
  68. Returns:
  69. The wrapped text.
  70. """
  71. close = get_close_char(open, close)
  72. # If desired, check if the text is already wrapped in braces.
  73. if check_first and is_wrapped(text=text, open=open, close=close):
  74. return text
  75. # Wrap the text in braces.
  76. return f"{open * num}{text}{close * num}"
  77. def indent(text: str, indent_level: int = 2) -> str:
  78. """Indent the given text by the given indent level.
  79. Args:
  80. text: The text to indent.
  81. indent_level: The indent level.
  82. Returns:
  83. The indented text.
  84. """
  85. lines = text.splitlines()
  86. if len(lines) < 2:
  87. return text
  88. return os.linesep.join(f"{' ' * indent_level}{line}" for line in lines) + os.linesep
  89. def to_snake_case(text: str) -> str:
  90. """Convert a string to snake case.
  91. The words in the text are converted to lowercase and
  92. separated by underscores.
  93. Args:
  94. text: The string to convert.
  95. Returns:
  96. The snake case string.
  97. """
  98. s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", text)
  99. return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()
  100. def to_camel_case(text: str) -> str:
  101. """Convert a string to camel case.
  102. The first word in the text is converted to lowercase and
  103. the rest of the words are converted to title case, removing underscores.
  104. Args:
  105. text: The string to convert.
  106. Returns:
  107. The camel case string.
  108. """
  109. if "_" not in text:
  110. return text
  111. camel = "".join(
  112. word.capitalize() if i > 0 else word.lower()
  113. for i, word in enumerate(text.lstrip("_").split("_"))
  114. )
  115. prefix = "_" if text.startswith("_") else ""
  116. return prefix + camel
  117. def to_title_case(text: str) -> str:
  118. """Convert a string from snake case to title case.
  119. Args:
  120. text: The string to convert.
  121. Returns:
  122. The title case string.
  123. """
  124. return "".join(word.capitalize() for word in text.split("_"))
  125. def format_string(string: str) -> str:
  126. """Format the given string as a JS string literal..
  127. Args:
  128. string: The string to format.
  129. Returns:
  130. The formatted string.
  131. """
  132. # Escape backticks.
  133. string = string.replace(r"\`", "`")
  134. string = string.replace("`", r"\`")
  135. # Wrap the string so it looks like {`string`}.
  136. string = wrap(string, "`")
  137. string = wrap(string, "{")
  138. return string
  139. def format_var(var: Var) -> str:
  140. """Format the given Var as a javascript value.
  141. Args:
  142. var: The Var to format.
  143. Returns:
  144. The formatted Var.
  145. """
  146. if not var.is_local or var.is_string:
  147. return str(var)
  148. if types._issubclass(var.type_, str):
  149. return format_string(var.full_name)
  150. if is_wrapped(var.full_name, "{"):
  151. return var.full_name
  152. return json_dumps(var.full_name)
  153. def format_route(route: str) -> str:
  154. """Format the given route.
  155. Args:
  156. route: The route to format.
  157. Returns:
  158. The formatted route.
  159. """
  160. # Strip the route.
  161. route = route.strip("/")
  162. route = to_snake_case(route).replace("_", "-")
  163. # If the route is empty, return the index route.
  164. if route == "":
  165. return constants.INDEX_ROUTE
  166. return route
  167. def format_cond(
  168. cond: str,
  169. true_value: str,
  170. false_value: str = '""',
  171. is_prop=False,
  172. ) -> str:
  173. """Format a conditional expression.
  174. Args:
  175. cond: The cond.
  176. true_value: The value to return if the cond is true.
  177. false_value: The value to return if the cond is false.
  178. is_prop: Whether the cond is a prop
  179. Returns:
  180. The formatted conditional expression.
  181. """
  182. # Import here to avoid circular imports.
  183. from reflex.vars import Var
  184. # Use Python truthiness.
  185. cond = f"isTrue({cond})"
  186. # Format prop conds.
  187. if is_prop:
  188. prop1 = Var.create_safe(true_value, is_string=type(true_value) is str).set(
  189. is_local=True
  190. ) # type: ignore
  191. prop2 = Var.create_safe(false_value, is_string=type(false_value) is str).set(
  192. is_local=True
  193. ) # type: ignore
  194. return f"{cond} ? {prop1} : {prop2}".replace("{", "").replace("}", "")
  195. # Format component conds.
  196. return wrap(f"{cond} ? {true_value} : {false_value}", "{")
  197. def get_event_handler_parts(handler: EventHandler) -> tuple[str, str]:
  198. """Get the state and function name of an event handler.
  199. Args:
  200. handler: The event handler to get the parts of.
  201. Returns:
  202. The state and function name.
  203. """
  204. # Get the class that defines the event handler.
  205. parts = handler.fn.__qualname__.split(".")
  206. # If there's no enclosing class, just return the function name.
  207. if len(parts) == 1:
  208. return ("", parts[-1])
  209. # Get the state and the function name.
  210. state_name, name = parts[-2:]
  211. # Construct the full event handler name.
  212. try:
  213. # Try to get the state from the module.
  214. state = vars(sys.modules[handler.fn.__module__])[state_name]
  215. except Exception:
  216. # If the state isn't in the module, just return the function name.
  217. return ("", handler.fn.__qualname__)
  218. return (state.get_full_name(), name)
  219. def format_event_handler(handler: EventHandler) -> str:
  220. """Format an event handler.
  221. Args:
  222. handler: The event handler to format.
  223. Returns:
  224. The formatted function.
  225. """
  226. state, name = get_event_handler_parts(handler)
  227. if state == "":
  228. return name
  229. return f"{state}.{name}"
  230. def format_event(event_spec: EventSpec) -> str:
  231. """Format an event.
  232. Args:
  233. event_spec: The event to format.
  234. Returns:
  235. The compiled event.
  236. """
  237. args = ",".join(
  238. [
  239. ":".join(
  240. (name.name, json.dumps(val.name) if val.is_string else val.full_name)
  241. )
  242. for name, val in event_spec.args
  243. ]
  244. )
  245. event_args = [
  246. wrap(format_event_handler(event_spec.handler), '"'),
  247. ]
  248. event_args.append(wrap(args, "{"))
  249. if event_spec.client_handler_name:
  250. event_args.append(wrap(event_spec.client_handler_name, '"'))
  251. return f"E({', '.join(event_args)})"
  252. def format_event_chain(
  253. event_chain: EventChain | Var[EventChain],
  254. event_arg: Var | None = None,
  255. ) -> str:
  256. """Format an event chain as a javascript invocation.
  257. Args:
  258. event_chain: The event chain to queue on the frontend.
  259. event_arg: The browser-native event (only used to preventDefault).
  260. Returns:
  261. Compiled javascript code to queue the given event chain on the frontend.
  262. Raises:
  263. ValueError: When the given event chain is not a valid event chain.
  264. """
  265. if isinstance(event_chain, Var):
  266. from reflex.event import EventChain
  267. if event_chain.type_ is not EventChain:
  268. raise ValueError(f"Invalid event chain: {event_chain}")
  269. return "".join(
  270. [
  271. "(() => {",
  272. format_var(event_chain),
  273. f"; preventDefault({format_var(event_arg)})" if event_arg else "",
  274. "})()",
  275. ]
  276. )
  277. chain = ",".join([format_event(event) for event in event_chain.events])
  278. return "".join(
  279. [
  280. f"Event([{chain}]",
  281. f", {format_var(event_arg)}" if event_arg else "",
  282. ")",
  283. ]
  284. )
  285. def format_query_params(router_data: dict[str, Any]) -> dict[str, str]:
  286. """Convert back query params name to python-friendly case.
  287. Args:
  288. router_data: the router_data dict containing the query params
  289. Returns:
  290. The reformatted query params
  291. """
  292. params = router_data[constants.RouteVar.QUERY]
  293. return {k.replace("-", "_"): v for k, v in params.items()}
  294. def format_dataframe_values(value: Type) -> list[Any]:
  295. """Format dataframe values.
  296. Args:
  297. value: The value to format.
  298. Returns:
  299. Format data
  300. """
  301. if not types.is_dataframe(type(value)):
  302. return value
  303. format_data = []
  304. for data in list(value.values.tolist()):
  305. element = []
  306. for d in data:
  307. element.append(str(d) if isinstance(d, (list, tuple)) else d)
  308. format_data.append(element)
  309. return format_data
  310. def format_image_data(value: Type) -> str:
  311. """Format image data.
  312. Args:
  313. value: The value to format.
  314. Returns:
  315. Format data
  316. """
  317. buff = io.BytesIO()
  318. value.save(buff, format="PNG")
  319. image_bytes = buff.getvalue()
  320. base64_image = base64.b64encode(image_bytes).decode("utf-8")
  321. return f"data:image/png;base64,{base64_image}"
  322. def format_state(value: Any) -> Any:
  323. """Recursively format values in the given state.
  324. Args:
  325. value: The state to format.
  326. Returns:
  327. The formatted state.
  328. Raises:
  329. TypeError: If the given value is not a valid state.
  330. """
  331. # Handle dicts.
  332. if isinstance(value, dict):
  333. return {k: format_state(v) for k, v in value.items()}
  334. # Handle lists, sets, typles.
  335. if isinstance(value, types.StateIterBases):
  336. return [format_state(v) for v in value]
  337. # Return state vars as is.
  338. if isinstance(value, types.StateBases):
  339. return value
  340. # Convert plotly figures to JSON.
  341. if isinstance(value, go.Figure):
  342. return json.loads(to_json(value))["data"] # type: ignore
  343. # Convert pandas dataframes to JSON.
  344. if types.is_dataframe(type(value)):
  345. return {
  346. "columns": value.columns.tolist(),
  347. "data": format_dataframe_values(value),
  348. }
  349. # Convert Image objects to base64.
  350. if types.is_image(type(value)):
  351. return format_image_data(value) # type: ignore
  352. raise TypeError(
  353. "State vars must be primitive Python types, "
  354. "or subclasses of rx.Base. "
  355. f"Got var of type {type(value)}."
  356. )
  357. def format_ref(ref: str) -> str:
  358. """Format a ref.
  359. Args:
  360. ref: The ref to format.
  361. Returns:
  362. The formatted ref.
  363. """
  364. # Replace all non-word characters with underscores.
  365. clean_ref = re.sub(r"[^\w]+", "_", ref)
  366. return f"ref_{clean_ref}"
  367. def format_array_ref(refs: str, idx) -> str:
  368. """Format a ref accessed by array.
  369. Args:
  370. refs : The ref array to access.
  371. idx : The index of the ref in the array.
  372. Returns:
  373. The formatted ref.
  374. """
  375. clean_ref = re.sub(r"[^\w]+", "_", refs)
  376. if idx:
  377. idx.is_local = True
  378. return f"refs_{clean_ref}[{idx}]"
  379. else:
  380. return f"refs_{clean_ref}"
  381. def format_dict(prop: ComponentStyle) -> str:
  382. """Format a dict with vars potentially as values.
  383. Args:
  384. prop: The dict to format.
  385. Returns:
  386. The formatted dict.
  387. """
  388. # Import here to avoid circular imports.
  389. from reflex.vars import Var
  390. # Convert any var keys to strings.
  391. prop = {key: str(val) if isinstance(val, Var) else val for key, val in prop.items()}
  392. # Dump the dict to a string.
  393. fprop = json_dumps(prop)
  394. def unescape_double_quotes_in_var(m: re.Match) -> str:
  395. # Since the outer quotes are removed, the inner escaped quotes must be unescaped.
  396. return re.sub('\\\\"', '"', m.group(1))
  397. # This substitution is necessary to unwrap var values.
  398. fprop = re.sub(
  399. pattern=r"""
  400. (?<!\\) # must NOT start with a backslash
  401. " # match opening double quote of JSON value
  402. {(.*?)} # extract the value between curly braces (non-greedy)
  403. " # match must end with an unescaped double quote
  404. """,
  405. repl=unescape_double_quotes_in_var,
  406. string=fprop,
  407. flags=re.VERBOSE,
  408. )
  409. # Return the formatted dict.
  410. return fprop
  411. def format_breadcrumbs(route: str) -> list[tuple[str, str]]:
  412. """Take a route and return a list of tuple for use in breadcrumb.
  413. Args:
  414. route: The route to transform.
  415. Returns:
  416. list[tuple[str, str]]: the list of tuples for the breadcrumb.
  417. """
  418. route_parts = route.lstrip("/").split("/")
  419. # create and return breadcrumbs
  420. return [
  421. (part, op.join("/", *route_parts[: i + 1]))
  422. for i, part in enumerate(route_parts)
  423. ]
  424. def json_dumps(obj: Any) -> str:
  425. """Takes an object and returns a jsonified string.
  426. Args:
  427. obj: The object to be serialized.
  428. Returns:
  429. A string
  430. """
  431. return json.dumps(obj, ensure_ascii=False, default=list)