prerequisites.py 64 KB

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