exec.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. """Everything regarding execution of the built app."""
  2. from __future__ import annotations
  3. import os
  4. from pathlib import Path
  5. from reflex import constants
  6. from reflex.config import get_config
  7. from reflex.utils import console, prerequisites, processes
  8. from reflex.utils.watch import AssetFolderWatch
  9. def start_watching_assets_folder(root):
  10. """Start watching assets folder.
  11. Args:
  12. root: root path of the project.
  13. """
  14. asset_watch = AssetFolderWatch(root)
  15. asset_watch.start()
  16. def run_process_and_launch_url(
  17. run_command: list[str],
  18. ):
  19. """Run the process and launch the URL.
  20. Args:
  21. run_command: The command to run.
  22. """
  23. process = processes.new_process(
  24. run_command,
  25. cwd=constants.WEB_DIR,
  26. )
  27. if process.stdout:
  28. for line in process.stdout:
  29. if "ready started server on" in line:
  30. url = line.split("url: ")[-1].strip()
  31. console.print(f"App running at: [bold green]{url}")
  32. else:
  33. console.debug(line)
  34. def run_frontend(
  35. root: Path,
  36. port: str,
  37. ):
  38. """Run the frontend.
  39. Args:
  40. root: The root path of the project.
  41. port: The port to run the frontend on.
  42. """
  43. # Start watching asset folder.
  44. start_watching_assets_folder(root)
  45. # Run the frontend in development mode.
  46. console.rule("[bold green]App Running")
  47. os.environ["PORT"] = get_config().frontend_port if port is None else port
  48. run_process_and_launch_url([prerequisites.get_package_manager(), "run", "dev"])
  49. def run_frontend_prod(
  50. root: Path,
  51. port: str,
  52. ):
  53. """Run the frontend.
  54. Args:
  55. root: The root path of the project (to keep same API as run_frontend).
  56. port: The port to run the frontend on.
  57. """
  58. # Set the port.
  59. os.environ["PORT"] = get_config().frontend_port if port is None else port
  60. # Run the frontend in production mode.
  61. console.rule("[bold green]App Running")
  62. run_process_and_launch_url([constants.NPM_PATH, "run", "prod"])
  63. def run_backend(
  64. app_name: str,
  65. host: str,
  66. port: int,
  67. loglevel: constants.LogLevel = constants.LogLevel.ERROR,
  68. ):
  69. """Run the backend.
  70. Args:
  71. host: The app host
  72. app_name: The app name.
  73. port: The app port
  74. loglevel: The log level.
  75. """
  76. processes.new_process(
  77. [
  78. "uvicorn",
  79. f"{app_name}:{constants.APP_VAR}.{constants.API_VAR}",
  80. "--host",
  81. host,
  82. "--port",
  83. str(port),
  84. "--log-level",
  85. loglevel,
  86. "--reload",
  87. "--reload-dir",
  88. app_name.split(".")[0],
  89. ],
  90. run=True,
  91. show_logs=True,
  92. )
  93. def run_backend_prod(
  94. app_name: str,
  95. host: str,
  96. port: int,
  97. loglevel: constants.LogLevel = constants.LogLevel.ERROR,
  98. ):
  99. """Run the backend.
  100. Args:
  101. host: The app host
  102. app_name: The app name.
  103. port: The app port
  104. loglevel: The log level.
  105. """
  106. num_workers = processes.get_num_workers()
  107. command = (
  108. [
  109. *constants.RUN_BACKEND_PROD_WINDOWS,
  110. "--host",
  111. host,
  112. "--port",
  113. str(port),
  114. f"{app_name}:{constants.APP_VAR}",
  115. ]
  116. if prerequisites.IS_WINDOWS
  117. else [
  118. *constants.RUN_BACKEND_PROD,
  119. "--bind",
  120. f"{host}:{port}",
  121. "--threads",
  122. str(num_workers),
  123. f"{app_name}:{constants.APP_VAR}()",
  124. ]
  125. )
  126. command += [
  127. "--log-level",
  128. loglevel.value,
  129. "--workers",
  130. str(num_workers),
  131. ]
  132. processes.new_process(command, run=True, show_logs=True)