prerequisites.py 62 KB

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