prerequisites.py 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  1. """Everything related to fetching or initializing build prerequisites."""
  2. from __future__ import annotations
  3. import ast
  4. import contextlib
  5. import dataclasses
  6. import functools
  7. import importlib
  8. import importlib.metadata
  9. import importlib.util
  10. import json
  11. import os
  12. import platform
  13. import random
  14. import re
  15. import shutil
  16. import stat
  17. import sys
  18. import tempfile
  19. import time
  20. import zipfile
  21. from datetime import datetime
  22. from pathlib import Path
  23. from types import ModuleType
  24. from typing import Callable, List, Optional
  25. import astor
  26. import httpx
  27. import typer
  28. from alembic.util.exc import CommandError
  29. from packaging import version
  30. from redis import Redis as RedisSync
  31. from redis.asyncio import Redis
  32. from redis.exceptions import RedisError
  33. from reflex import constants, model
  34. from reflex.compiler import templates
  35. from reflex.config import Config, environment, get_config
  36. from reflex.utils import console, net, path_ops, processes, redir
  37. from reflex.utils.exceptions import (
  38. GeneratedCodeHasNoFunctionDefs,
  39. raise_system_package_missing_error,
  40. )
  41. from reflex.utils.format import format_library_name
  42. from reflex.utils.registry import _get_npm_registry
  43. CURRENTLY_INSTALLING_NODE = False
  44. @dataclasses.dataclass(frozen=True)
  45. class Template:
  46. """A template for a Reflex app."""
  47. name: str
  48. description: str
  49. code_url: str
  50. demo_url: str
  51. @dataclasses.dataclass(frozen=True)
  52. class CpuInfo:
  53. """Model to save cpu info."""
  54. manufacturer_id: Optional[str]
  55. model_name: Optional[str]
  56. address_width: Optional[int]
  57. def get_web_dir() -> Path:
  58. """Get the working directory for the next.js commands.
  59. Can be overridden with REFLEX_WEB_WORKDIR.
  60. Returns:
  61. The working directory.
  62. """
  63. return environment.REFLEX_WEB_WORKDIR.get()
  64. def _python_version_check():
  65. """Emit deprecation warning for deprecated python versions."""
  66. # Check for end-of-life python versions.
  67. if sys.version_info < (3, 10):
  68. console.deprecate(
  69. feature_name="Support for Python 3.9 and older",
  70. reason="please upgrade to Python 3.10 or newer",
  71. deprecation_version="0.6.0",
  72. removal_version="0.7.0",
  73. )
  74. def check_latest_package_version(package_name: str):
  75. """Check if the latest version of the package is installed.
  76. Args:
  77. package_name: The name of the package.
  78. """
  79. if environment.REFLEX_CHECK_LATEST_VERSION.get() is False:
  80. return
  81. try:
  82. # Get the latest version from PyPI
  83. current_version = importlib.metadata.version(package_name)
  84. url = f"https://pypi.org/pypi/{package_name}/json"
  85. response = net.get(url)
  86. latest_version = response.json()["info"]["version"]
  87. if get_or_set_last_reflex_version_check_datetime():
  88. # Versions were already checked and saved in reflex.json, no need to warn again
  89. return
  90. if version.parse(current_version) < version.parse(latest_version):
  91. # Show a warning when the host version is older than PyPI version
  92. console.warn(
  93. f"Your version ({current_version}) of {package_name} is out of date. Upgrade to {latest_version} with 'pip install {package_name} --upgrade'"
  94. )
  95. # Check for deprecated python versions
  96. _python_version_check()
  97. except Exception:
  98. pass
  99. def get_or_set_last_reflex_version_check_datetime():
  100. """Get the last time a check was made for the latest reflex version.
  101. This is typically useful for cases where the host reflex version is
  102. less than that on Pypi.
  103. Returns:
  104. The last version check datetime.
  105. """
  106. reflex_json_file = get_web_dir() / constants.Reflex.JSON
  107. if not reflex_json_file.exists():
  108. return None
  109. # Open and read the file
  110. data = json.loads(reflex_json_file.read_text())
  111. last_version_check_datetime = data.get("last_version_check_datetime")
  112. if not last_version_check_datetime:
  113. data.update({"last_version_check_datetime": str(datetime.now())})
  114. path_ops.update_json_file(reflex_json_file, data)
  115. return last_version_check_datetime
  116. def set_last_reflex_run_time():
  117. """Set the last Reflex run time."""
  118. path_ops.update_json_file(
  119. get_web_dir() / constants.Reflex.JSON,
  120. {"last_reflex_run_datetime": str(datetime.now())},
  121. )
  122. def check_node_version() -> bool:
  123. """Check the version of Node.js.
  124. Returns:
  125. Whether the version of Node.js is valid.
  126. """
  127. current_version = get_node_version()
  128. return current_version is not None and current_version >= version.parse(
  129. constants.Node.MIN_VERSION
  130. )
  131. def get_node_version() -> version.Version | None:
  132. """Get the version of node.
  133. Returns:
  134. The version of node.
  135. """
  136. node_path = path_ops.get_node_path()
  137. if node_path is None:
  138. return None
  139. try:
  140. result = processes.new_process([node_path, "-v"], run=True)
  141. # The output will be in the form "vX.Y.Z", but version.parse() can handle it
  142. return version.parse(result.stdout) # type: ignore
  143. except (FileNotFoundError, TypeError):
  144. return None
  145. def get_fnm_version() -> version.Version | None:
  146. """Get the version of fnm.
  147. Returns:
  148. The version of FNM.
  149. """
  150. try:
  151. result = processes.new_process([constants.Fnm.EXE, "--version"], run=True)
  152. return version.parse(result.stdout.split(" ")[1]) # type: ignore
  153. except (FileNotFoundError, TypeError):
  154. return None
  155. except version.InvalidVersion as e:
  156. console.warn(
  157. f"The detected fnm version ({e.args[0]}) is not valid. Defaulting to None."
  158. )
  159. return None
  160. def get_bun_version() -> version.Version | None:
  161. """Get the version of bun.
  162. Returns:
  163. The version of bun.
  164. """
  165. try:
  166. # Run the bun -v command and capture the output
  167. result = processes.new_process([str(get_config().bun_path), "-v"], run=True)
  168. return version.parse(result.stdout) # type: ignore
  169. except FileNotFoundError:
  170. return None
  171. except version.InvalidVersion as e:
  172. console.warn(
  173. f"The detected bun version ({e.args[0]}) is not valid. Defaulting to None."
  174. )
  175. return None
  176. def get_install_package_manager(on_failure_return_none: bool = False) -> str | None:
  177. """Get the package manager executable for installation.
  178. Currently, bun is used for installation only.
  179. Args:
  180. on_failure_return_none: Whether to return None on failure.
  181. Returns:
  182. The path to the package manager.
  183. """
  184. if constants.IS_WINDOWS and (
  185. not is_windows_bun_supported()
  186. or windows_check_onedrive_in_path()
  187. or windows_npm_escape_hatch()
  188. ):
  189. return get_package_manager(on_failure_return_none)
  190. return str(get_config().bun_path)
  191. def get_package_manager(on_failure_return_none: bool = False) -> str | None:
  192. """Get the package manager executable for running app.
  193. Currently on unix systems, npm is used for running the app only.
  194. Args:
  195. on_failure_return_none: Whether to return None on failure.
  196. Returns:
  197. The path to the package manager.
  198. Raises:
  199. FileNotFoundError: If the package manager is not found.
  200. """
  201. npm_path = path_ops.get_npm_path()
  202. if npm_path is not None:
  203. return str(Path(npm_path).resolve())
  204. if on_failure_return_none:
  205. return None
  206. raise FileNotFoundError("NPM not found. You may need to run `reflex init`.")
  207. def windows_check_onedrive_in_path() -> bool:
  208. """For windows, check if oneDrive is present in the project dir path.
  209. Returns:
  210. If oneDrive is in the path of the project directory.
  211. """
  212. return "onedrive" in str(Path.cwd()).lower()
  213. def windows_npm_escape_hatch() -> bool:
  214. """For windows, if the user sets REFLEX_USE_NPM, use npm instead of bun.
  215. Returns:
  216. If the user has set REFLEX_USE_NPM.
  217. """
  218. return environment.REFLEX_USE_NPM.get()
  219. def get_app(reload: bool = False) -> ModuleType:
  220. """Get the app module based on the default config.
  221. Args:
  222. reload: Re-import the app module from disk
  223. Returns:
  224. The app based on the default config.
  225. Raises:
  226. RuntimeError: If the app name is not set in the config.
  227. """
  228. from reflex.utils import telemetry
  229. try:
  230. environment.RELOAD_CONFIG.set(reload)
  231. config = get_config()
  232. if not config.app_name:
  233. raise RuntimeError(
  234. "Cannot get the app module because `app_name` is not set in rxconfig! "
  235. "If this error occurs in a reflex test case, ensure that `get_app` is mocked."
  236. )
  237. module = config.module
  238. sys.path.insert(0, str(Path.cwd()))
  239. app = __import__(module, fromlist=(constants.CompileVars.APP,))
  240. if reload:
  241. from reflex.state import reload_state_module
  242. # Reset rx.State subclasses to avoid conflict when reloading.
  243. reload_state_module(module=module)
  244. # Reload the app module.
  245. importlib.reload(app)
  246. return app
  247. except Exception as ex:
  248. telemetry.send_error(ex, context="frontend")
  249. raise
  250. def get_compiled_app(reload: bool = False, export: bool = False) -> ModuleType:
  251. """Get the app module based on the default config after first compiling it.
  252. Args:
  253. reload: Re-import the app module from disk
  254. export: Compile the app for export
  255. Returns:
  256. The compiled app based on the default config.
  257. """
  258. app_module = get_app(reload=reload)
  259. app = getattr(app_module, constants.CompileVars.APP)
  260. # For py3.9 compatibility when redis is used, we MUST add any decorator pages
  261. # before compiling the app in a thread to avoid event loop error (REF-2172).
  262. app._apply_decorated_pages()
  263. app._compile(export=export)
  264. return app_module
  265. def get_redis() -> Redis | None:
  266. """Get the asynchronous redis client.
  267. Returns:
  268. The asynchronous redis client.
  269. """
  270. if (redis_url := parse_redis_url()) is not None:
  271. return Redis.from_url(
  272. redis_url,
  273. retry_on_error=[RedisError],
  274. )
  275. return None
  276. def get_redis_sync() -> RedisSync | None:
  277. """Get the synchronous redis client.
  278. Returns:
  279. The synchronous redis client.
  280. """
  281. if (redis_url := parse_redis_url()) is not None:
  282. return RedisSync.from_url(
  283. redis_url,
  284. retry_on_error=[RedisError],
  285. )
  286. return None
  287. def parse_redis_url() -> str | None:
  288. """Parse the REDIS_URL in config if applicable.
  289. Returns:
  290. If url is non-empty, return the URL as it is.
  291. Raises:
  292. ValueError: If the REDIS_URL is not a supported scheme.
  293. """
  294. config = get_config()
  295. if not config.redis_url:
  296. return None
  297. if not config.redis_url.startswith(("redis://", "rediss://", "unix://")):
  298. raise ValueError(
  299. "REDIS_URL must start with 'redis://', 'rediss://', or 'unix://'."
  300. )
  301. return config.redis_url
  302. async def get_redis_status() -> dict[str, bool | None]:
  303. """Checks the status of the Redis connection.
  304. Attempts to connect to Redis and send a ping command to verify connectivity.
  305. Returns:
  306. The status of the Redis connection.
  307. """
  308. try:
  309. status = True
  310. redis_client = get_redis_sync()
  311. if redis_client is not None:
  312. redis_client.ping()
  313. else:
  314. status = None
  315. except RedisError:
  316. status = False
  317. return {"redis": status}
  318. def validate_app_name(app_name: str | None = None) -> str:
  319. """Validate the app name.
  320. The default app name is the name of the current directory.
  321. Args:
  322. app_name: the name passed by user during reflex init
  323. Returns:
  324. The app name after validation.
  325. Raises:
  326. Exit: if the app directory name is reflex or if the name is not standard for a python package name.
  327. """
  328. app_name = app_name if app_name else Path.cwd().name.replace("-", "_")
  329. # Make sure the app is not named "reflex".
  330. if app_name.lower() == constants.Reflex.MODULE_NAME:
  331. console.error(
  332. f"The app directory cannot be named [bold]{constants.Reflex.MODULE_NAME}[/bold]."
  333. )
  334. raise typer.Exit(1)
  335. # Make sure the app name is standard for a python package name.
  336. if not re.match(r"^[a-zA-Z][a-zA-Z0-9_]*$", app_name):
  337. console.error(
  338. "The app directory name must start with a letter and can contain letters, numbers, and underscores."
  339. )
  340. raise typer.Exit(1)
  341. return app_name
  342. class ImportRenamer(ast.NodeTransformer):
  343. """Rename imports in a tree."""
  344. def __init__(self, old_name, new_name):
  345. """Initialize the ImportRenamer."""
  346. self.old_name = old_name
  347. self.new_name = new_name
  348. def visit_Import(self, node):
  349. """Rename imports of the form `import foo`."""
  350. for alias in node.names:
  351. if alias.name == self.old_name:
  352. alias.name = self.new_name
  353. return node
  354. def visit_ImportFrom(self, node):
  355. """Rename imports of the form `from foo import bar`."""
  356. if node.module == self.old_name:
  357. node.module = self.new_name
  358. return node
  359. def visit_Assign(self, node):
  360. """Handle assignments like `config = rx.Config(app_name='foo')`."""
  361. if (
  362. isinstance(node.targets[0], ast.Name)
  363. and node.targets[0].id == "config"
  364. and isinstance(node.value, ast.Call)
  365. and isinstance(node.value.func, ast.Attribute)
  366. and node.value.func.attr == "Config"
  367. ):
  368. for kw in node.value.keywords:
  369. if kw.arg == "app_name" and isinstance(kw.value, ast.Constant):
  370. if kw.value.value == self.old_name:
  371. kw.value = ast.Constant(value=self.new_name)
  372. # Handle positional arguments
  373. if node.value.args and isinstance(node.value.args[0], ast.Constant):
  374. if node.value.args[0].value == self.old_name:
  375. node.value.args[0] = ast.Constant(value=self.new_name)
  376. return node
  377. def rename_imports_and_app_name_in_file(file_path, old_name, new_name):
  378. """Rename imports and update the app_name in rxconfig.py."""
  379. file_path = Path(file_path)
  380. content = file_path.read_text()
  381. tree = ast.parse(content)
  382. transformer = ImportRenamer(old_name, new_name)
  383. new_tree = transformer.visit(tree)
  384. ast.fix_missing_locations(new_tree)
  385. modified_content = astor.to_source(new_tree)
  386. file_path.write_text(modified_content)
  387. def process_directory(directory, old_name, new_name, exclude_dirs=None):
  388. """Process all Python files in a directory, excluding specified directories."""
  389. exclude_dirs = exclude_dirs or []
  390. directory = Path(directory)
  391. for root in directory.rglob("*.py"):
  392. if not any(root.parts[i] in exclude_dirs for i in range(len(root.parts))):
  393. rename_imports_and_app_name_in_file(root, old_name, new_name)
  394. def rename_path_up_tree(full_path, old_name, new_name):
  395. """Rename all instances of `old_name` in the path (file and directories) to `new_name`."""
  396. current_path = Path(full_path)
  397. new_path = None
  398. while True:
  399. directory, base = current_path.parent, current_path.name
  400. if old_name in base:
  401. new_base = base.replace(old_name, new_name)
  402. new_path = directory / new_base
  403. current_path.rename(new_path)
  404. current_path = new_path
  405. else:
  406. new_path = current_path
  407. # Stop if we've reached the root package
  408. if old_name not in directory.name:
  409. break
  410. # Move up the directory tree
  411. current_path = directory
  412. return new_path
  413. def rename_app(app_name: str):
  414. """Rename the app directory."""
  415. config = get_config()
  416. process_directory(
  417. Path.cwd(), config.app_name, app_name, exclude_dirs=["assets", ".web"]
  418. )
  419. full_path = importlib.util.find_spec(config.module).origin
  420. rename_path_up_tree(full_path, config.app_name, app_name)
  421. def create_config(app_name: str):
  422. """Create a new rxconfig file.
  423. Args:
  424. app_name: The name of the app.
  425. """
  426. # Import here to avoid circular imports.
  427. from reflex.compiler import templates
  428. config_name = f"{re.sub(r'[^a-zA-Z]', '', app_name).capitalize()}Config"
  429. console.debug(f"Creating {constants.Config.FILE}")
  430. constants.Config.FILE.write_text(
  431. templates.RXCONFIG.render(app_name=app_name, config_name=config_name)
  432. )
  433. def initialize_gitignore(
  434. gitignore_file: Path = constants.GitIgnore.FILE,
  435. files_to_ignore: set[str] | list[str] = constants.GitIgnore.DEFAULTS,
  436. ):
  437. """Initialize the template .gitignore file.
  438. Args:
  439. gitignore_file: The .gitignore file to create.
  440. files_to_ignore: The files to add to the .gitignore file.
  441. """
  442. # Combine with the current ignored files.
  443. current_ignore: list[str] = []
  444. if gitignore_file.exists():
  445. current_ignore = [ln.strip() for ln in gitignore_file.read_text().splitlines()]
  446. if files_to_ignore == current_ignore:
  447. console.debug(f"{gitignore_file} already up to date.")
  448. return
  449. files_to_ignore = [ln for ln in files_to_ignore if ln not in current_ignore]
  450. files_to_ignore += current_ignore
  451. # Write files to the .gitignore file.
  452. gitignore_file.touch(exist_ok=True)
  453. console.debug(f"Creating {gitignore_file}")
  454. gitignore_file.write_text("\n".join(files_to_ignore) + "\n")
  455. def initialize_requirements_txt():
  456. """Initialize the requirements.txt file.
  457. If absent, generate one for the user.
  458. If the requirements.txt does not have reflex as dependency,
  459. generate a requirement pinning current version and append to
  460. the requirements.txt file.
  461. """
  462. fp = Path(constants.RequirementsTxt.FILE)
  463. encoding = "utf-8"
  464. if not fp.exists():
  465. fp.touch()
  466. else:
  467. # Detect the encoding of the original file
  468. import charset_normalizer
  469. charset_matches = charset_normalizer.from_path(fp)
  470. maybe_charset_match = charset_matches.best()
  471. if maybe_charset_match is None:
  472. console.debug(f"Unable to detect encoding for {fp}, exiting.")
  473. return
  474. encoding = maybe_charset_match.encoding
  475. console.debug(f"Detected encoding for {fp} as {encoding}.")
  476. try:
  477. other_requirements_exist = False
  478. with fp.open("r", encoding=encoding) as f:
  479. for req in f:
  480. # Check if we have a package name that is reflex
  481. if re.match(r"^reflex[^a-zA-Z0-9]", req):
  482. console.debug(f"{fp} already has reflex as dependency.")
  483. return
  484. other_requirements_exist = True
  485. with fp.open("a", encoding=encoding) as f:
  486. preceding_newline = "\n" if other_requirements_exist else ""
  487. f.write(
  488. f"{preceding_newline}{constants.RequirementsTxt.DEFAULTS_STUB}{constants.Reflex.VERSION}\n"
  489. )
  490. except Exception:
  491. console.info(f"Unable to check {fp} for reflex dependency.")
  492. def initialize_app_directory(
  493. app_name: str,
  494. template_name: str = constants.Templates.DEFAULT,
  495. template_code_dir_name: str | None = None,
  496. template_dir: Path | None = None,
  497. ):
  498. """Initialize the app directory on reflex init.
  499. Args:
  500. app_name: The name of the app.
  501. template_name: The name of the template to use.
  502. template_code_dir_name: The name of the code directory in the template.
  503. template_dir: The directory of the template source files.
  504. Raises:
  505. Exit: If template_name, template_code_dir_name, template_dir combination is not supported.
  506. """
  507. console.log("Initializing the app directory.")
  508. # By default, use the blank template from local assets.
  509. if template_name == constants.Templates.DEFAULT:
  510. if template_code_dir_name is not None or template_dir is not None:
  511. console.error(
  512. f"Only {template_name=} should be provided, got {template_code_dir_name=}, {template_dir=}."
  513. )
  514. raise typer.Exit(1)
  515. template_code_dir_name = constants.Templates.Dirs.CODE
  516. template_dir = Path(constants.Templates.Dirs.BASE, "apps", template_name)
  517. else:
  518. if template_code_dir_name is None or template_dir is None:
  519. console.error(
  520. f"For `{template_name}` template, `template_code_dir_name` and `template_dir` should both be provided."
  521. )
  522. raise typer.Exit(1)
  523. console.debug(f"Using {template_name=} {template_dir=} {template_code_dir_name=}.")
  524. # Remove all pyc and __pycache__ dirs in template directory.
  525. for pyc_file in template_dir.glob("**/*.pyc"):
  526. pyc_file.unlink()
  527. for pycache_dir in template_dir.glob("**/__pycache__"):
  528. pycache_dir.rmdir()
  529. for file in template_dir.iterdir():
  530. # Copy the file to current directory but keep the name the same.
  531. path_ops.cp(str(file), file.name)
  532. # Rename the template app to the app name.
  533. path_ops.mv(template_code_dir_name, app_name)
  534. path_ops.mv(
  535. Path(app_name) / (template_name + constants.Ext.PY),
  536. Path(app_name) / (app_name + constants.Ext.PY),
  537. )
  538. # Fix up the imports.
  539. path_ops.find_replace(
  540. app_name,
  541. f"from {template_name}",
  542. f"from {app_name}",
  543. )
  544. def get_project_hash(raise_on_fail: bool = False) -> int | None:
  545. """Get the project hash from the reflex.json file if the file exists.
  546. Args:
  547. raise_on_fail: Whether to raise an error if the file does not exist.
  548. Returns:
  549. project_hash: The app hash.
  550. """
  551. json_file = get_web_dir() / constants.Reflex.JSON
  552. if not json_file.exists() and not raise_on_fail:
  553. return None
  554. data = json.loads(json_file.read_text())
  555. return data.get("project_hash")
  556. def initialize_web_directory():
  557. """Initialize the web directory on reflex init."""
  558. console.log("Initializing the web directory.")
  559. # Reuse the hash if one is already created, so we don't over-write it when running reflex init
  560. project_hash = get_project_hash()
  561. path_ops.cp(constants.Templates.Dirs.WEB_TEMPLATE, str(get_web_dir()))
  562. initialize_package_json()
  563. initialize_bun_config()
  564. path_ops.mkdir(get_web_dir() / constants.Dirs.PUBLIC)
  565. update_next_config()
  566. # Initialize the reflex json file.
  567. init_reflex_json(project_hash=project_hash)
  568. def _turbopack_flag() -> str:
  569. return " --turbopack" if environment.REFLEX_USE_TURBOPACK.get() else ""
  570. def _compile_package_json():
  571. return templates.PACKAGE_JSON.render(
  572. scripts={
  573. "dev": constants.PackageJson.Commands.DEV + _turbopack_flag(),
  574. "export": constants.PackageJson.Commands.EXPORT,
  575. "export_sitemap": constants.PackageJson.Commands.EXPORT_SITEMAP,
  576. "prod": constants.PackageJson.Commands.PROD,
  577. },
  578. dependencies=constants.PackageJson.DEPENDENCIES,
  579. dev_dependencies=constants.PackageJson.DEV_DEPENDENCIES,
  580. )
  581. def initialize_package_json():
  582. """Render and write in .web the package.json file."""
  583. output_path = get_web_dir() / constants.PackageJson.PATH
  584. output_path.write_text(_compile_package_json())
  585. def initialize_bun_config():
  586. """Initialize the bun config file."""
  587. bun_config_path = get_web_dir() / constants.Bun.CONFIG_PATH
  588. if (custom_bunfig := Path(constants.Bun.CONFIG_PATH)).exists():
  589. bunfig_content = custom_bunfig.read_text()
  590. console.info(f"Copying custom bunfig.toml inside {get_web_dir()} folder")
  591. else:
  592. best_registry = _get_npm_registry()
  593. bunfig_content = constants.Bun.DEFAULT_CONFIG.format(registry=best_registry)
  594. bun_config_path.write_text(bunfig_content)
  595. def init_reflex_json(project_hash: int | None):
  596. """Write the hash of the Reflex project to a REFLEX_JSON.
  597. Reuse the hash if one is already created, therefore do not
  598. overwrite it every time we run the reflex init command
  599. .
  600. Args:
  601. project_hash: The app hash.
  602. """
  603. if project_hash is not None:
  604. console.debug(f"Project hash is already set to {project_hash}.")
  605. else:
  606. # Get a random project hash.
  607. project_hash = random.getrandbits(128)
  608. console.debug(f"Setting project hash to {project_hash}.")
  609. # Write the hash and version to the reflex json file.
  610. reflex_json = {
  611. "version": constants.Reflex.VERSION,
  612. "project_hash": project_hash,
  613. }
  614. path_ops.update_json_file(get_web_dir() / constants.Reflex.JSON, reflex_json)
  615. def update_next_config(export=False, transpile_packages: Optional[List[str]] = None):
  616. """Update Next.js config from Reflex config.
  617. Args:
  618. export: if the method run during reflex export.
  619. transpile_packages: list of packages to transpile via next.config.js.
  620. """
  621. next_config_file = get_web_dir() / constants.Next.CONFIG_FILE
  622. next_config = _update_next_config(
  623. get_config(), export=export, transpile_packages=transpile_packages
  624. )
  625. # Overwriting the next.config.js triggers a full server reload, so make sure
  626. # there is actually a diff.
  627. orig_next_config = next_config_file.read_text() if next_config_file.exists() else ""
  628. if orig_next_config != next_config:
  629. next_config_file.write_text(next_config)
  630. def _update_next_config(
  631. config: Config, export: bool = False, transpile_packages: Optional[List[str]] = None
  632. ):
  633. next_config = {
  634. "basePath": config.frontend_path or "",
  635. "compress": config.next_compression,
  636. "reactStrictMode": config.react_strict_mode,
  637. "trailingSlash": True,
  638. "staticPageGenerationTimeout": config.static_page_generation_timeout,
  639. }
  640. if transpile_packages:
  641. next_config["transpilePackages"] = list(
  642. {format_library_name(p) for p in transpile_packages}
  643. )
  644. if export:
  645. next_config["output"] = "export"
  646. next_config["distDir"] = constants.Dirs.STATIC
  647. next_config_json = re.sub(r'"([^"]+)"(?=:)', r"\1", json.dumps(next_config))
  648. return f"module.exports = {next_config_json};"
  649. def remove_existing_bun_installation():
  650. """Remove existing bun installation."""
  651. console.debug("Removing existing bun installation.")
  652. if Path(get_config().bun_path).exists():
  653. path_ops.rm(constants.Bun.ROOT_PATH)
  654. def download_and_run(url: str, *args, show_status: bool = False, **env):
  655. """Download and run a script.
  656. Args:
  657. url: The url of the script.
  658. args: The arguments to pass to the script.
  659. show_status: Whether to show the status of the script.
  660. env: The environment variables to use.
  661. """
  662. # Download the script
  663. console.debug(f"Downloading {url}")
  664. response = net.get(url)
  665. if response.status_code != httpx.codes.OK:
  666. response.raise_for_status()
  667. # Save the script to a temporary file.
  668. script = Path(tempfile.NamedTemporaryFile().name)
  669. script.write_text(response.text)
  670. # Run the script.
  671. env = {**os.environ, **env}
  672. process = processes.new_process(["bash", str(script), *args], env=env)
  673. show = processes.show_status if show_status else processes.show_logs
  674. show(f"Installing {url}", process)
  675. def download_and_extract_fnm_zip():
  676. """Download and run a script.
  677. Raises:
  678. Exit: If an error occurs while downloading or extracting the FNM zip.
  679. """
  680. # Download the zip file
  681. url = constants.Fnm.INSTALL_URL
  682. console.debug(f"Downloading {url}")
  683. fnm_zip_file: Path = constants.Fnm.DIR / f"{constants.Fnm.FILENAME}.zip"
  684. # Function to download and extract the FNM zip release.
  685. try:
  686. # Download the FNM zip release.
  687. # TODO: show progress to improve UX
  688. response = net.get(url, follow_redirects=True)
  689. response.raise_for_status()
  690. with fnm_zip_file.open("wb") as output_file:
  691. for chunk in response.iter_bytes():
  692. output_file.write(chunk)
  693. # Extract the downloaded zip file.
  694. with zipfile.ZipFile(fnm_zip_file, "r") as zip_ref:
  695. zip_ref.extractall(constants.Fnm.DIR)
  696. console.debug("FNM package downloaded and extracted successfully.")
  697. except Exception as e:
  698. console.error(f"An error occurred while downloading fnm package: {e}")
  699. raise typer.Exit(1) from e
  700. finally:
  701. # Clean up the downloaded zip file.
  702. path_ops.rm(fnm_zip_file)
  703. def install_node():
  704. """Install fnm and nodejs for use by Reflex.
  705. Independent of any existing system installations.
  706. """
  707. if not constants.Fnm.FILENAME:
  708. # fnm only support Linux, macOS and Windows distros.
  709. console.debug("")
  710. return
  711. # Skip installation if check_node_version() checks out
  712. if check_node_version():
  713. console.debug("Skipping node installation as it is already installed.")
  714. return
  715. path_ops.mkdir(constants.Fnm.DIR)
  716. if not constants.Fnm.EXE.exists():
  717. download_and_extract_fnm_zip()
  718. if constants.IS_WINDOWS:
  719. # Install node
  720. fnm_exe = Path(constants.Fnm.EXE).resolve()
  721. fnm_dir = Path(constants.Fnm.DIR).resolve()
  722. process = processes.new_process(
  723. [
  724. "powershell",
  725. "-Command",
  726. f'& "{fnm_exe}" install {constants.Node.VERSION} --fnm-dir "{fnm_dir}"',
  727. ],
  728. )
  729. else: # All other platforms (Linux, MacOS).
  730. # Add execute permissions to fnm executable.
  731. constants.Fnm.EXE.chmod(stat.S_IXUSR)
  732. # Install node.
  733. # Specify arm64 arch explicitly for M1s and M2s.
  734. architecture_arg = (
  735. ["--arch=arm64"]
  736. if platform.system() == "Darwin" and platform.machine() == "arm64"
  737. else []
  738. )
  739. process = processes.new_process(
  740. [
  741. constants.Fnm.EXE,
  742. "install",
  743. *architecture_arg,
  744. constants.Node.VERSION,
  745. "--fnm-dir",
  746. constants.Fnm.DIR,
  747. ],
  748. )
  749. processes.show_status("Installing node", process)
  750. def install_bun():
  751. """Install bun onto the user's system."""
  752. win_supported = is_windows_bun_supported()
  753. one_drive_in_path = windows_check_onedrive_in_path()
  754. if constants.IS_WINDOWS and (not win_supported or one_drive_in_path):
  755. if not win_supported:
  756. console.warn(
  757. "Bun for Windows is currently only available for x86 64-bit Windows. Installation will fall back on npm."
  758. )
  759. if one_drive_in_path:
  760. console.warn(
  761. "Creating project directories in OneDrive is not recommended for bun usage on windows. This will fallback to npm."
  762. )
  763. # Skip if bun is already installed.
  764. if Path(get_config().bun_path).exists() and get_bun_version() == version.parse(
  765. constants.Bun.VERSION
  766. ):
  767. console.debug("Skipping bun installation as it is already installed.")
  768. return
  769. # if unzip is installed
  770. if constants.IS_WINDOWS:
  771. processes.new_process(
  772. [
  773. "powershell",
  774. "-c",
  775. f"irm {constants.Bun.WINDOWS_INSTALL_URL}|iex",
  776. ],
  777. env={
  778. "BUN_INSTALL": str(constants.Bun.ROOT_PATH),
  779. "BUN_VERSION": constants.Bun.VERSION,
  780. },
  781. shell=True,
  782. run=True,
  783. show_logs=console.is_debug(),
  784. )
  785. else:
  786. unzip_path = path_ops.which("unzip")
  787. if unzip_path is None:
  788. raise_system_package_missing_error("unzip")
  789. # Run the bun install script.
  790. download_and_run(
  791. constants.Bun.INSTALL_URL,
  792. f"bun-v{constants.Bun.VERSION}",
  793. BUN_INSTALL=str(constants.Bun.ROOT_PATH),
  794. )
  795. def _write_cached_procedure_file(payload: str, cache_file: str | Path):
  796. cache_file = Path(cache_file)
  797. cache_file.write_text(payload)
  798. def _read_cached_procedure_file(cache_file: str | Path) -> str | None:
  799. cache_file = Path(cache_file)
  800. if cache_file.exists():
  801. return cache_file.read_text()
  802. return None
  803. def _clear_cached_procedure_file(cache_file: str | Path):
  804. cache_file = Path(cache_file)
  805. if cache_file.exists():
  806. cache_file.unlink()
  807. def cached_procedure(cache_file: str, payload_fn: Callable[..., str]):
  808. """Decorator to cache the runs of a procedure on disk. Procedures should not have
  809. a return value.
  810. Args:
  811. cache_file: The file to store the cache payload in.
  812. payload_fn: Function that computes cache payload from function args
  813. Returns:
  814. The decorated function.
  815. """
  816. def _inner_decorator(func):
  817. def _inner(*args, **kwargs):
  818. payload = _read_cached_procedure_file(cache_file)
  819. new_payload = payload_fn(*args, **kwargs)
  820. if payload != new_payload:
  821. _clear_cached_procedure_file(cache_file)
  822. func(*args, **kwargs)
  823. _write_cached_procedure_file(new_payload, cache_file)
  824. return _inner
  825. return _inner_decorator
  826. @cached_procedure(
  827. cache_file=str(get_web_dir() / "reflex.install_frontend_packages.cached"),
  828. payload_fn=lambda p, c: f"{sorted(p)!r},{c.json()}",
  829. )
  830. def install_frontend_packages(packages: set[str], config: Config):
  831. """Installs the base and custom frontend packages.
  832. Args:
  833. packages: A list of package names to be installed.
  834. config: The config object.
  835. Raises:
  836. FileNotFoundError: If the package manager is not found.
  837. Example:
  838. >>> install_frontend_packages(["react", "react-dom"], get_config())
  839. """
  840. # unsupported archs(arm and 32bit machines) will use npm anyway. so we dont have to run npm twice
  841. fallback_command = (
  842. get_package_manager(on_failure_return_none=True)
  843. if (
  844. not constants.IS_WINDOWS
  845. or (
  846. constants.IS_WINDOWS
  847. and (
  848. is_windows_bun_supported() and not windows_check_onedrive_in_path()
  849. )
  850. )
  851. )
  852. else None
  853. )
  854. install_package_manager = (
  855. get_install_package_manager(on_failure_return_none=True) or fallback_command
  856. )
  857. if install_package_manager is None:
  858. raise FileNotFoundError(
  859. "Could not find a package manager to install frontend packages. You may need to run `reflex init`."
  860. )
  861. fallback_command = (
  862. fallback_command if fallback_command is not install_package_manager else None
  863. )
  864. processes.run_process_with_fallback(
  865. [install_package_manager, "install"], # type: ignore
  866. fallback=fallback_command,
  867. analytics_enabled=True,
  868. show_status_message="Installing base frontend packages",
  869. cwd=get_web_dir(),
  870. shell=constants.IS_WINDOWS,
  871. )
  872. if config.tailwind is not None:
  873. processes.run_process_with_fallback(
  874. [
  875. install_package_manager,
  876. "add",
  877. "-d",
  878. constants.Tailwind.VERSION,
  879. *((config.tailwind or {}).get("plugins", [])),
  880. ],
  881. fallback=fallback_command,
  882. analytics_enabled=True,
  883. show_status_message="Installing tailwind",
  884. cwd=get_web_dir(),
  885. shell=constants.IS_WINDOWS,
  886. )
  887. # Install custom packages defined in frontend_packages
  888. if len(packages) > 0:
  889. processes.run_process_with_fallback(
  890. [install_package_manager, "add", *packages],
  891. fallback=fallback_command,
  892. analytics_enabled=True,
  893. show_status_message="Installing frontend packages from config and components",
  894. cwd=get_web_dir(),
  895. shell=constants.IS_WINDOWS,
  896. )
  897. def needs_reinit(frontend: bool = True) -> bool:
  898. """Check if an app needs to be reinitialized.
  899. Args:
  900. frontend: Whether to check if the frontend is initialized.
  901. Returns:
  902. Whether the app needs to be reinitialized.
  903. Raises:
  904. Exit: If the app is not initialized.
  905. """
  906. if not constants.Config.FILE.exists():
  907. console.error(
  908. f"[cyan]{constants.Config.FILE}[/cyan] not found. Move to the root folder of your project, or run [bold]{constants.Reflex.MODULE_NAME} init[/bold] to start a new project."
  909. )
  910. raise typer.Exit(1)
  911. # Don't need to reinit if not running in frontend mode.
  912. if not frontend:
  913. return False
  914. # Make sure the .reflex directory exists.
  915. if not environment.REFLEX_DIR.get().exists():
  916. return True
  917. # Make sure the .web directory exists in frontend mode.
  918. if not get_web_dir().exists():
  919. return True
  920. # If the template is out of date, then we need to re-init
  921. if not is_latest_template():
  922. return True
  923. if constants.IS_WINDOWS:
  924. console.warn(
  925. """Windows Subsystem for Linux (WSL) is recommended for improving initial install times."""
  926. )
  927. if windows_check_onedrive_in_path():
  928. console.warn(
  929. "Creating project directories in OneDrive may lead to performance issues. For optimal performance, It is recommended to avoid using OneDrive for your reflex app."
  930. )
  931. # No need to reinitialize if the app is already initialized.
  932. return False
  933. def is_latest_template() -> bool:
  934. """Whether the app is using the latest template.
  935. Returns:
  936. Whether the app is using the latest template.
  937. """
  938. json_file = get_web_dir() / constants.Reflex.JSON
  939. if not json_file.exists():
  940. return False
  941. app_version = json.loads(json_file.read_text()).get("version")
  942. return app_version == constants.Reflex.VERSION
  943. def validate_bun():
  944. """Validate bun if a custom bun path is specified to ensure the bun version meets requirements.
  945. Raises:
  946. Exit: If custom specified bun does not exist or does not meet requirements.
  947. """
  948. # if a custom bun path is provided, make sure its valid
  949. # This is specific to non-FHS OS
  950. bun_path = get_config().bun_path
  951. if path_ops.use_system_bun():
  952. bun_path = path_ops.which("bun")
  953. if bun_path != constants.Bun.DEFAULT_PATH:
  954. console.info(f"Using custom Bun path: {bun_path}")
  955. bun_version = get_bun_version()
  956. if not bun_version:
  957. console.error(
  958. "Failed to obtain bun version. Make sure the specified bun path in your config is correct."
  959. )
  960. raise typer.Exit(1)
  961. elif bun_version < version.parse(constants.Bun.MIN_VERSION):
  962. console.error(
  963. f"Reflex requires bun version {constants.Bun.VERSION} or higher to run, but the detected version is "
  964. f"{bun_version}. If you have specified a custom bun path in your config, make sure to provide one "
  965. f"that satisfies the minimum version requirement."
  966. )
  967. raise typer.Exit(1)
  968. def validate_frontend_dependencies(init=True):
  969. """Validate frontend dependencies to ensure they meet requirements.
  970. Args:
  971. init: whether running `reflex init`
  972. Raises:
  973. Exit: If the package manager is invalid.
  974. """
  975. if not init:
  976. # we only need to validate the package manager when running app.
  977. # `reflex init` will install the deps anyway(if applied).
  978. package_manager = get_package_manager()
  979. if not package_manager:
  980. console.error(
  981. "Could not find NPM package manager. Make sure you have node installed."
  982. )
  983. raise typer.Exit(1)
  984. if not check_node_version():
  985. node_version = get_node_version()
  986. console.error(
  987. f"Reflex requires node version {constants.Node.MIN_VERSION} or higher to run, but the detected version is {node_version}",
  988. )
  989. raise typer.Exit(1)
  990. if init:
  991. # we only need bun for package install on `reflex init`.
  992. validate_bun()
  993. def ensure_reflex_installation_id() -> Optional[int]:
  994. """Ensures that a reflex distinct id has been generated and stored in the reflex directory.
  995. Returns:
  996. Distinct id.
  997. """
  998. try:
  999. initialize_reflex_user_directory()
  1000. installation_id_file = environment.REFLEX_DIR.get() / "installation_id"
  1001. installation_id = None
  1002. if installation_id_file.exists():
  1003. with contextlib.suppress(Exception):
  1004. installation_id = int(installation_id_file.read_text())
  1005. # If anything goes wrong at all... just regenerate.
  1006. # Like what? Examples:
  1007. # - file not exists
  1008. # - file not readable
  1009. # - content not parseable as an int
  1010. if installation_id is None:
  1011. installation_id = random.getrandbits(128)
  1012. installation_id_file.write_text(str(installation_id))
  1013. # If we get here, installation_id is definitely set
  1014. return installation_id
  1015. except Exception as e:
  1016. console.debug(f"Failed to ensure reflex installation id: {e}")
  1017. return None
  1018. def initialize_reflex_user_directory():
  1019. """Initialize the reflex user directory."""
  1020. # Create the reflex directory.
  1021. path_ops.mkdir(environment.REFLEX_DIR.get())
  1022. def initialize_frontend_dependencies():
  1023. """Initialize all the frontend dependencies."""
  1024. # validate dependencies before install
  1025. validate_frontend_dependencies()
  1026. # Avoid warning about Node installation while we're trying to install it.
  1027. global CURRENTLY_INSTALLING_NODE
  1028. CURRENTLY_INSTALLING_NODE = True
  1029. # Install the frontend dependencies.
  1030. processes.run_concurrently(install_node, install_bun)
  1031. CURRENTLY_INSTALLING_NODE = False
  1032. # Set up the web directory.
  1033. initialize_web_directory()
  1034. def check_db_used() -> bool:
  1035. """Check if the database is used.
  1036. Returns:
  1037. True if the database is used.
  1038. """
  1039. return bool(get_config().db_url)
  1040. def check_redis_used() -> bool:
  1041. """Check if Redis is used.
  1042. Returns:
  1043. True if Redis is used.
  1044. """
  1045. return bool(get_config().redis_url)
  1046. def check_db_initialized() -> bool:
  1047. """Check if the database migrations are initialized.
  1048. Returns:
  1049. True if alembic is initialized (or if database is not used).
  1050. """
  1051. if (
  1052. get_config().db_url is not None
  1053. and not environment.ALEMBIC_CONFIG.get().exists()
  1054. ):
  1055. console.error(
  1056. "Database is not initialized. Run [bold]reflex db init[/bold] first."
  1057. )
  1058. return False
  1059. return True
  1060. def check_schema_up_to_date():
  1061. """Check if the sqlmodel metadata matches the current database schema."""
  1062. if get_config().db_url is None or not environment.ALEMBIC_CONFIG.get().exists():
  1063. return
  1064. with model.Model.get_db_engine().connect() as connection:
  1065. try:
  1066. if model.Model.alembic_autogenerate(
  1067. connection=connection,
  1068. write_migration_scripts=False,
  1069. ):
  1070. console.error(
  1071. "Detected database schema changes. Run [bold]reflex db makemigrations[/bold] "
  1072. "to generate migration scripts.",
  1073. )
  1074. except CommandError as command_error:
  1075. if "Target database is not up to date." in str(command_error):
  1076. console.error(
  1077. f"{command_error} Run [bold]reflex db migrate[/bold] to update database."
  1078. )
  1079. def prompt_for_template_options(templates: list[Template]) -> str:
  1080. """Prompt the user to specify a template.
  1081. Args:
  1082. templates: The templates to choose from.
  1083. Returns:
  1084. The template name the user selects.
  1085. """
  1086. # Show the user the URLs of each template to preview.
  1087. console.print("\nGet started with a template:")
  1088. def format_demo_url_str(url: str) -> str:
  1089. return f" ({url})" if url else ""
  1090. # Prompt the user to select a template.
  1091. id_to_name = {
  1092. str(
  1093. idx
  1094. ): f"{template.name.replace('_', ' ').replace('-', ' ')}{format_demo_url_str(template.demo_url)} - {template.description}"
  1095. for idx, template in enumerate(templates)
  1096. }
  1097. for id in range(len(id_to_name)):
  1098. console.print(f"({id}) {id_to_name[str(id)]}")
  1099. template = console.ask(
  1100. "Which template would you like to use?",
  1101. choices=[str(i) for i in range(len(id_to_name))],
  1102. show_choices=False,
  1103. default="0",
  1104. )
  1105. # Return the template.
  1106. return templates[int(template)].name
  1107. def fetch_app_templates(version: str) -> dict[str, Template]:
  1108. """Fetch a dict of templates from the templates repo using github API.
  1109. Args:
  1110. version: The version of the templates to fetch.
  1111. Returns:
  1112. The dict of templates.
  1113. """
  1114. def get_release_by_tag(tag: str) -> dict | None:
  1115. response = net.get(constants.Reflex.RELEASES_URL)
  1116. response.raise_for_status()
  1117. releases = response.json()
  1118. for release in releases:
  1119. if release["tag_name"] == f"v{tag}":
  1120. return release
  1121. return None
  1122. release = get_release_by_tag(version)
  1123. if release is None:
  1124. console.warn(f"No templates known for version {version}")
  1125. return {}
  1126. assets = release.get("assets", [])
  1127. asset = next((a for a in assets if a["name"] == "templates.json"), None)
  1128. if asset is None:
  1129. console.warn(f"Templates metadata not found for version {version}")
  1130. return {}
  1131. else:
  1132. templates_url = asset["browser_download_url"]
  1133. templates_data = net.get(templates_url, follow_redirects=True).json()["templates"]
  1134. for template in templates_data:
  1135. if template["name"] == "blank":
  1136. template["code_url"] = ""
  1137. continue
  1138. template["code_url"] = next(
  1139. (
  1140. a["browser_download_url"]
  1141. for a in assets
  1142. if a["name"] == f"{template['name']}.zip"
  1143. ),
  1144. None,
  1145. )
  1146. filtered_templates = {}
  1147. for tp in templates_data:
  1148. if tp["hidden"] or tp["code_url"] is None:
  1149. continue
  1150. known_fields = {f.name for f in dataclasses.fields(Template)}
  1151. filtered_templates[tp["name"]] = Template(
  1152. **{k: v for k, v in tp.items() if k in known_fields}
  1153. )
  1154. return filtered_templates
  1155. def create_config_init_app_from_remote_template(app_name: str, template_url: str):
  1156. """Create new rxconfig and initialize app using a remote template.
  1157. Args:
  1158. app_name: The name of the app.
  1159. template_url: The path to the template source code as a zip file.
  1160. Raises:
  1161. Exit: If any download, file operations fail or unexpected zip file format.
  1162. """
  1163. # Create a temp directory for the zip download.
  1164. try:
  1165. temp_dir = tempfile.mkdtemp()
  1166. except OSError as ose:
  1167. console.error(f"Failed to create temp directory for download: {ose}")
  1168. raise typer.Exit(1) from ose
  1169. # Use httpx GET with redirects to download the zip file.
  1170. zip_file_path: Path = Path(temp_dir) / "template.zip"
  1171. try:
  1172. # Note: following redirects can be risky. We only allow this for reflex built templates at the moment.
  1173. response = net.get(template_url, follow_redirects=True)
  1174. console.debug(f"Server responded download request: {response}")
  1175. response.raise_for_status()
  1176. except httpx.HTTPError as he:
  1177. console.error(f"Failed to download the template: {he}")
  1178. raise typer.Exit(1) from he
  1179. try:
  1180. zip_file_path.write_bytes(response.content)
  1181. console.debug(f"Downloaded the zip to {zip_file_path}")
  1182. except OSError as ose:
  1183. console.error(f"Unable to write the downloaded zip to disk {ose}")
  1184. raise typer.Exit(1) from ose
  1185. # Create a temp directory for the zip extraction.
  1186. try:
  1187. unzip_dir = Path(tempfile.mkdtemp())
  1188. except OSError as ose:
  1189. console.error(f"Failed to create temp directory for extracting zip: {ose}")
  1190. raise typer.Exit(1) from ose
  1191. try:
  1192. zipfile.ZipFile(zip_file_path).extractall(path=unzip_dir)
  1193. # The zip file downloaded from github looks like:
  1194. # repo-name-branch/**/*, so we need to remove the top level directory.
  1195. if len(subdirs := os.listdir(unzip_dir)) != 1:
  1196. console.error(f"Expected one directory in the zip, found {subdirs}")
  1197. raise typer.Exit(1)
  1198. template_dir = unzip_dir / subdirs[0]
  1199. console.debug(f"Template folder is located at {template_dir}")
  1200. except Exception as uze:
  1201. console.error(f"Failed to unzip the template: {uze}")
  1202. raise typer.Exit(1) from uze
  1203. # Move the rxconfig file here first.
  1204. path_ops.mv(str(template_dir / constants.Config.FILE), constants.Config.FILE)
  1205. new_config = get_config(reload=True)
  1206. # Get the template app's name from rxconfig in case it is different than
  1207. # the source code repo name on github.
  1208. template_name = new_config.app_name
  1209. create_config(app_name)
  1210. initialize_app_directory(
  1211. app_name,
  1212. template_name=template_name,
  1213. template_code_dir_name=template_name,
  1214. template_dir=template_dir,
  1215. )
  1216. req_file = Path("requirements.txt")
  1217. if req_file.exists() and len(req_file.read_text().splitlines()) > 1:
  1218. console.info(
  1219. "Run `pip install -r requirements.txt` to install the required python packages for this template."
  1220. )
  1221. # Clean up the temp directories.
  1222. shutil.rmtree(temp_dir)
  1223. shutil.rmtree(unzip_dir)
  1224. def initialize_default_app(app_name: str):
  1225. """Initialize the default app.
  1226. Args:
  1227. app_name: The name of the app.
  1228. """
  1229. create_config(app_name)
  1230. initialize_app_directory(app_name)
  1231. def validate_and_create_app_using_remote_template(app_name, template, templates):
  1232. """Validate and create an app using a remote template.
  1233. Args:
  1234. app_name: The name of the app.
  1235. template: The name of the template.
  1236. templates: The available templates.
  1237. Raises:
  1238. Exit: If the template is not found.
  1239. """
  1240. # If user selects a template, it needs to exist.
  1241. if template in templates:
  1242. from reflex_cli.v2.utils import hosting
  1243. authenticated_token = hosting.authenticated_token()
  1244. if not authenticated_token or not authenticated_token[0]:
  1245. console.print(
  1246. f"Please use `reflex login` to access the '{template}' template."
  1247. )
  1248. raise typer.Exit(3)
  1249. template_url = templates[template].code_url
  1250. else:
  1251. # Check if the template is a github repo.
  1252. if template.startswith("https://github.com"):
  1253. template_url = f"{template.strip('/').replace('.git', '')}/archive/main.zip"
  1254. else:
  1255. console.error(f"Template `{template}` not found or invalid.")
  1256. raise typer.Exit(1)
  1257. if template_url is None:
  1258. return
  1259. create_config_init_app_from_remote_template(
  1260. app_name=app_name, template_url=template_url
  1261. )
  1262. def generate_template_using_ai(template: str | None = None) -> str:
  1263. """Generate a template using AI(Flexgen).
  1264. Args:
  1265. template: The name of the template.
  1266. Returns:
  1267. The generation hash.
  1268. Raises:
  1269. Exit: If the template and ai flags are used.
  1270. """
  1271. if template is None:
  1272. # If AI is requested and no template specified, redirect the user to reflex.build.
  1273. return redir.reflex_build_redirect()
  1274. elif is_generation_hash(template):
  1275. # Otherwise treat the template as a generation hash.
  1276. return template
  1277. else:
  1278. console.error(
  1279. "Cannot use `--template` option with `--ai` option. Please remove `--template` option."
  1280. )
  1281. raise typer.Exit(2)
  1282. def fetch_remote_templates(
  1283. template: str,
  1284. ) -> tuple[str, dict[str, Template]]:
  1285. """Fetch the available remote templates.
  1286. Args:
  1287. template: The name of the template.
  1288. Returns:
  1289. The selected template and the available templates.
  1290. """
  1291. available_templates = {}
  1292. try:
  1293. # Get the available templates
  1294. available_templates = fetch_app_templates(constants.Reflex.VERSION)
  1295. except Exception as e:
  1296. console.warn("Failed to fetch templates. Falling back to default template.")
  1297. console.debug(f"Error while fetching templates: {e}")
  1298. template = constants.Templates.DEFAULT
  1299. return template, available_templates
  1300. def initialize_app(
  1301. app_name: str, template: str | None = None, ai: bool = False
  1302. ) -> str | None:
  1303. """Initialize the app either from a remote template or a blank app. If the config file exists, it is considered as reinit.
  1304. Args:
  1305. app_name: The name of the app.
  1306. template: The name of the template to use.
  1307. ai: Whether to use AI to generate the template.
  1308. Returns:
  1309. The name of the template.
  1310. Raises:
  1311. Exit: If the template is not valid or unspecified.
  1312. """
  1313. # Local imports to avoid circular imports.
  1314. from reflex.utils import telemetry
  1315. # Check if the app is already initialized.
  1316. if constants.Config.FILE.exists():
  1317. telemetry.send("reinit")
  1318. return
  1319. generation_hash = None
  1320. if ai:
  1321. generation_hash = generate_template_using_ai(template)
  1322. template = constants.Templates.DEFAULT
  1323. templates: dict[str, Template] = {}
  1324. # Don't fetch app templates if the user directly asked for DEFAULT.
  1325. if template is not None and (template not in (constants.Templates.DEFAULT,)):
  1326. template, templates = fetch_remote_templates(template)
  1327. if template is None:
  1328. template = prompt_for_template_options(get_init_cli_prompt_options())
  1329. if template == constants.Templates.AI:
  1330. generation_hash = generate_template_using_ai()
  1331. # change to the default to allow creation of default app
  1332. template = constants.Templates.DEFAULT
  1333. elif template == constants.Templates.CHOOSE_TEMPLATES:
  1334. console.print(
  1335. f"Go to the templates page ({constants.Templates.REFLEX_TEMPLATES_URL}) and copy the command to init with a template."
  1336. )
  1337. raise typer.Exit(0)
  1338. # If the blank template is selected, create a blank app.
  1339. if template in (constants.Templates.DEFAULT,):
  1340. # Default app creation behavior: a blank app.
  1341. initialize_default_app(app_name)
  1342. else:
  1343. validate_and_create_app_using_remote_template(
  1344. app_name=app_name, template=template, templates=templates
  1345. )
  1346. # If a reflex.build generation hash is available, download the code and apply it to the main module.
  1347. if generation_hash:
  1348. initialize_main_module_index_from_generation(
  1349. app_name, generation_hash=generation_hash
  1350. )
  1351. telemetry.send("init", template=template)
  1352. return template
  1353. def get_init_cli_prompt_options() -> list[Template]:
  1354. """Get the CLI options for initializing a Reflex app.
  1355. Returns:
  1356. The CLI options.
  1357. """
  1358. return [
  1359. Template(
  1360. name=constants.Templates.DEFAULT,
  1361. description="A blank Reflex app.",
  1362. demo_url=constants.Templates.DEFAULT_TEMPLATE_URL,
  1363. code_url="",
  1364. ),
  1365. Template(
  1366. name=constants.Templates.AI,
  1367. description="Generate a template using AI [Experimental]",
  1368. demo_url="",
  1369. code_url="",
  1370. ),
  1371. Template(
  1372. name=constants.Templates.CHOOSE_TEMPLATES,
  1373. description="Choose an existing template.",
  1374. demo_url="",
  1375. code_url="",
  1376. ),
  1377. ]
  1378. def initialize_main_module_index_from_generation(app_name: str, generation_hash: str):
  1379. """Overwrite the `index` function in the main module with reflex.build generated code.
  1380. Args:
  1381. app_name: The name of the app.
  1382. generation_hash: The generation hash from reflex.build.
  1383. Raises:
  1384. GeneratedCodeHasNoFunctionDefs: If the fetched code has no function definitions
  1385. (the refactored reflex code is expected to have at least one root function defined).
  1386. """
  1387. # Download the reflex code for the generation.
  1388. url = constants.Templates.REFLEX_BUILD_CODE_URL.format(
  1389. generation_hash=generation_hash
  1390. )
  1391. resp = net.get(url)
  1392. while resp.status_code == httpx.codes.SERVICE_UNAVAILABLE:
  1393. console.debug("Waiting for the code to be generated...")
  1394. time.sleep(1)
  1395. resp = net.get(url)
  1396. resp.raise_for_status()
  1397. # Determine the name of the last function, which renders the generated code.
  1398. defined_funcs = re.findall(r"def ([a-zA-Z_]+)\(", resp.text)
  1399. if not defined_funcs:
  1400. raise GeneratedCodeHasNoFunctionDefs(
  1401. f"No function definitions found in generated code from {url!r}."
  1402. )
  1403. render_func_name = defined_funcs[-1]
  1404. def replace_content(_match):
  1405. return "\n".join(
  1406. [
  1407. resp.text,
  1408. "",
  1409. "" "def index() -> rx.Component:",
  1410. f" return {render_func_name}()",
  1411. "",
  1412. "",
  1413. ],
  1414. )
  1415. main_module_path = Path(app_name, app_name + constants.Ext.PY)
  1416. main_module_code = main_module_path.read_text()
  1417. main_module_code = re.sub(
  1418. r"def index\(\).*:\n([^\n]\s+.*\n+)+",
  1419. replace_content,
  1420. main_module_code,
  1421. )
  1422. # Make the app use light mode until flexgen enforces the conversion of
  1423. # tailwind colors to radix colors.
  1424. main_module_code = re.sub(
  1425. r"app\s*=\s*rx\.App\(\s*\)",
  1426. 'app = rx.App(theme=rx.theme(color_mode="light"))',
  1427. main_module_code,
  1428. )
  1429. main_module_path.write_text(main_module_code)
  1430. def format_address_width(address_width) -> int | None:
  1431. """Cast address width to an int.
  1432. Args:
  1433. address_width: The address width.
  1434. Returns:
  1435. Address width int
  1436. """
  1437. try:
  1438. return int(address_width) if address_width else None
  1439. except ValueError:
  1440. return None
  1441. @functools.lru_cache(maxsize=None)
  1442. def get_cpu_info() -> CpuInfo | None:
  1443. """Get the CPU info of the underlining host.
  1444. Returns:
  1445. The CPU info.
  1446. """
  1447. platform_os = platform.system()
  1448. cpuinfo = {}
  1449. try:
  1450. if platform_os == "Windows":
  1451. cmd = "wmic cpu get addresswidth,caption,manufacturer /FORMAT:csv"
  1452. output = processes.execute_command_and_return_output(cmd)
  1453. if output:
  1454. val = output.splitlines()[-1].split(",")[1:]
  1455. cpuinfo["manufacturer_id"] = val[2]
  1456. cpuinfo["model_name"] = val[1].split("Family")[0].strip()
  1457. cpuinfo["address_width"] = format_address_width(val[0])
  1458. elif platform_os == "Linux":
  1459. output = processes.execute_command_and_return_output("lscpu")
  1460. if output:
  1461. lines = output.split("\n")
  1462. for line in lines:
  1463. if "Architecture" in line:
  1464. cpuinfo["address_width"] = (
  1465. 64 if line.split(":")[1].strip() == "x86_64" else 32
  1466. )
  1467. if "Vendor ID:" in line:
  1468. cpuinfo["manufacturer_id"] = line.split(":")[1].strip()
  1469. if "Model name" in line:
  1470. cpuinfo["model_name"] = line.split(":")[1].strip()
  1471. elif platform_os == "Darwin":
  1472. cpuinfo["address_width"] = format_address_width(
  1473. processes.execute_command_and_return_output("getconf LONG_BIT")
  1474. )
  1475. cpuinfo["manufacturer_id"] = processes.execute_command_and_return_output(
  1476. "sysctl -n machdep.cpu.brand_string"
  1477. )
  1478. cpuinfo["model_name"] = processes.execute_command_and_return_output(
  1479. "uname -m"
  1480. )
  1481. except Exception as err:
  1482. console.error(f"Failed to retrieve CPU info. {err}")
  1483. return None
  1484. return (
  1485. CpuInfo(
  1486. manufacturer_id=cpuinfo.get("manufacturer_id"),
  1487. model_name=cpuinfo.get("model_name"),
  1488. address_width=cpuinfo.get("address_width"),
  1489. )
  1490. if cpuinfo
  1491. else None
  1492. )
  1493. @functools.lru_cache(maxsize=None)
  1494. def is_windows_bun_supported() -> bool:
  1495. """Check whether the underlining host running windows qualifies to run bun.
  1496. We typically do not run bun on ARM or 32 bit devices that use windows.
  1497. Returns:
  1498. Whether the host is qualified to use bun.
  1499. """
  1500. cpu_info = get_cpu_info()
  1501. return (
  1502. constants.IS_WINDOWS
  1503. and cpu_info is not None
  1504. and cpu_info.address_width == 64
  1505. and cpu_info.model_name is not None
  1506. and "ARM" not in cpu_info.model_name
  1507. )
  1508. def is_generation_hash(template: str) -> bool:
  1509. """Check if the template looks like a generation hash.
  1510. Args:
  1511. template: The template name.
  1512. Returns:
  1513. True if the template is composed of 32 or more hex characters.
  1514. """
  1515. return re.match(r"^[0-9a-f]{32,}$", template) is not None