exec.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. """Everything regarding execution of the built app."""
  2. from __future__ import annotations
  3. import os
  4. import platform
  5. import subprocess
  6. from pathlib import Path
  7. from typing import TYPE_CHECKING
  8. import uvicorn
  9. from pynecone import constants
  10. from pynecone.config import get_config
  11. from pynecone.utils import console, prerequisites, processes
  12. from pynecone.utils.build import export_app, setup_backend, setup_frontend
  13. from pynecone.utils.watch import AssetFolderWatch
  14. if TYPE_CHECKING:
  15. from pynecone.app import App
  16. def start_watching_assets_folder(root):
  17. """Start watching assets folder.
  18. Args:
  19. root: root path of the project.
  20. """
  21. asset_watch = AssetFolderWatch(root)
  22. asset_watch.start()
  23. def run_frontend(app: App, root: Path, port: str):
  24. """Run the frontend.
  25. Args:
  26. app: The app.
  27. root: root path of the project.
  28. port: port of the app.
  29. """
  30. # Set up the frontend.
  31. setup_frontend(root)
  32. # start watching asset folder
  33. start_watching_assets_folder(root)
  34. # Compile the frontend.
  35. app.compile(force_compile=True)
  36. # Run the frontend in development mode.
  37. console.rule("[bold green]App Running")
  38. os.environ["PORT"] = get_config().port if port is None else port
  39. # Run the frontend in development mode.
  40. subprocess.Popen(
  41. [prerequisites.get_package_manager(), "run", "dev"],
  42. cwd=constants.WEB_DIR,
  43. env=os.environ,
  44. )
  45. def run_frontend_prod(app: App, root: Path, port: str):
  46. """Run the frontend.
  47. Args:
  48. app: The app.
  49. root: root path of the project.
  50. port: port of the app.
  51. """
  52. # Set up the frontend.
  53. setup_frontend(root)
  54. # Export the app.
  55. export_app(app)
  56. # Set the port.
  57. os.environ["PORT"] = get_config().port if port is None else port
  58. # Run the frontend in production mode.
  59. subprocess.Popen(
  60. [prerequisites.get_package_manager(), "run", "prod"],
  61. cwd=constants.WEB_DIR,
  62. env=os.environ,
  63. )
  64. def run_backend(
  65. app_name: str, port: int, loglevel: constants.LogLevel = constants.LogLevel.ERROR
  66. ):
  67. """Run the backend.
  68. Args:
  69. app_name: The app name.
  70. port: The app port
  71. loglevel: The log level.
  72. """
  73. setup_backend()
  74. uvicorn.run(
  75. f"{app_name}:{constants.APP_VAR}.{constants.API_VAR}",
  76. host=constants.BACKEND_HOST,
  77. port=port,
  78. log_level=loglevel,
  79. reload=True,
  80. )
  81. def run_backend_prod(
  82. app_name: str, port: int, loglevel: constants.LogLevel = constants.LogLevel.ERROR
  83. ):
  84. """Run the backend.
  85. Args:
  86. app_name: The app name.
  87. port: The app port
  88. loglevel: The log level.
  89. """
  90. setup_backend()
  91. num_workers = processes.get_num_workers()
  92. command = (
  93. [
  94. *constants.RUN_BACKEND_PROD_WINDOWS,
  95. "--host",
  96. "0.0.0.0",
  97. "--port",
  98. str(port),
  99. f"{app_name}:{constants.APP_VAR}",
  100. ]
  101. if platform.system() == "Windows"
  102. else [
  103. *constants.RUN_BACKEND_PROD,
  104. "--bind",
  105. f"0.0.0.0:{port}",
  106. "--threads",
  107. str(num_workers),
  108. f"{app_name}:{constants.APP_VAR}()",
  109. ]
  110. )
  111. command += [
  112. "--log-level",
  113. loglevel.value,
  114. "--workers",
  115. str(num_workers),
  116. ]
  117. subprocess.run(command)