constants.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. """Constants used throughout the package."""
  2. import os
  3. from enum import Enum
  4. import pkg_resources
  5. # App names and versions.
  6. # The name of the Pynecone module.
  7. MODULE_NAME = "pynecone"
  8. # The name of the pip install package.
  9. PACKAGE_NAME = "pynecone-io"
  10. # The current version of Pynecone.
  11. VERSION = pkg_resources.get_distribution(PACKAGE_NAME).version
  12. # Minimum version of Node.js required to run Pynecone.
  13. MIN_NODE_VERSION = "12.22.0"
  14. # Files and directories used to init a new project.
  15. # The root directory of the pynecone library.
  16. ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  17. # The name of the file used for pc init.
  18. APP_TEMPLATE_FILE = "tutorial.py"
  19. # The name of the assets directory.
  20. APP_ASSETS_DIR = "assets"
  21. # The template directory used during pc init.
  22. TEMPLATE_DIR = os.path.join(ROOT_DIR, MODULE_NAME, ".templates")
  23. # The web subdirectory of the template directory.
  24. WEB_TEMPLATE_DIR = os.path.join(TEMPLATE_DIR, "web")
  25. # The app subdirectory of the template directory.
  26. APP_TEMPLATE_DIR = os.path.join(TEMPLATE_DIR, "app")
  27. # The assets subdirectory of the template directory.
  28. ASSETS_TEMPLATE_DIR = os.path.join(TEMPLATE_DIR, APP_ASSETS_DIR)
  29. # The frontend directories in a project.
  30. # The web folder where the NextJS app is compiled to.
  31. WEB_DIR = ".web"
  32. # The name of the utils file.
  33. UTILS_DIR = "utils"
  34. # The name of the state file.
  35. STATE_PATH = "/".join([UTILS_DIR, "state"])
  36. # The name of the components file.
  37. COMPONENTS_PATH = "/".join([UTILS_DIR, "components"])
  38. # The directory where the app pages are compiled to.
  39. WEB_PAGES_DIR = os.path.join(WEB_DIR, "pages")
  40. # The directory where the static build is located.
  41. WEB_STATIC_DIR = os.path.join(WEB_DIR, "_static")
  42. # The directory where the utils file is located.
  43. WEB_UTILS_DIR = os.path.join(WEB_DIR, UTILS_DIR)
  44. # The directory where the assets are located.
  45. WEB_ASSETS_DIR = os.path.join(WEB_DIR, "public")
  46. # The node modules directory.
  47. NODE_MODULES = "node_modules"
  48. # The package lock file.
  49. PACKAGE_LOCK = "package-lock.json"
  50. # The pcversion template file.
  51. PCVERSION_TEMPLATE_FILE = os.path.join(WEB_TEMPLATE_DIR, "pcversion.txt")
  52. # The pcversion app file.
  53. PCVERSION_APP_FILE = os.path.join(WEB_DIR, "pcversion.txt")
  54. # Commands to run the app.
  55. # The frontend default port.
  56. FRONTEND_PORT = "3000"
  57. # The backend api url.
  58. API_URL = "http://localhost:8000"
  59. # The default path where bun is installed.
  60. BUN_PATH = "$HOME/.bun/bin/bun"
  61. # Command to install bun.
  62. INSTALL_BUN = "curl https://bun.sh/install | bash"
  63. # Default host in dev mode.
  64. BACKEND_HOST = "0.0.0.0"
  65. # The default timeout when launching the gunicorn server.
  66. TIMEOUT = 120
  67. # The command to run the backend in production mode.
  68. RUN_BACKEND_PROD = f"gunicorn --worker-class uvicorn.workers.UvicornH11Worker --preload --timeout {TIMEOUT} --log-level critical".split()
  69. # Compiler variables.
  70. # The extension for compiled Javascript files.
  71. JS_EXT = ".js"
  72. # The extension for python files.
  73. PY_EXT = ".py"
  74. # The expected variable name where the pc.App is stored.
  75. APP_VAR = "app"
  76. # The expected variable name where the API object is stored for deployment.
  77. API_VAR = "api"
  78. # The name of the router variable.
  79. ROUTER = "router"
  80. # The name of the socket variable.
  81. SOCKET = "socket"
  82. # The name of the variable to hold API results.
  83. RESULT = "result"
  84. # The name of the process variable.
  85. PROCESSING = "processing"
  86. # The name of the state variable.
  87. STATE = "state"
  88. # The name of the events variable.
  89. EVENTS = "events"
  90. # The name of the initial hydrate event.
  91. HYDRATE = "hydrate"
  92. # The name of the index page.
  93. INDEX_ROUTE = "index"
  94. # The name of the document root page.
  95. DOCUMENT_ROOT = "_document"
  96. # The name of the theme page.
  97. THEME = "theme"
  98. # The prefix used to create setters for state vars.
  99. SETTER_PREFIX = "set_"
  100. # The name of the frontend zip during deployment.
  101. FRONTEND_ZIP = "frontend.zip"
  102. # The name of the backend zip during deployment.
  103. BACKEND_ZIP = "backend.zip"
  104. # The name of the sqlite database.
  105. DB_NAME = "pynecone.db"
  106. # The sqlite url.
  107. DB_URL = f"sqlite:///{DB_NAME}"
  108. # The default title to show for Pynecone apps.
  109. DEFAULT_TITLE = "Pynecone App"
  110. # The default description to show for Pynecone apps.
  111. DEFAULT_DESCRIPTION = "A Pynecone app."
  112. # The default image to show for Pynecone apps.
  113. DEFAULT_IMAGE = "favicon.ico"
  114. # The name of the pynecone config module.
  115. CONFIG_MODULE = "pcconfig"
  116. # The python config file.
  117. CONFIG_FILE = f"{CONFIG_MODULE}{PY_EXT}"
  118. # The deployment URL.
  119. PRODUCTION_BACKEND_URL = "https://{username}-{app_name}.api.pynecone.app"
  120. # Token expiration time in seconds.
  121. TOKEN_EXPIRATION = 60 * 60
  122. # Env modes
  123. class Env(str, Enum):
  124. """The environment modes."""
  125. DEV = "dev"
  126. PROD = "prod"
  127. # Log levels
  128. class LogLevel(str, Enum):
  129. """The log levels."""
  130. DEBUG = "debug"
  131. INFO = "info"
  132. WARNING = "warning"
  133. ERROR = "error"
  134. CRITICAL = "critical"
  135. class Endpoint(Enum):
  136. """Endpoints for the pynecone backend API."""
  137. PING = "ping"
  138. EVENT = "event"
  139. def __str__(self) -> str:
  140. """Get the string representation of the endpoint.
  141. Returns:
  142. The path for the endpoint.
  143. """
  144. return f"/{self.value}"
  145. def get_url(self) -> str:
  146. """Get the URL for the endpoint.
  147. Returns:
  148. The full URL for the endpoint.
  149. """
  150. # Import here to avoid circular imports.
  151. from pynecone import utils
  152. # Get the API URL from the config.
  153. config = utils.get_config()
  154. url = "".join([config.api_url, str(self)])
  155. # The event endpoint is a websocket.
  156. if self == Endpoint.EVENT:
  157. # Replace the protocol with ws.
  158. url = url.replace("https://", "wss://").replace("http://", "ws://")
  159. # Return the url.
  160. return url