base.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. """Base file for constants that don't fit any other categories."""
  2. from __future__ import annotations
  3. import platform
  4. from enum import Enum
  5. from importlib import metadata
  6. from pathlib import Path
  7. from types import SimpleNamespace
  8. from platformdirs import PlatformDirs
  9. from .utils import classproperty
  10. IS_WINDOWS = platform.system() == "Windows"
  11. IS_MACOS = platform.system() == "Darwin"
  12. IS_LINUX = platform.system() == "Linux"
  13. class Dirs(SimpleNamespace):
  14. """Various directories/paths used by Reflex."""
  15. # The frontend directories in a project.
  16. # The web folder where the NextJS app is compiled to.
  17. WEB = ".web"
  18. # The directory where uploaded files are stored.
  19. UPLOADED_FILES = "uploaded_files"
  20. # The name of the assets directory.
  21. APP_ASSETS = "assets"
  22. # The name of the assets directory for external resources (a subfolder of APP_ASSETS).
  23. EXTERNAL_APP_ASSETS = "external"
  24. # The name of the utils file.
  25. UTILS = "utils"
  26. # The name of the state file.
  27. STATE_PATH = "/".join([UTILS, "state"])
  28. # The name of the components file.
  29. COMPONENTS_PATH = "/".join([UTILS, "components"])
  30. # The name of the contexts file.
  31. CONTEXTS_PATH = "/".join([UTILS, "context"])
  32. # The name of the output static directory.
  33. STATIC = "_static"
  34. # The name of the public html directory served at "/"
  35. PUBLIC = "public"
  36. # The directory where styles are located.
  37. STYLES = "styles"
  38. # The name of the pages directory.
  39. PAGES = "pages"
  40. # The name of the env json file.
  41. ENV_JSON = "env.json"
  42. # The name of the reflex json file.
  43. REFLEX_JSON = "reflex.json"
  44. # The name of the postcss config file.
  45. POSTCSS_JS = "postcss.config.js"
  46. # The name of the states directory.
  47. STATES = ".states"
  48. class Reflex(SimpleNamespace):
  49. """Base constants concerning Reflex."""
  50. # App names and versions.
  51. # The name of the Reflex package.
  52. MODULE_NAME = "reflex"
  53. # The current version of Reflex.
  54. VERSION = metadata.version(MODULE_NAME)
  55. # The reflex json file.
  56. JSON = "reflex.json"
  57. # Files and directories used to init a new project.
  58. # The directory to store reflex dependencies.
  59. # on windows, we use C:/Users/<username>/AppData/Local/reflex.
  60. # on macOS, we use ~/Library/Application Support/reflex.
  61. # on linux, we use ~/.local/share/reflex.
  62. # If user sets REFLEX_DIR envroment variable use that instead.
  63. DIR = PlatformDirs(MODULE_NAME, False).user_data_path
  64. LOGS_DIR = DIR / "logs"
  65. # The root directory of the reflex library.
  66. ROOT_DIR = Path(__file__).parents[2]
  67. RELEASES_URL = "https://api.github.com/repos/reflex-dev/templates/releases"
  68. class ReflexHostingCLI(SimpleNamespace):
  69. """Base constants concerning Reflex Hosting CLI."""
  70. # The name of the Reflex Hosting CLI package.
  71. MODULE_NAME = "reflex-hosting-cli"
  72. class Templates(SimpleNamespace):
  73. """Constants related to Templates."""
  74. # The route on Reflex backend to query which templates are available and their URLs.
  75. APP_TEMPLATES_ROUTE = "/app-templates"
  76. # The default template
  77. DEFAULT = "blank"
  78. # The AI template
  79. AI = "ai"
  80. # The option for the user to choose a remote template.
  81. CHOOSE_TEMPLATES = "choose-templates"
  82. # The URL to find reflex templates.
  83. REFLEX_TEMPLATES_URL = "https://reflex.dev/templates"
  84. # Demo url for the default template.
  85. DEFAULT_TEMPLATE_URL = "https://blank-template.reflex.run"
  86. # The reflex.build frontend host
  87. REFLEX_BUILD_FRONTEND = "https://flexgen.reflex.run"
  88. # The reflex.build backend host
  89. REFLEX_BUILD_BACKEND = "https://flexgen-prod-flexgen.fly.dev"
  90. @classproperty
  91. @classmethod
  92. def REFLEX_BUILD_URL(cls):
  93. """The URL to redirect to reflex.build.
  94. Returns:
  95. The URL to redirect to reflex.build.
  96. """
  97. from reflex.config import environment
  98. return (
  99. environment.REFLEX_BUILD_FRONTEND.get()
  100. + "/gen?reflex_init_token={reflex_init_token}"
  101. )
  102. @classproperty
  103. @classmethod
  104. def REFLEX_BUILD_POLL_URL(cls):
  105. """The URL to poll waiting for the user to select a generation.
  106. Returns:
  107. The URL to poll waiting for the user to select a generation.
  108. """
  109. from reflex.config import environment
  110. return environment.REFLEX_BUILD_BACKEND.get() + "/api/init/{reflex_init_token}"
  111. @classproperty
  112. @classmethod
  113. def REFLEX_BUILD_CODE_URL(cls):
  114. """The URL to fetch the generation's reflex code.
  115. Returns:
  116. The URL to fetch the generation's reflex code.
  117. """
  118. from reflex.config import environment
  119. return (
  120. environment.REFLEX_BUILD_BACKEND.get()
  121. + "/api/gen/{generation_hash}/refactored"
  122. )
  123. class Dirs(SimpleNamespace):
  124. """Folders used by the template system of Reflex."""
  125. # The template directory used during reflex init.
  126. BASE = Reflex.ROOT_DIR / Reflex.MODULE_NAME / ".templates"
  127. # The web subdirectory of the template directory.
  128. WEB_TEMPLATE = BASE / "web"
  129. # The jinja template directory.
  130. JINJA_TEMPLATE = BASE / "jinja"
  131. # Where the code for the templates is stored.
  132. CODE = "code"
  133. class Next(SimpleNamespace):
  134. """Constants related to NextJS."""
  135. # The NextJS config file
  136. CONFIG_FILE = "next.config.js"
  137. # The sitemap config file.
  138. SITEMAP_CONFIG_FILE = "next-sitemap.config.js"
  139. # The node modules directory.
  140. NODE_MODULES = "node_modules"
  141. # The package lock file.
  142. PACKAGE_LOCK = "package-lock.json"
  143. # Regex to check for message displayed when frontend comes up
  144. FRONTEND_LISTENING_REGEX = "Local:[\\s]+(.*)"
  145. # Color mode variables
  146. class ColorMode(SimpleNamespace):
  147. """Constants related to ColorMode."""
  148. NAME = "rawColorMode"
  149. RESOLVED_NAME = "resolvedColorMode"
  150. USE = "useColorMode"
  151. TOGGLE = "toggleColorMode"
  152. SET = "setColorMode"
  153. # Env modes
  154. class Env(str, Enum):
  155. """The environment modes."""
  156. DEV = "dev"
  157. PROD = "prod"
  158. # Log levels
  159. class LogLevel(str, Enum):
  160. """The log levels."""
  161. DEBUG = "debug"
  162. DEFAULT = "default"
  163. INFO = "info"
  164. WARNING = "warning"
  165. ERROR = "error"
  166. CRITICAL = "critical"
  167. def __le__(self, other: LogLevel) -> bool:
  168. """Compare log levels.
  169. Args:
  170. other: The other log level.
  171. Returns:
  172. True if the log level is less than or equal to the other log level.
  173. """
  174. levels = list(LogLevel)
  175. return levels.index(self) <= levels.index(other)
  176. def subprocess_level(self):
  177. """Return the log level for the subprocess.
  178. Returns:
  179. The log level for the subprocess
  180. """
  181. return self if self != LogLevel.DEFAULT else LogLevel.WARNING
  182. # Server socket configuration variables
  183. POLLING_MAX_HTTP_BUFFER_SIZE = 1000 * 1000
  184. class Ping(SimpleNamespace):
  185. """PING constants."""
  186. # The 'ping' interval
  187. INTERVAL = 25
  188. # The 'ping' timeout
  189. TIMEOUT = 120
  190. # Keys in the client_side_storage dict
  191. COOKIES = "cookies"
  192. LOCAL_STORAGE = "local_storage"
  193. SESSION_STORAGE = "session_storage"
  194. # Testing variables.
  195. # Testing os env set by pytest when running a test case.
  196. PYTEST_CURRENT_TEST = "PYTEST_CURRENT_TEST"
  197. APP_HARNESS_FLAG = "APP_HARNESS_FLAG"
  198. REFLEX_VAR_OPENING_TAG = "<reflex.Var>"
  199. REFLEX_VAR_CLOSING_TAG = "</reflex.Var>"