format.py 16 KB

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