utils.py 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219
  1. """General utility functions."""
  2. from __future__ import annotations
  3. import inspect
  4. import json
  5. import os
  6. import platform
  7. import random
  8. import re
  9. import shutil
  10. import signal
  11. import string
  12. import subprocess
  13. import sys
  14. from collections import defaultdict
  15. from pathlib import Path
  16. from subprocess import PIPE
  17. from types import ModuleType
  18. from typing import _GenericAlias # type: ignore
  19. from typing import (
  20. TYPE_CHECKING,
  21. Any,
  22. Callable,
  23. Dict,
  24. List,
  25. Optional,
  26. Tuple,
  27. Type,
  28. Union,
  29. )
  30. from urllib.parse import urlparse
  31. import plotly.graph_objects as go
  32. import typer
  33. import uvicorn
  34. from plotly.io import to_json
  35. from redis import Redis
  36. from rich.console import Console
  37. from pynecone import constants
  38. from pynecone.base import Base
  39. if TYPE_CHECKING:
  40. from pynecone.app import App
  41. from pynecone.components.component import ImportDict
  42. from pynecone.config import Config
  43. from pynecone.event import Event, EventHandler, EventSpec
  44. from pynecone.var import Var
  45. # Shorthand for join.
  46. join = os.linesep.join
  47. # Console for pretty printing.
  48. console = Console()
  49. # Union of generic types.
  50. GenericType = Union[Type, _GenericAlias]
  51. # Valid state var types.
  52. PrimitiveType = Union[int, float, bool, str, list, dict, tuple]
  53. StateVar = Union[PrimitiveType, Base, None]
  54. def get_args(alias: _GenericAlias) -> Tuple[Type, ...]:
  55. """Get the arguments of a type alias.
  56. Args:
  57. alias: The type alias.
  58. Returns:
  59. The arguments of the type alias.
  60. """
  61. return alias.__args__
  62. def is_generic_alias(cls: GenericType) -> bool:
  63. """Check whether the class is a generic alias.
  64. Args:
  65. cls: The class to check.
  66. Returns:
  67. Whether the class is a generic alias.
  68. """
  69. # For older versions of Python.
  70. if isinstance(cls, _GenericAlias):
  71. return True
  72. try:
  73. from typing import _SpecialGenericAlias # type: ignore
  74. if isinstance(cls, _SpecialGenericAlias):
  75. return True
  76. except ImportError:
  77. pass
  78. # For newer versions of Python.
  79. try:
  80. from types import GenericAlias # type: ignore
  81. return isinstance(cls, GenericAlias)
  82. except ImportError:
  83. return False
  84. def is_union(cls: GenericType) -> bool:
  85. """Check if a class is a Union.
  86. Args:
  87. cls: The class to check.
  88. Returns:
  89. Whether the class is a Union.
  90. """
  91. try:
  92. from typing import _UnionGenericAlias # type: ignore
  93. return isinstance(cls, _UnionGenericAlias)
  94. except ImportError:
  95. pass
  96. if is_generic_alias(cls):
  97. return cls.__origin__ == Union
  98. return False
  99. def get_base_class(cls: GenericType) -> Type:
  100. """Get the base class of a class.
  101. Args:
  102. cls: The class.
  103. Returns:
  104. The base class of the class.
  105. """
  106. if is_union(cls):
  107. return tuple(get_base_class(arg) for arg in get_args(cls))
  108. if is_generic_alias(cls):
  109. return get_base_class(cls.__origin__)
  110. return cls
  111. def _issubclass(cls: GenericType, cls_check: GenericType) -> bool:
  112. """Check if a class is a subclass of another class.
  113. Args:
  114. cls: The class to check.
  115. cls_check: The class to check against.
  116. Returns:
  117. Whether the class is a subclass of the other class.
  118. """
  119. # Special check for Any.
  120. if cls_check == Any:
  121. return True
  122. if cls == Any or cls == Callable:
  123. return False
  124. cls_base = get_base_class(cls)
  125. cls_check_base = get_base_class(cls_check)
  126. return cls_check_base == Any or issubclass(cls_base, cls_check_base)
  127. def _isinstance(obj: Any, cls: GenericType) -> bool:
  128. """Check if an object is an instance of a class.
  129. Args:
  130. obj: The object to check.
  131. cls: The class to check against.
  132. Returns:
  133. Whether the object is an instance of the class.
  134. """
  135. return isinstance(obj, get_base_class(cls))
  136. def rm(path: str):
  137. """Remove a file or directory.
  138. Args:
  139. path: The path to the file or directory.
  140. """
  141. if os.path.isdir(path):
  142. shutil.rmtree(path)
  143. elif os.path.isfile(path):
  144. os.remove(path)
  145. def cp(src: str, dest: str, overwrite: bool = True) -> bool:
  146. """Copy a file or directory.
  147. Args:
  148. src: The path to the file or directory.
  149. dest: The path to the destination.
  150. overwrite: Whether to overwrite the destination.
  151. Returns:
  152. Whether the copy was successful.
  153. """
  154. if src == dest:
  155. return False
  156. if not overwrite and os.path.exists(dest):
  157. return False
  158. if os.path.isdir(src):
  159. rm(dest)
  160. shutil.copytree(src, dest)
  161. else:
  162. shutil.copyfile(src, dest)
  163. return True
  164. def mv(src: str, dest: str, overwrite: bool = True) -> bool:
  165. """Move a file or directory.
  166. Args:
  167. src: The path to the file or directory.
  168. dest: The path to the destination.
  169. overwrite: Whether to overwrite the destination.
  170. Returns:
  171. Whether the move was successful.
  172. """
  173. if src == dest:
  174. return False
  175. if not overwrite and os.path.exists(dest):
  176. return False
  177. rm(dest)
  178. shutil.move(src, dest)
  179. return True
  180. def mkdir(path: str):
  181. """Create a directory.
  182. Args:
  183. path: The path to the directory.
  184. """
  185. if not os.path.exists(path):
  186. os.makedirs(path)
  187. def ln(src: str, dest: str, overwrite: bool = False) -> bool:
  188. """Create a symbolic link.
  189. Args:
  190. src: The path to the file or directory.
  191. dest: The path to the destination.
  192. overwrite: Whether to overwrite the destination.
  193. Returns:
  194. Whether the link was successful.
  195. """
  196. if src == dest:
  197. return False
  198. if not overwrite and (os.path.exists(dest) or os.path.islink(dest)):
  199. return False
  200. if os.path.isdir(src):
  201. rm(dest)
  202. os.symlink(src, dest, target_is_directory=True)
  203. else:
  204. os.symlink(src, dest)
  205. return True
  206. def kill(pid):
  207. """Kill a process.
  208. Args:
  209. pid: The process ID.
  210. """
  211. os.kill(pid, signal.SIGTERM)
  212. def which(program: str) -> Optional[str]:
  213. """Find the path to an executable.
  214. Args:
  215. program: The name of the executable.
  216. Returns:
  217. The path to the executable.
  218. """
  219. return shutil.which(program)
  220. def get_config() -> Config:
  221. """Get the app config.
  222. Returns:
  223. The app config.
  224. """
  225. from pynecone.config import Config
  226. sys.path.append(os.getcwd())
  227. try:
  228. return __import__(constants.CONFIG_MODULE).config
  229. except ImportError:
  230. return Config(app_name="")
  231. def check_node_version(min_version):
  232. """Check the version of Node.js.
  233. Args:
  234. min_version: The minimum version of Node.js required.
  235. Returns:
  236. Whether the version of Node.js is high enough.
  237. """
  238. try:
  239. # Run the node -v command and capture the output
  240. result = subprocess.run(
  241. ["node", "-v"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
  242. )
  243. # The output will be in the form "vX.Y.Z", so we can split it on the "v" character and take the second part
  244. version = result.stdout.decode().strip().split("v")[1]
  245. # Compare the version numbers
  246. return version.split(".") >= min_version.split(".")
  247. except Exception as e:
  248. return False
  249. def get_package_manager() -> str:
  250. """Get the package manager executable.
  251. Returns:
  252. The path to the package manager.
  253. Raises:
  254. FileNotFoundError: If bun or npm is not installed.
  255. Exit: If the app directory is invalid.
  256. """
  257. # Check that the node version is valid.
  258. if not check_node_version(constants.MIN_NODE_VERSION):
  259. console.print(
  260. f"[red]Node.js version {constants.MIN_NODE_VERSION} or higher is required to run Pynecone."
  261. )
  262. raise typer.Exit()
  263. # On Windows, we use npm instead of bun.
  264. if platform.system() == "Windows":
  265. npm_path = which("npm")
  266. if npm_path is None:
  267. raise FileNotFoundError("Pynecone requires npm to be installed on Windows.")
  268. return npm_path
  269. # On other platforms, we use bun.
  270. return os.path.expandvars(get_config().bun_path)
  271. def get_app() -> ModuleType:
  272. """Get the app module based on the default config.
  273. Returns:
  274. The app based on the default config.
  275. """
  276. config = get_config()
  277. module = ".".join([config.app_name, config.app_name])
  278. app = __import__(module, fromlist=(constants.APP_VAR,))
  279. return app
  280. def create_config(app_name: str):
  281. """Create a new pcconfig file.
  282. Args:
  283. app_name: The name of the app.
  284. """
  285. # Import here to avoid circular imports.
  286. from pynecone.compiler import templates
  287. with open(constants.CONFIG_FILE, "w") as f:
  288. f.write(templates.PCCONFIG.format(app_name=app_name))
  289. def initialize_gitignore():
  290. """Initialize the template .gitignore file."""
  291. # The files to add to the .gitignore file.
  292. files = constants.DEFAULT_GITIGNORE
  293. # Subtract current ignored files.
  294. if os.path.exists(constants.GITIGNORE_FILE):
  295. with open(constants.GITIGNORE_FILE, "r") as f:
  296. files -= set(f.read().splitlines())
  297. # Add the new files to the .gitignore file.
  298. with open(constants.GITIGNORE_FILE, "a") as f:
  299. f.write(join(files))
  300. def initialize_app_directory(app_name: str):
  301. """Initialize the app directory on pc init.
  302. Args:
  303. app_name: The name of the app.
  304. """
  305. console.log("Initializing the app directory.")
  306. cp(constants.APP_TEMPLATE_DIR, app_name)
  307. mv(
  308. os.path.join(app_name, constants.APP_TEMPLATE_FILE),
  309. os.path.join(app_name, app_name + constants.PY_EXT),
  310. )
  311. cp(constants.ASSETS_TEMPLATE_DIR, constants.APP_ASSETS_DIR)
  312. def initialize_web_directory():
  313. """Initialize the web directory on pc init."""
  314. console.log("Initializing the web directory.")
  315. rm(os.path.join(constants.WEB_TEMPLATE_DIR, constants.NODE_MODULES))
  316. rm(os.path.join(constants.WEB_TEMPLATE_DIR, constants.PACKAGE_LOCK))
  317. cp(constants.WEB_TEMPLATE_DIR, constants.WEB_DIR)
  318. def install_bun():
  319. """Install bun onto the user's system."""
  320. # Bun is not supported on Windows.
  321. if platform.system() == "Windows":
  322. console.log("Skipping bun installation on Windows.")
  323. return
  324. # Only install if bun is not already installed.
  325. if not os.path.exists(get_package_manager()):
  326. console.log("Installing bun...")
  327. os.system(constants.INSTALL_BUN)
  328. def install_frontend_packages():
  329. """Install the frontend packages."""
  330. # Install the base packages.
  331. subprocess.run(
  332. [get_package_manager(), "install"], cwd=constants.WEB_DIR, stdout=PIPE
  333. )
  334. # Install the app packages.
  335. packages = get_config().frontend_packages
  336. if len(packages) > 0:
  337. subprocess.run(
  338. [get_package_manager(), "add", *packages],
  339. cwd=constants.WEB_DIR,
  340. stdout=PIPE,
  341. )
  342. def is_initialized() -> bool:
  343. """Check whether the app is initialized.
  344. Returns:
  345. Whether the app is initialized in the current directory.
  346. """
  347. return os.path.exists(constants.CONFIG_FILE) and os.path.exists(constants.WEB_DIR)
  348. def is_latest_template() -> bool:
  349. """Whether the app is using the latest template.
  350. Returns:
  351. Whether the app is using the latest template.
  352. """
  353. template_version = open(constants.PCVERSION_TEMPLATE_FILE).read()
  354. if not os.path.exists(constants.PCVERSION_APP_FILE):
  355. return False
  356. app_version = open(constants.PCVERSION_APP_FILE).read()
  357. return app_version >= template_version
  358. def export_app(app: App, zip: bool = False):
  359. """Zip up the app for deployment.
  360. Args:
  361. app: The app.
  362. zip: Whether to zip the app.
  363. """
  364. # Force compile the app.
  365. app.compile(force_compile=True)
  366. # Remove the static folder.
  367. rm(constants.WEB_STATIC_DIR)
  368. # Export the Next app.
  369. subprocess.run([get_package_manager(), "run", "export"], cwd=constants.WEB_DIR)
  370. # Zip up the app.
  371. if zip:
  372. cmd = r"cd .web/_static && zip -r ../../frontend.zip ./* && cd ../.. && zip -r backend.zip ./* -x .web/\* ./assets\* ./frontend.zip\* ./backend.zip\*"
  373. os.system(cmd)
  374. def setup_frontend(root: Path):
  375. """Set up the frontend.
  376. Args:
  377. root: root path of the project.
  378. """
  379. # Initialize the web directory if it doesn't exist.
  380. cp(constants.WEB_TEMPLATE_DIR, str(root / constants.WEB_DIR), overwrite=False)
  381. # Install the frontend packages.
  382. console.rule("[bold]Installing frontend packages")
  383. install_frontend_packages()
  384. # copy asset files to public folder
  385. mkdir(str(root / constants.WEB_ASSETS_DIR))
  386. cp(
  387. src=str(root / constants.APP_ASSETS_DIR),
  388. dest=str(root / constants.WEB_ASSETS_DIR),
  389. )
  390. def run_frontend(app: App, root: Path):
  391. """Run the frontend.
  392. Args:
  393. app: The app.
  394. root: root path of the project.
  395. """
  396. # Set up the frontend.
  397. setup_frontend(root)
  398. # Compile the frontend.
  399. app.compile(force_compile=True)
  400. # Run the frontend in development mode.
  401. console.rule("[bold green]App Running")
  402. os.environ["PORT"] = get_config().port
  403. subprocess.Popen(
  404. [get_package_manager(), "run", "dev"], cwd=constants.WEB_DIR, env=os.environ
  405. )
  406. def run_frontend_prod(app: App, root: Path):
  407. """Run the frontend.
  408. Args:
  409. app: The app.
  410. root: root path of the project.
  411. """
  412. # Set up the frontend.
  413. setup_frontend(root)
  414. # Export the app.
  415. export_app(app)
  416. os.environ["PORT"] = get_config().port
  417. # Run the frontend in production mode.
  418. subprocess.Popen(
  419. [get_package_manager(), "run", "prod"], cwd=constants.WEB_DIR, env=os.environ
  420. )
  421. def get_num_workers() -> int:
  422. """Get the number of backend worker processes.
  423. Returns:
  424. The number of backend worker processes.
  425. """
  426. if get_redis() is None:
  427. # If there is no redis, then just use 1 worker.
  428. return 1
  429. # Use the number of cores * 2 + 1.
  430. return (os.cpu_count() or 1) * 2 + 1
  431. def get_api_port() -> int:
  432. """Get the API port.
  433. Returns:
  434. The API port.
  435. """
  436. port = urlparse(get_config().api_url).port
  437. if port is None:
  438. port = urlparse(constants.API_URL).port
  439. assert port is not None
  440. return port
  441. def run_backend(app_name: str, loglevel: constants.LogLevel = constants.LogLevel.ERROR):
  442. """Run the backend.
  443. Args:
  444. app_name: The app name.
  445. loglevel: The log level.
  446. """
  447. uvicorn.run(
  448. f"{app_name}:{constants.APP_VAR}.{constants.API_VAR}",
  449. host=constants.BACKEND_HOST,
  450. port=get_api_port(),
  451. log_level=loglevel,
  452. reload=True,
  453. )
  454. def run_backend_prod(
  455. app_name: str, loglevel: constants.LogLevel = constants.LogLevel.ERROR
  456. ):
  457. """Run the backend.
  458. Args:
  459. app_name: The app name.
  460. loglevel: The log level.
  461. """
  462. num_workers = get_num_workers()
  463. command = constants.RUN_BACKEND_PROD + [
  464. "--bind",
  465. f"0.0.0.0:{get_api_port()}",
  466. "--workers",
  467. str(num_workers),
  468. "--threads",
  469. str(num_workers),
  470. "--log-level",
  471. str(loglevel),
  472. f"{app_name}:{constants.APP_VAR}()",
  473. ]
  474. subprocess.run(command)
  475. def get_production_backend_url() -> str:
  476. """Get the production backend URL.
  477. Returns:
  478. The production backend URL.
  479. """
  480. config = get_config()
  481. return constants.PRODUCTION_BACKEND_URL.format(
  482. username=config.username,
  483. app_name=config.app_name,
  484. )
  485. def to_snake_case(text: str) -> str:
  486. """Convert a string to snake case.
  487. The words in the text are converted to lowercase and
  488. separated by underscores.
  489. Args:
  490. text: The string to convert.
  491. Returns:
  492. The snake case string.
  493. """
  494. s1 = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", text)
  495. return re.sub("([a-z0-9])([A-Z])", r"\1_\2", s1).lower()
  496. def to_camel_case(text: str) -> str:
  497. """Convert a string to camel case.
  498. The first word in the text is converted to lowercase and
  499. the rest of the words are converted to title case, removing underscores.
  500. Args:
  501. text: The string to convert.
  502. Returns:
  503. The camel case string.
  504. """
  505. if "_" not in text:
  506. return text
  507. camel = "".join(
  508. word.capitalize() if i > 0 else word.lower()
  509. for i, word in enumerate(text.lstrip("_").split("_"))
  510. )
  511. prefix = "_" if text.startswith("_") else ""
  512. return prefix + camel
  513. def to_title_case(text: str) -> str:
  514. """Convert a string from snake case to title case.
  515. Args:
  516. text: The string to convert.
  517. Returns:
  518. The title case string.
  519. """
  520. return "".join(word.capitalize() for word in text.split("_"))
  521. WRAP_MAP = {
  522. "{": "}",
  523. "(": ")",
  524. "[": "]",
  525. "<": ">",
  526. '"': '"',
  527. "'": "'",
  528. "`": "`",
  529. }
  530. def get_close_char(open: str, close: Optional[str] = None) -> str:
  531. """Check if the given character is a valid brace.
  532. Args:
  533. open: The open character.
  534. close: The close character if provided.
  535. Returns:
  536. The close character.
  537. Raises:
  538. ValueError: If the open character is not a valid brace.
  539. """
  540. if close is not None:
  541. return close
  542. if open not in WRAP_MAP:
  543. raise ValueError(f"Invalid wrap open: {open}, must be one of {WRAP_MAP.keys()}")
  544. return WRAP_MAP[open]
  545. def is_wrapped(text: str, open: str, close: Optional[str] = None) -> bool:
  546. """Check if the given text is wrapped in the given open and close characters.
  547. Args:
  548. text: The text to check.
  549. open: The open character.
  550. close: The close character.
  551. Returns:
  552. Whether the text is wrapped.
  553. """
  554. close = get_close_char(open, close)
  555. return text.startswith(open) and text.endswith(close)
  556. def wrap(
  557. text: str,
  558. open: str,
  559. close: Optional[str] = None,
  560. check_first: bool = True,
  561. num: int = 1,
  562. ) -> str:
  563. """Wrap the given text in the given open and close characters.
  564. Args:
  565. text: The text to wrap.
  566. open: The open character.
  567. close: The close character.
  568. check_first: Whether to check if the text is already wrapped.
  569. num: The number of times to wrap the text.
  570. Returns:
  571. The wrapped text.
  572. """
  573. close = get_close_char(open, close)
  574. # If desired, check if the text is already wrapped in braces.
  575. if check_first and is_wrapped(text=text, open=open, close=close):
  576. return text
  577. # Wrap the text in braces.
  578. return f"{open * num}{text}{close * num}"
  579. def indent(text: str, indent_level: int = 2) -> str:
  580. """Indent the given text by the given indent level.
  581. Args:
  582. text: The text to indent.
  583. indent_level: The indent level.
  584. Returns:
  585. The indented text.
  586. """
  587. lines = text.splitlines()
  588. if len(lines) < 2:
  589. return text
  590. return os.linesep.join(f"{' ' * indent_level}{line}" for line in lines) + os.linesep
  591. def verify_path_validity(path: str) -> None:
  592. """Verify if the path is valid, and throw an error if not.
  593. Args:
  594. path: the path that need to be checked
  595. Raises:
  596. ValueError: explains what is wrong with the path.
  597. """
  598. pattern = catchall_in_route(path)
  599. if pattern and not path.endswith(pattern):
  600. raise ValueError(f"Catch-all must be the last part of the URL: {path}")
  601. def get_path_args(path: str) -> Dict[str, str]:
  602. """Get the path arguments for the given path.
  603. Args:
  604. path: The path to get the arguments for.
  605. Returns:
  606. The path arguments.
  607. """
  608. def add_path_arg(match: re.Match[str], type_: str):
  609. """Add arg from regex search result.
  610. Args:
  611. match: result of a regex search
  612. type_: the assigned type for this arg
  613. Raises:
  614. ValueError: explains what is wrong with the path.
  615. """
  616. arg_name = match.groups()[0]
  617. if arg_name in args:
  618. raise ValueError(
  619. f"arg name [{arg_name}] is used more than once in this URL"
  620. )
  621. args[arg_name] = type_
  622. # Regex to check for path args.
  623. check = constants.RouteRegex.ARG
  624. check_strict_catchall = constants.RouteRegex.STRICT_CATCHALL
  625. check_opt_catchall = constants.RouteRegex.OPT_CATCHALL
  626. # Iterate over the path parts and check for path args.
  627. args = {}
  628. for part in path.split("/"):
  629. match_opt = check_opt_catchall.match(part)
  630. if match_opt:
  631. add_path_arg(match_opt, constants.PathArgType.LIST)
  632. break
  633. match_strict = check_strict_catchall.match(part)
  634. if match_strict:
  635. add_path_arg(match_strict, constants.PathArgType.LIST)
  636. break
  637. match = check.match(part)
  638. if match:
  639. # Add the path arg to the list.
  640. add_path_arg(match, constants.PathArgType.SINGLE)
  641. return args
  642. def catchall_in_route(route: str) -> str:
  643. """Extract the catchall part from a route.
  644. Args:
  645. route: the route from which to extract
  646. Returns:
  647. str: the catchall part of the URI
  648. """
  649. match_ = constants.RouteRegex.CATCHALL.search(route)
  650. return match_.group() if match_ else ""
  651. def catchall_prefix(route: str) -> str:
  652. """Extract the prefix part from a route that contains a catchall.
  653. Args:
  654. route: the route from which to extract
  655. Returns:
  656. str: the prefix part of the URI
  657. """
  658. pattern = catchall_in_route(route)
  659. return route.replace(pattern, "") if pattern else ""
  660. def format_route(route: str) -> str:
  661. """Format the given route.
  662. Args:
  663. route: The route to format.
  664. Returns:
  665. The formatted route.
  666. """
  667. route = route.strip(os.path.sep)
  668. route = to_snake_case(route).replace("_", "-")
  669. if route == "":
  670. return constants.INDEX_ROUTE
  671. return route
  672. def format_cond(
  673. cond: str, true_value: str, false_value: str = '""', is_nested: bool = False
  674. ) -> str:
  675. """Format a conditional expression.
  676. Args:
  677. cond: The cond.
  678. true_value: The value to return if the cond is true.
  679. false_value: The value to return if the cond is false.
  680. is_nested: Whether the cond is nested.
  681. Returns:
  682. The formatted conditional expression.
  683. """
  684. expr = f"{cond} ? {true_value} : {false_value}"
  685. if not is_nested:
  686. expr = wrap(expr, "{")
  687. return expr
  688. def format_event_handler(handler: EventHandler) -> str:
  689. """Format an event handler.
  690. Args:
  691. handler: The event handler to format.
  692. Returns:
  693. The formatted function.
  694. """
  695. # Get the class that defines the event handler.
  696. parts = handler.fn.__qualname__.split(".")
  697. # If there's no enclosing class, just return the function name.
  698. if len(parts) == 1:
  699. return parts[-1]
  700. # Get the state and the function name.
  701. state_name, name = parts[-2:]
  702. # Construct the full event handler name.
  703. try:
  704. # Try to get the state from the module.
  705. state = vars(sys.modules[handler.fn.__module__])[state_name]
  706. except:
  707. # If the state isn't in the module, just return the function name.
  708. return handler.fn.__qualname__
  709. return ".".join([state.get_full_name(), name])
  710. def format_event(event_spec: EventSpec) -> str:
  711. """Format an event.
  712. Args:
  713. event_spec: The event to format.
  714. Returns:
  715. The compiled event.
  716. """
  717. args = ",".join([":".join((name, val)) for name, val in event_spec.args])
  718. return f"E(\"{format_event_handler(event_spec.handler)}\", {wrap(args, '{')})"
  719. USED_VARIABLES = set()
  720. def get_unique_variable_name() -> str:
  721. """Get a unique variable name.
  722. Returns:
  723. The unique variable name.
  724. """
  725. name = "".join([random.choice(string.ascii_lowercase) for _ in range(8)])
  726. if name not in USED_VARIABLES:
  727. USED_VARIABLES.add(name)
  728. return name
  729. return get_unique_variable_name()
  730. def get_default_app_name() -> str:
  731. """Get the default app name.
  732. The default app name is the name of the current directory.
  733. Returns:
  734. The default app name.
  735. """
  736. return os.getcwd().split(os.path.sep)[-1].replace("-", "_")
  737. def is_dataframe(value: Type) -> bool:
  738. """Check if the given value is a dataframe.
  739. Args:
  740. value: The value to check.
  741. Returns:
  742. Whether the value is a dataframe.
  743. """
  744. return value.__name__ == "DataFrame"
  745. def format_state(value: Any) -> Dict:
  746. """Recursively format values in the given state.
  747. Args:
  748. value: The state to format.
  749. Returns:
  750. The formatted state.
  751. Raises:
  752. TypeError: If the given value is not a valid state.
  753. """
  754. # Handle dicts.
  755. if isinstance(value, dict):
  756. return {k: format_state(v) for k, v in value.items()}
  757. # Return state vars as is.
  758. if isinstance(value, StateBases):
  759. return value
  760. # Convert plotly figures to JSON.
  761. if isinstance(value, go.Figure):
  762. return json.loads(to_json(value))["data"]
  763. # Convert pandas dataframes to JSON.
  764. if is_dataframe(type(value)):
  765. return {
  766. "columns": value.columns.tolist(),
  767. "data": value.values.tolist(),
  768. }
  769. raise TypeError(
  770. "State vars must be primitive Python types, "
  771. "or subclasses of pc.Base. "
  772. f"Got var of type {type(value)}."
  773. )
  774. def get_event(state, event):
  775. """Get the event from the given state.
  776. Args:
  777. state: The state.
  778. event: The event.
  779. Returns:
  780. The event.
  781. """
  782. return f"{state.get_name()}.{event}"
  783. def format_string(string: str) -> str:
  784. """Format the given string as a JS string literal..
  785. Args:
  786. string: The string to format.
  787. Returns:
  788. The formatted string.
  789. """
  790. # Escape backticks.
  791. string = string.replace(r"\`", "`")
  792. string = string.replace("`", r"\`")
  793. # Wrap the string so it looks like {`string`}.
  794. string = wrap(string, "`")
  795. string = wrap(string, "{")
  796. return string
  797. def call_event_handler(event_handler: EventHandler, arg: Var) -> EventSpec:
  798. """Call an event handler to get the event spec.
  799. This function will inspect the function signature of the event handler.
  800. If it takes in an arg, the arg will be passed to the event handler.
  801. Otherwise, the event handler will be called with no args.
  802. Args:
  803. event_handler: The event handler.
  804. arg: The argument to pass to the event handler.
  805. Returns:
  806. The event spec from calling the event handler.
  807. """
  808. args = inspect.getfullargspec(event_handler.fn).args
  809. if len(args) == 1:
  810. return event_handler()
  811. assert (
  812. len(args) == 2
  813. ), f"Event handler {event_handler.fn} must have 1 or 2 arguments."
  814. return event_handler(arg)
  815. def call_event_fn(fn: Callable, arg: Var) -> List[EventSpec]:
  816. """Call a function to a list of event specs.
  817. The function should return either a single EventSpec or a list of EventSpecs.
  818. If the function takes in an arg, the arg will be passed to the function.
  819. Otherwise, the function will be called with no args.
  820. Args:
  821. fn: The function to call.
  822. arg: The argument to pass to the function.
  823. Returns:
  824. The event specs from calling the function.
  825. Raises:
  826. ValueError: If the lambda has an invalid signature.
  827. """
  828. args = inspect.getfullargspec(fn).args
  829. if len(args) == 0:
  830. out = fn()
  831. elif len(args) == 1:
  832. out = fn(arg)
  833. else:
  834. raise ValueError(f"Lambda {fn} must have 0 or 1 arguments.")
  835. if not isinstance(out, List):
  836. out = [out]
  837. return out
  838. def get_handler_args(event_spec: EventSpec, arg: Var) -> Tuple[Tuple[str, str], ...]:
  839. """Get the handler args for the given event spec.
  840. Args:
  841. event_spec: The event spec.
  842. arg: The controlled event argument.
  843. Returns:
  844. The handler args.
  845. """
  846. args = inspect.getfullargspec(event_spec.handler.fn).args
  847. if len(args) > 2:
  848. return event_spec.args
  849. else:
  850. return ((args[1], arg.name),)
  851. def fix_events(events: Optional[List[Event]], token: str) -> List[Event]:
  852. """Fix a list of events returned by an event handler.
  853. Args:
  854. events: The events to fix.
  855. token: The user token.
  856. Returns:
  857. The fixed events.
  858. """
  859. from pynecone.event import Event, EventHandler, EventSpec
  860. # If the event handler returns nothing, return an empty list.
  861. if events is None:
  862. return []
  863. # If the handler returns a single event, wrap it in a list.
  864. if not isinstance(events, List):
  865. events = [events]
  866. # Fix the events created by the handler.
  867. out = []
  868. for e in events:
  869. # If it is already an event, don't modify it.
  870. if isinstance(e, Event):
  871. name = e.name
  872. payload = e.payload
  873. # Otherwise, create an event from the event spec.
  874. else:
  875. if isinstance(e, EventHandler):
  876. e = e()
  877. assert isinstance(e, EventSpec), f"Unexpected event type, {type(e)}."
  878. name = format_event_handler(e.handler)
  879. payload = dict(e.args)
  880. # Create an event and append it to the list.
  881. out.append(
  882. Event(
  883. token=token,
  884. name=name,
  885. payload=payload,
  886. )
  887. )
  888. return out
  889. def merge_imports(*imports) -> ImportDict:
  890. """Merge two import dicts together.
  891. Args:
  892. *imports: The list of import dicts to merge.
  893. Returns:
  894. The merged import dicts.
  895. """
  896. all_imports = defaultdict(set)
  897. for import_dict in imports:
  898. for lib, fields in import_dict.items():
  899. for field in fields:
  900. all_imports[lib].add(field)
  901. return all_imports
  902. def get_hydrate_event(state) -> str:
  903. """Get the name of the hydrate event for the state.
  904. Args:
  905. state: The state.
  906. Returns:
  907. The name of the hydrate event.
  908. """
  909. return get_event(state, constants.HYDRATE)
  910. def get_redis() -> Optional[Redis]:
  911. """Get the redis client.
  912. Returns:
  913. The redis client.
  914. """
  915. config = get_config()
  916. if config.redis_url is None:
  917. return None
  918. redis_url, redis_port = config.redis_url.split(":")
  919. print("Using redis at", config.redis_url)
  920. return Redis(host=redis_url, port=int(redis_port), db=0)
  921. # Store this here for performance.
  922. StateBases = get_base_class(StateVar)