custom_components.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. """CLI for creating custom components."""
  2. from __future__ import annotations
  3. import os
  4. import re
  5. import subprocess
  6. import sys
  7. from collections import namedtuple
  8. from contextlib import contextmanager
  9. from pathlib import Path
  10. import httpx
  11. import typer
  12. from reflex import constants
  13. from reflex.config import get_config
  14. from reflex.constants import CustomComponents
  15. from reflex.utils import console
  16. custom_components_cli = typer.Typer()
  17. POST_CUSTOM_COMPONENTS_GALLERY_TIMEOUT = 15
  18. @contextmanager
  19. def set_directory(working_directory: str | Path):
  20. """Context manager that sets the working directory.
  21. Args:
  22. working_directory: The working directory to change to.
  23. Yields:
  24. Yield to the caller to perform operations in the working directory.
  25. """
  26. current_directory = Path.cwd()
  27. working_directory = Path(working_directory)
  28. try:
  29. os.chdir(working_directory)
  30. yield
  31. finally:
  32. os.chdir(current_directory)
  33. def _create_package_config(module_name: str, package_name: str):
  34. """Create a package config pyproject.toml file.
  35. Args:
  36. module_name: The name of the module.
  37. package_name: The name of the package typically constructed with `reflex-` prefix and a meaningful library name.
  38. """
  39. from reflex.compiler import templates
  40. pyproject = Path(CustomComponents.PYPROJECT_TOML)
  41. pyproject.write_text(
  42. templates.CUSTOM_COMPONENTS_PYPROJECT_TOML.render(
  43. module_name=module_name,
  44. package_name=package_name,
  45. reflex_version=constants.Reflex.VERSION,
  46. )
  47. )
  48. def _create_readme(module_name: str, package_name: str):
  49. """Create a package README file.
  50. Args:
  51. module_name: The name of the module.
  52. package_name: The name of the python package to be published.
  53. """
  54. from reflex.compiler import templates
  55. readme = Path(CustomComponents.PACKAGE_README)
  56. readme.write_text(
  57. templates.CUSTOM_COMPONENTS_README.render(
  58. module_name=module_name,
  59. package_name=package_name,
  60. )
  61. )
  62. def _write_source_and_init_py(
  63. custom_component_src_dir: Path,
  64. component_class_name: str,
  65. module_name: str,
  66. ):
  67. """Write the source code and init file from templates for the custom component.
  68. Args:
  69. custom_component_src_dir: The name of the custom component source directory.
  70. component_class_name: The name of the component class.
  71. module_name: The name of the module.
  72. """
  73. from reflex.compiler import templates
  74. module_path = custom_component_src_dir / f"{module_name}.py"
  75. module_path.write_text(
  76. templates.CUSTOM_COMPONENTS_SOURCE.render(
  77. component_class_name=component_class_name, module_name=module_name
  78. )
  79. )
  80. init_path = custom_component_src_dir / CustomComponents.INIT_FILE
  81. init_path.write_text(
  82. templates.CUSTOM_COMPONENTS_INIT_FILE.render(module_name=module_name)
  83. )
  84. def _populate_demo_app(name_variants: NameVariants):
  85. """Populate the demo app that imports the custom components.
  86. Args:
  87. name_variants: the tuple including various names such as package name, class name needed for the project.
  88. """
  89. from reflex import constants
  90. from reflex.compiler import templates
  91. from reflex.reflex import _init
  92. demo_app_dir = Path(name_variants.demo_app_dir)
  93. demo_app_name = name_variants.demo_app_name
  94. console.info(f"Creating app for testing: {demo_app_dir!s}")
  95. demo_app_dir.mkdir(exist_ok=True)
  96. with set_directory(demo_app_dir):
  97. # We start with the blank template as basis.
  98. _init(name=demo_app_name, template=constants.Templates.DEFAULT)
  99. # Then overwrite the app source file with the one we want for testing custom components.
  100. # This source file is rendered using jinja template file.
  101. demo_file = Path(f"{demo_app_name}/{demo_app_name}.py")
  102. demo_file.write_text(
  103. templates.CUSTOM_COMPONENTS_DEMO_APP.render(
  104. custom_component_module_dir=name_variants.custom_component_module_dir,
  105. module_name=name_variants.module_name,
  106. )
  107. )
  108. # Append the custom component package to the requirements.txt file.
  109. with Path(f"{constants.RequirementsTxt.FILE}").open(mode="a") as f:
  110. f.write(f"{name_variants.package_name}\n")
  111. def _get_default_library_name_parts() -> list[str]:
  112. """Get the default library name. Based on the current directory name, remove any non-alphanumeric characters.
  113. Raises:
  114. Exit: If the current directory name is not suitable for python projects, and we cannot find a valid library name based off it.
  115. Returns:
  116. The parts of default library name.
  117. """
  118. current_dir_name = Path.cwd().name
  119. cleaned_dir_name = re.sub("[^0-9a-zA-Z-_]+", "", current_dir_name).lower()
  120. parts = [part for part in re.split("-|_", cleaned_dir_name) if part]
  121. if parts and parts[0] == constants.Reflex.MODULE_NAME:
  122. # If the directory name already starts with "reflex", remove it from the parts.
  123. parts = parts[1:]
  124. # If no parts left, cannot find a valid library name, exit.
  125. if not parts:
  126. # The folder likely has a name not suitable for python paths.
  127. console.error(
  128. f"Based on current directory name {current_dir_name}, the library name is {constants.Reflex.MODULE_NAME}. This package already exists. Please use --library-name to specify a different name."
  129. )
  130. raise typer.Exit(code=1)
  131. if not parts:
  132. # The folder likely has a name not suitable for python paths.
  133. console.error(
  134. f"Could not find a valid library name based on the current directory: got {current_dir_name}."
  135. )
  136. raise typer.Exit(code=1)
  137. return parts
  138. NameVariants = namedtuple(
  139. "NameVariants",
  140. [
  141. "library_name",
  142. "component_class_name",
  143. "package_name",
  144. "module_name",
  145. "custom_component_module_dir",
  146. "demo_app_dir",
  147. "demo_app_name",
  148. ],
  149. )
  150. def _validate_library_name(library_name: str | None) -> NameVariants:
  151. """Validate the library name.
  152. Args:
  153. library_name: The name of the library if picked otherwise None.
  154. Raises:
  155. Exit: If the library name is not suitable for python projects.
  156. Returns:
  157. A tuple containing the various names such as package name, class name, etc., needed for the project.
  158. """
  159. if library_name is not None and not re.match(
  160. r"^[a-zA-Z-]+[a-zA-Z0-9-]*$", library_name
  161. ):
  162. console.error(
  163. f"Please use only alphanumeric characters or dashes: got {library_name}"
  164. )
  165. raise typer.Exit(code=1)
  166. # If not specified, use the current directory name to form the module name.
  167. name_parts = (
  168. [part.lower() for part in library_name.split("-")]
  169. if library_name
  170. else _get_default_library_name_parts()
  171. )
  172. if not library_name:
  173. library_name = "-".join(name_parts)
  174. # Component class name is the camel case.
  175. component_class_name = "".join([part.capitalize() for part in name_parts])
  176. console.debug(f"Component class name: {component_class_name}")
  177. # Package name is commonly kebab case.
  178. package_name = f"reflex-{library_name}"
  179. console.debug(f"Package name: {package_name}")
  180. # Module name is the snake case.
  181. module_name = "_".join(name_parts)
  182. custom_component_module_dir = Path(f"reflex_{module_name}")
  183. console.debug(f"Custom component source directory: {custom_component_module_dir}")
  184. # Use the same name for the directory and the app.
  185. demo_app_dir = demo_app_name = f"{module_name}_demo"
  186. console.debug(f"Demo app directory: {demo_app_dir}")
  187. return NameVariants(
  188. library_name=library_name,
  189. component_class_name=component_class_name,
  190. package_name=package_name,
  191. module_name=module_name,
  192. custom_component_module_dir=custom_component_module_dir,
  193. demo_app_dir=demo_app_dir,
  194. demo_app_name=demo_app_name,
  195. )
  196. def _populate_custom_component_project(name_variants: NameVariants):
  197. """Populate the custom component source directory. This includes the pyproject.toml, README.md, and the code template for the custom component.
  198. Args:
  199. name_variants: the tuple including various names such as package name, class name needed for the project.
  200. """
  201. console.info(
  202. f"Populating pyproject.toml with package name: {name_variants.package_name}"
  203. )
  204. # write pyproject.toml, README.md, etc.
  205. _create_package_config(
  206. module_name=name_variants.library_name, package_name=name_variants.package_name
  207. )
  208. _create_readme(
  209. module_name=name_variants.library_name, package_name=name_variants.package_name
  210. )
  211. console.info(
  212. f"Initializing the component directory: {CustomComponents.SRC_DIR / name_variants.custom_component_module_dir}"
  213. )
  214. CustomComponents.SRC_DIR.mkdir(exist_ok=True)
  215. with set_directory(CustomComponents.SRC_DIR):
  216. module_dir = Path(name_variants.custom_component_module_dir)
  217. module_dir.mkdir(exist_ok=True, parents=True)
  218. _write_source_and_init_py(
  219. custom_component_src_dir=module_dir,
  220. component_class_name=name_variants.component_class_name,
  221. module_name=name_variants.module_name,
  222. )
  223. @custom_components_cli.command(name="init")
  224. def init(
  225. library_name: str | None = typer.Option(
  226. None,
  227. help="The name of your library. On PyPI, package will be published as `reflex-{library-name}`.",
  228. ),
  229. install: bool = typer.Option(
  230. True,
  231. help="Whether to install package from this local custom component in editable mode.",
  232. ),
  233. loglevel: constants.LogLevel | None = typer.Option(
  234. None, help="The log level to use."
  235. ),
  236. ):
  237. """Initialize a custom component.
  238. Args:
  239. library_name: The name of the library.
  240. install: Whether to install package from this local custom component in editable mode.
  241. loglevel: The log level to use.
  242. Raises:
  243. Exit: If the pyproject.toml already exists.
  244. """
  245. from reflex.utils import exec, prerequisites
  246. console.set_log_level(loglevel or get_config().loglevel)
  247. if CustomComponents.PYPROJECT_TOML.exists():
  248. console.error(f"A {CustomComponents.PYPROJECT_TOML} already exists. Aborting.")
  249. typer.Exit(code=1)
  250. # Show system info.
  251. exec.output_system_info()
  252. # Check the name follows the convention if picked.
  253. name_variants = _validate_library_name(library_name)
  254. console.rule(f"[bold]Initializing {name_variants.package_name} project")
  255. _populate_custom_component_project(name_variants)
  256. _populate_demo_app(name_variants)
  257. # Initialize the .gitignore.
  258. prerequisites.initialize_gitignore(
  259. gitignore_file=CustomComponents.FILE, files_to_ignore=CustomComponents.DEFAULTS
  260. )
  261. if install:
  262. package_name = name_variants.package_name
  263. console.rule(f"[bold]Installing {package_name} in editable mode.")
  264. if _pip_install_on_demand(package_name=".", install_args=["-e"]):
  265. console.info(f"Package {package_name} installed!")
  266. else:
  267. raise typer.Exit(code=1)
  268. console.print("[bold]Custom component initialized successfully!")
  269. console.rule("[bold]Project Summary")
  270. console.print(
  271. f"[ {CustomComponents.PACKAGE_README} ]: Package description. Please add usage examples."
  272. )
  273. console.print(
  274. f"[ {CustomComponents.PYPROJECT_TOML} ]: Project configuration file. Please fill in details such as your name, email, homepage URL."
  275. )
  276. console.print(
  277. f"[ {CustomComponents.SRC_DIR}/ ]: Custom component code template. Start by editing it with your component implementation."
  278. )
  279. console.print(
  280. f"[ {name_variants.demo_app_dir}/ ]: Demo App. Add more code to this app and test."
  281. )
  282. def _pip_install_on_demand(
  283. package_name: str,
  284. install_args: list[str] | None = None,
  285. ) -> bool:
  286. """Install a package on demand.
  287. Args:
  288. package_name: The name of the package.
  289. install_args: The additional arguments for the pip install command.
  290. Returns:
  291. True if the package is installed successfully, False otherwise.
  292. """
  293. install_args = install_args or []
  294. install_cmds = [
  295. sys.executable,
  296. "-m",
  297. "pip",
  298. "install",
  299. *install_args,
  300. package_name,
  301. ]
  302. console.debug(f"Install package: {' '.join(install_cmds)}")
  303. return _run_commands_in_subprocess(install_cmds)
  304. def _run_commands_in_subprocess(cmds: list[str]) -> bool:
  305. """Run commands in a subprocess.
  306. Args:
  307. cmds: The commands to run.
  308. Returns:
  309. True if the command runs successfully, False otherwise.
  310. """
  311. console.debug(f"Running command: {' '.join(cmds)}")
  312. try:
  313. result = subprocess.run(cmds, capture_output=True, text=True, check=True)
  314. except subprocess.CalledProcessError as cpe:
  315. console.error(cpe.stdout)
  316. console.error(cpe.stderr)
  317. return False
  318. else:
  319. console.debug(result.stdout)
  320. return True
  321. def _make_pyi_files():
  322. """Create pyi files for the custom component."""
  323. from reflex.utils.pyi_generator import PyiGenerator
  324. for top_level_dir in Path.cwd().iterdir():
  325. if not top_level_dir.is_dir() or top_level_dir.name.startswith("."):
  326. continue
  327. for dir, _, _ in top_level_dir.walk():
  328. if "__pycache__" in dir.name:
  329. continue
  330. PyiGenerator().scan_all([dir])
  331. def _run_build():
  332. """Run the build command.
  333. Raises:
  334. Exit: If the build fails.
  335. """
  336. console.print("Building custom component...")
  337. _make_pyi_files()
  338. cmds = [sys.executable, "-m", "build", "."]
  339. if _run_commands_in_subprocess(cmds):
  340. console.info("Custom component built successfully!")
  341. else:
  342. raise typer.Exit(code=1)
  343. @custom_components_cli.command(name="build")
  344. def build(
  345. loglevel: constants.LogLevel | None = typer.Option(
  346. None, help="The log level to use."
  347. ),
  348. ):
  349. """Build a custom component. Must be run from the project root directory where the pyproject.toml is.
  350. Args:
  351. loglevel: The log level to use.
  352. """
  353. console.set_log_level(loglevel or get_config().loglevel)
  354. _run_build()
  355. @custom_components_cli.command(name="publish", deprecated=True)
  356. def publish():
  357. """Publish a custom component. This command is deprecated and will be removed in future releases.
  358. Raises:
  359. Exit: If the publish command fails.
  360. """
  361. console.error(
  362. "The publish command is deprecated. You can use `reflex component build` followed by `twine upload` or a similar publishing command to publish your custom component."
  363. "\nIf you want to share your custom component with the Reflex community, please use `reflex component share`."
  364. )
  365. raise typer.Exit(code=1)
  366. def _collect_details_for_gallery():
  367. """Helper to collect details on the custom component to be included in the gallery.
  368. Raises:
  369. Exit: If pyproject.toml file is ill-formed or the request to the backend services fails.
  370. """
  371. from reflex_cli.utils import hosting
  372. console.rule("[bold]Authentication with Reflex Services")
  373. console.print("First let's log in to Reflex backend services.")
  374. access_token, _ = hosting.authenticated_token()
  375. if not access_token:
  376. console.error(
  377. "Unable to authenticate with Reflex backend services. Make sure you are logged in."
  378. )
  379. raise typer.Exit(code=1)
  380. console.rule("[bold]Custom Component Information")
  381. params = {}
  382. package_name = console.ask("[ Published python package name ]")
  383. console.print(f"[ Custom component package name ] : {package_name}")
  384. params["package_name"] = package_name
  385. post_custom_components_gallery_endpoint = (
  386. "https://gallery-backend.reflex.dev/custom-components/gallery"
  387. )
  388. # Check the backend services if the user is allowed to update information of this package is already shared.
  389. try:
  390. console.debug(
  391. f"Checking if user has permission to upsert information for {package_name} by POST."
  392. )
  393. # Send a POST request to achieve two things at once:
  394. # 1. Check if the package is already shared by the user. If not, the backend will return 403.
  395. # 2. If this package is not shared before, this request records the package name in the backend.
  396. response = httpx.post(
  397. post_custom_components_gallery_endpoint,
  398. headers={"Authorization": f"Bearer {access_token}"},
  399. data=params,
  400. )
  401. if response.status_code == httpx.codes.FORBIDDEN:
  402. console.error(
  403. f"{package_name} is owned by another user. Unable to update the information for it."
  404. )
  405. raise typer.Exit(code=1)
  406. response.raise_for_status()
  407. except httpx.HTTPError as he:
  408. console.error(f"Unable to complete request due to {he}.")
  409. raise typer.Exit(code=1) from he
  410. files = []
  411. if (image_file_and_extension := _get_file_from_prompt_in_loop()) is not None:
  412. files.append(
  413. ("files", (image_file_and_extension[1], image_file_and_extension[0]))
  414. )
  415. demo_url = None
  416. while True:
  417. demo_url = (
  418. console.ask(
  419. "[ Full URL of deployed demo app, e.g. `https://my-app.reflex.run` ] (enter to skip)"
  420. )
  421. or None
  422. )
  423. if _validate_url_with_protocol_prefix(demo_url):
  424. break
  425. if demo_url:
  426. params["demo_url"] = demo_url
  427. # Now send the post request to Reflex backend services.
  428. try:
  429. console.debug(f"Sending custom component data: {params}")
  430. response = httpx.post(
  431. post_custom_components_gallery_endpoint,
  432. headers={"Authorization": f"Bearer {access_token}"},
  433. data=params,
  434. files=files,
  435. timeout=POST_CUSTOM_COMPONENTS_GALLERY_TIMEOUT,
  436. )
  437. response.raise_for_status()
  438. except httpx.HTTPError as he:
  439. console.error(f"Unable to complete request due to {he}.")
  440. raise typer.Exit(code=1) from he
  441. console.info("Custom component information successfully shared!")
  442. def _validate_url_with_protocol_prefix(url: str | None) -> bool:
  443. """Validate the URL with protocol prefix. Empty string is acceptable.
  444. Args:
  445. url: the URL string to check.
  446. Returns:
  447. Whether the entered URL is acceptable.
  448. """
  449. return not url or (url.startswith("http://") or url.startswith("https://"))
  450. def _get_file_from_prompt_in_loop() -> tuple[bytes, str] | None:
  451. image_file = file_extension = None
  452. while image_file is None:
  453. image_path_str = console.ask(
  454. "Upload a preview image of your demo app (enter to skip)"
  455. )
  456. if not image_path_str:
  457. break
  458. image_file_path = Path(image_path_str)
  459. if not image_file_path:
  460. break
  461. if not image_file_path.exists():
  462. console.error(f"File {image_file_path} does not exist.")
  463. continue
  464. file_extension = image_file_path.suffix
  465. try:
  466. image_file = image_file_path.read_bytes()
  467. except OSError as ose:
  468. console.error(f"Unable to read the {file_extension} file due to {ose}")
  469. raise typer.Exit(code=1) from ose
  470. else:
  471. return image_file, file_extension
  472. console.debug(f"File extension detected: {file_extension}")
  473. return None
  474. @custom_components_cli.command(name="share")
  475. def share_more_detail(
  476. loglevel: constants.LogLevel | None = typer.Option(
  477. None, help="The log level to use."
  478. ),
  479. ):
  480. """Collect more details on the published package for gallery.
  481. Args:
  482. loglevel: The log level to use.
  483. """
  484. console.set_log_level(loglevel or get_config().loglevel)
  485. _collect_details_for_gallery()
  486. @custom_components_cli.command()
  487. def install(
  488. loglevel: constants.LogLevel | None = typer.Option(
  489. None, help="The log level to use."
  490. ),
  491. ):
  492. """Install package from this local custom component in editable mode.
  493. Args:
  494. loglevel: The log level to use.
  495. Raises:
  496. Exit: If unable to install the current directory in editable mode.
  497. """
  498. console.set_log_level(loglevel or get_config().loglevel)
  499. if _pip_install_on_demand(package_name=".", install_args=["-e"]):
  500. console.info("Package installed successfully!")
  501. else:
  502. raise typer.Exit(code=1)