base.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. # Where compilation artifacts for the backend are stored.
  49. BACKEND = "backend"
  50. # JSON-encoded list of page routes that need to be evaluated on the backend.
  51. STATEFUL_PAGES = "stateful_pages.json"
  52. # Marker file indicating that upload component was used in the frontend.
  53. UPLOAD_IS_USED = "upload_is_used"
  54. class Reflex(SimpleNamespace):
  55. """Base constants concerning Reflex."""
  56. # App names and versions.
  57. # The name of the Reflex package.
  58. MODULE_NAME = "reflex"
  59. # The current version of Reflex.
  60. VERSION = metadata.version(MODULE_NAME)
  61. # The reflex json file.
  62. JSON = "reflex.json"
  63. # Files and directories used to init a new project.
  64. # The directory to store reflex dependencies.
  65. # on windows, we use C:/Users/<username>/AppData/Local/reflex.
  66. # on macOS, we use ~/Library/Application Support/reflex.
  67. # on linux, we use ~/.local/share/reflex.
  68. # If user sets REFLEX_DIR envroment variable use that instead.
  69. DIR = PlatformDirs(MODULE_NAME, False).user_data_path
  70. LOGS_DIR = DIR / "logs"
  71. # The root directory of the reflex library.
  72. ROOT_DIR = Path(__file__).parents[2]
  73. RELEASES_URL = "https://api.github.com/repos/reflex-dev/templates/releases"
  74. class ReflexHostingCLI(SimpleNamespace):
  75. """Base constants concerning Reflex Hosting CLI."""
  76. # The name of the Reflex Hosting CLI package.
  77. MODULE_NAME = "reflex-hosting-cli"
  78. class Templates(SimpleNamespace):
  79. """Constants related to Templates."""
  80. # The route on Reflex backend to query which templates are available and their URLs.
  81. APP_TEMPLATES_ROUTE = "/app-templates"
  82. # The default template
  83. DEFAULT = "blank"
  84. # The AI template
  85. AI = "ai"
  86. # The option for the user to choose a remote template.
  87. CHOOSE_TEMPLATES = "choose-templates"
  88. # The URL to find reflex templates.
  89. REFLEX_TEMPLATES_URL = "https://reflex.dev/templates"
  90. # Demo url for the default template.
  91. DEFAULT_TEMPLATE_URL = "https://blank-template.reflex.run"
  92. # The reflex.build frontend host
  93. REFLEX_BUILD_FRONTEND = "https://flexgen.reflex.run"
  94. # The reflex.build backend host
  95. REFLEX_BUILD_BACKEND = "https://flexgen-prod-flexgen.fly.dev"
  96. @classproperty
  97. @classmethod
  98. def REFLEX_BUILD_URL(cls):
  99. """The URL to redirect to reflex.build.
  100. Returns:
  101. The URL to redirect to reflex.build.
  102. """
  103. from reflex.config import environment
  104. return (
  105. environment.REFLEX_BUILD_FRONTEND.get()
  106. + "/gen?reflex_init_token={reflex_init_token}"
  107. )
  108. @classproperty
  109. @classmethod
  110. def REFLEX_BUILD_POLL_URL(cls):
  111. """The URL to poll waiting for the user to select a generation.
  112. Returns:
  113. The URL to poll waiting for the user to select a generation.
  114. """
  115. from reflex.config import environment
  116. return environment.REFLEX_BUILD_BACKEND.get() + "/api/init/{reflex_init_token}"
  117. @classproperty
  118. @classmethod
  119. def REFLEX_BUILD_CODE_URL(cls):
  120. """The URL to fetch the generation's reflex code.
  121. Returns:
  122. The URL to fetch the generation's reflex code.
  123. """
  124. from reflex.config import environment
  125. return (
  126. environment.REFLEX_BUILD_BACKEND.get()
  127. + "/api/gen/{generation_hash}/refactored"
  128. )
  129. class Dirs(SimpleNamespace):
  130. """Folders used by the template system of Reflex."""
  131. # The template directory used during reflex init.
  132. BASE = Reflex.ROOT_DIR / Reflex.MODULE_NAME / ".templates"
  133. # The web subdirectory of the template directory.
  134. WEB_TEMPLATE = BASE / "web"
  135. # The jinja template directory.
  136. JINJA_TEMPLATE = BASE / "jinja"
  137. # Where the code for the templates is stored.
  138. CODE = "code"
  139. class Next(SimpleNamespace):
  140. """Constants related to NextJS."""
  141. # The NextJS config file
  142. CONFIG_FILE = "next.config.js"
  143. # The sitemap config file.
  144. SITEMAP_CONFIG_FILE = "next-sitemap.config.js"
  145. # The node modules directory.
  146. NODE_MODULES = "node_modules"
  147. # The package lock file.
  148. PACKAGE_LOCK = "package-lock.json"
  149. # Regex to check for message displayed when frontend comes up
  150. FRONTEND_LISTENING_REGEX = "Local:[\\s]+(.*)"
  151. # Color mode variables
  152. class ColorMode(SimpleNamespace):
  153. """Constants related to ColorMode."""
  154. NAME = "rawColorMode"
  155. RESOLVED_NAME = "resolvedColorMode"
  156. USE = "useColorMode"
  157. TOGGLE = "toggleColorMode"
  158. SET = "setColorMode"
  159. # Env modes
  160. class Env(str, Enum):
  161. """The environment modes."""
  162. DEV = "dev"
  163. PROD = "prod"
  164. # Log levels
  165. class LogLevel(str, Enum):
  166. """The log levels."""
  167. DEBUG = "debug"
  168. DEFAULT = "default"
  169. INFO = "info"
  170. WARNING = "warning"
  171. ERROR = "error"
  172. CRITICAL = "critical"
  173. def __le__(self, other: LogLevel) -> bool:
  174. """Compare log levels.
  175. Args:
  176. other: The other log level.
  177. Returns:
  178. True if the log level is less than or equal to the other log level.
  179. """
  180. levels = list(LogLevel)
  181. return levels.index(self) <= levels.index(other)
  182. def subprocess_level(self):
  183. """Return the log level for the subprocess.
  184. Returns:
  185. The log level for the subprocess
  186. """
  187. return self if self != LogLevel.DEFAULT else LogLevel.WARNING
  188. # Server socket configuration variables
  189. POLLING_MAX_HTTP_BUFFER_SIZE = 1000 * 1000
  190. class Ping(SimpleNamespace):
  191. """PING constants."""
  192. # The 'ping' interval
  193. INTERVAL = 25
  194. # The 'ping' timeout
  195. TIMEOUT = 120
  196. # Keys in the client_side_storage dict
  197. COOKIES = "cookies"
  198. LOCAL_STORAGE = "local_storage"
  199. SESSION_STORAGE = "session_storage"
  200. # Testing variables.
  201. # Testing os env set by pytest when running a test case.
  202. PYTEST_CURRENT_TEST = "PYTEST_CURRENT_TEST"
  203. APP_HARNESS_FLAG = "APP_HARNESS_FLAG"
  204. REFLEX_VAR_OPENING_TAG = "<reflex.Var>"
  205. REFLEX_VAR_CLOSING_TAG = "</reflex.Var>"