base.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. # The reflex stylesheet language supported
  75. STYLESHEETS_SUPPORTED = ["css", "sass", "scss"]
  76. class ReflexHostingCLI(SimpleNamespace):
  77. """Base constants concerning Reflex Hosting CLI."""
  78. # The name of the Reflex Hosting CLI package.
  79. MODULE_NAME = "reflex-hosting-cli"
  80. class Templates(SimpleNamespace):
  81. """Constants related to Templates."""
  82. # The route on Reflex backend to query which templates are available and their URLs.
  83. APP_TEMPLATES_ROUTE = "/app-templates"
  84. # The default template
  85. DEFAULT = "blank"
  86. # The AI template
  87. AI = "ai"
  88. # The option for the user to choose a remote template.
  89. CHOOSE_TEMPLATES = "choose-templates"
  90. # The URL to find reflex templates.
  91. REFLEX_TEMPLATES_URL = "https://reflex.dev/templates"
  92. # Demo url for the default template.
  93. DEFAULT_TEMPLATE_URL = "https://blank-template.reflex.run"
  94. # The reflex.build frontend host
  95. REFLEX_BUILD_FRONTEND = "https://flexgen.reflex.run"
  96. # The reflex.build backend host
  97. REFLEX_BUILD_BACKEND = "https://flexgen-prod-flexgen.fly.dev"
  98. @classproperty
  99. @classmethod
  100. def REFLEX_BUILD_URL(cls):
  101. """The URL to redirect to reflex.build.
  102. Returns:
  103. The URL to redirect to reflex.build.
  104. """
  105. from reflex.config import environment
  106. return (
  107. environment.REFLEX_BUILD_FRONTEND.get()
  108. + "/gen?reflex_init_token={reflex_init_token}"
  109. )
  110. @classproperty
  111. @classmethod
  112. def REFLEX_BUILD_POLL_URL(cls):
  113. """The URL to poll waiting for the user to select a generation.
  114. Returns:
  115. The URL to poll waiting for the user to select a generation.
  116. """
  117. from reflex.config import environment
  118. return environment.REFLEX_BUILD_BACKEND.get() + "/api/init/{reflex_init_token}"
  119. @classproperty
  120. @classmethod
  121. def REFLEX_BUILD_CODE_URL(cls):
  122. """The URL to fetch the generation's reflex code.
  123. Returns:
  124. The URL to fetch the generation's reflex code.
  125. """
  126. from reflex.config import environment
  127. return (
  128. environment.REFLEX_BUILD_BACKEND.get()
  129. + "/api/gen/{generation_hash}/refactored"
  130. )
  131. class Dirs(SimpleNamespace):
  132. """Folders used by the template system of Reflex."""
  133. # The template directory used during reflex init.
  134. BASE = Reflex.ROOT_DIR / Reflex.MODULE_NAME / ".templates"
  135. # The web subdirectory of the template directory.
  136. WEB_TEMPLATE = BASE / "web"
  137. # The jinja template directory.
  138. JINJA_TEMPLATE = BASE / "jinja"
  139. # Where the code for the templates is stored.
  140. CODE = "code"
  141. class Next(SimpleNamespace):
  142. """Constants related to NextJS."""
  143. # The NextJS config file
  144. CONFIG_FILE = "next.config.js"
  145. # The sitemap config file.
  146. SITEMAP_CONFIG_FILE = "next-sitemap.config.js"
  147. # The node modules directory.
  148. NODE_MODULES = "node_modules"
  149. # The package lock file.
  150. PACKAGE_LOCK = "package-lock.json"
  151. # Regex to check for message displayed when frontend comes up
  152. FRONTEND_LISTENING_REGEX = "Local:[\\s]+(.*)"
  153. # Color mode variables
  154. class ColorMode(SimpleNamespace):
  155. """Constants related to ColorMode."""
  156. NAME = "rawColorMode"
  157. RESOLVED_NAME = "resolvedColorMode"
  158. USE = "useColorMode"
  159. TOGGLE = "toggleColorMode"
  160. SET = "setColorMode"
  161. # Env modes
  162. class Env(str, Enum):
  163. """The environment modes."""
  164. DEV = "dev"
  165. PROD = "prod"
  166. # Log levels
  167. class LogLevel(str, Enum):
  168. """The log levels."""
  169. DEBUG = "debug"
  170. DEFAULT = "default"
  171. INFO = "info"
  172. WARNING = "warning"
  173. ERROR = "error"
  174. CRITICAL = "critical"
  175. def __le__(self, other: LogLevel) -> bool:
  176. """Compare log levels.
  177. Args:
  178. other: The other log level.
  179. Returns:
  180. True if the log level is less than or equal to the other log level.
  181. """
  182. levels = list(LogLevel)
  183. return levels.index(self) <= levels.index(other)
  184. def subprocess_level(self):
  185. """Return the log level for the subprocess.
  186. Returns:
  187. The log level for the subprocess
  188. """
  189. return self if self != LogLevel.DEFAULT else LogLevel.WARNING
  190. # Server socket configuration variables
  191. POLLING_MAX_HTTP_BUFFER_SIZE = 1000 * 1000
  192. class Ping(SimpleNamespace):
  193. """PING constants."""
  194. # The 'ping' interval
  195. INTERVAL = 25
  196. # The 'ping' timeout
  197. TIMEOUT = 120
  198. # Keys in the client_side_storage dict
  199. COOKIES = "cookies"
  200. LOCAL_STORAGE = "local_storage"
  201. SESSION_STORAGE = "session_storage"
  202. # Testing variables.
  203. # Testing os env set by pytest when running a test case.
  204. PYTEST_CURRENT_TEST = "PYTEST_CURRENT_TEST"
  205. APP_HARNESS_FLAG = "APP_HARNESS_FLAG"
  206. REFLEX_VAR_OPENING_TAG = "<reflex.Var>"
  207. REFLEX_VAR_CLOSING_TAG = "</reflex.Var>"