constants.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. # Files and directories used to init a new project.
  13. # The root directory of the pynecone library.
  14. ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  15. # The name of the file used for pc init.
  16. APP_TEMPLATE_FILE = "tutorial.py"
  17. # The name of the assets directory.
  18. APP_ASSETS_DIR = "assets"
  19. # The template directory used during pc init.
  20. TEMPLATE_DIR = os.path.join(ROOT_DIR, MODULE_NAME, ".templates")
  21. # The web subdirectory of the template directory.
  22. WEB_TEMPLATE_DIR = os.path.join(TEMPLATE_DIR, "web")
  23. # The app subdirectory of the template directory.
  24. APP_TEMPLATE_DIR = os.path.join(TEMPLATE_DIR, "app")
  25. # The assets subdirectory of the template directory.
  26. ASSETS_TEMPLATE_DIR = os.path.join(TEMPLATE_DIR, APP_ASSETS_DIR)
  27. # The frontend directories in a project.
  28. # The web folder where the NextJS app is compiled to.
  29. WEB_DIR = ".web"
  30. # The name of the utils file.
  31. UTILS_DIR = "utils"
  32. # The name of the state file.
  33. STATE_PATH = os.path.join(UTILS_DIR, "state")
  34. # The directory where the app pages are compiled to.
  35. WEB_PAGES_DIR = os.path.join(WEB_DIR, "pages")
  36. # The directory where the static build is located.
  37. WEB_STATIC_DIR = os.path.join(WEB_DIR, "_static")
  38. # The directory where the utils file is located.
  39. WEB_UTILS_DIR = os.path.join(WEB_DIR, UTILS_DIR)
  40. # The directory where the assets are located.
  41. WEB_ASSETS_DIR = os.path.join(WEB_DIR, "public")
  42. # The node modules directory.
  43. NODE_MODULES = "node_modules"
  44. # The package lock file.
  45. PACKAGE_LOCK = "package-lock.json"
  46. # Commands to run the app.
  47. # The backend api url.
  48. API_URL = "http://localhost:8000"
  49. # The default path where bun is installed.
  50. BUN_PATH = "$HOME/.bun/bin/bun"
  51. # Command to install bun.
  52. INSTALL_BUN = "curl https://bun.sh/install | bash"
  53. # Command to run the backend in dev mode.
  54. RUN_BACKEND = "uvicorn --log-level critical --reload --host 0.0.0.0".split()
  55. # The default timeout when launching the gunicorn server.
  56. TIMEOUT = 120
  57. # The command to run the backend in production mode.
  58. RUN_BACKEND_PROD = f"gunicorn --worker-class uvicorn.workers.UvicornH11Worker --bind 0.0.0.0:8000 --preload --timeout {TIMEOUT} --log-level debug".split()
  59. # Compiler variables.
  60. # The extension for compiled Javascript files.
  61. JS_EXT = ".js"
  62. # The extension for python files.
  63. PY_EXT = ".py"
  64. # The expected variable name where the pc.App is stored.
  65. APP_VAR = "app"
  66. # The expected variable name where the API object is stored for deployment.
  67. API_VAR = "api"
  68. # The name of the router variable.
  69. ROUTER = "router"
  70. # The name of the variable to hold API results.
  71. RESULT = "result"
  72. # The name of the process variable.
  73. PROCESSING = "processing"
  74. # The name of the state variable.
  75. STATE = "state"
  76. # The name of the events variable.
  77. EVENTS = "events"
  78. # The name of the initial hydrate event.
  79. HYDRATE = "hydrate"
  80. # The name of the index page.
  81. INDEX_ROUTE = "index"
  82. # The name of the document root page.
  83. DOCUMENT_ROOT = "_document"
  84. # The name of the theme page.
  85. THEME = "theme"
  86. # The prefix used to create setters for state vars.
  87. SETTER_PREFIX = "set_"
  88. # The name of the frontend zip during deployment.
  89. FRONTEND_ZIP = "frontend.zip"
  90. # The name of the backend zip during deployment.
  91. BACKEND_ZIP = "backend.zip"
  92. # The name of the sqlite database.
  93. DB_NAME = "pynecone.db"
  94. # The sqlite url.
  95. DB_URL = f"sqlite:///{DB_NAME}"
  96. # The default title to show for Pynecone apps.
  97. DEFAULT_TITLE = "Pynecone App"
  98. # The default description to show for Pynecone apps.
  99. DEFAULT_DESCRIPTION = "A Pynecone app."
  100. # The default image to show for Pynecone apps.
  101. DEFAULT_IMAGE = "favicon.ico"
  102. # The name of the pynecone config module.
  103. CONFIG_MODULE = "pcconfig"
  104. # The python config file.
  105. CONFIG_FILE = f"{CONFIG_MODULE}{PY_EXT}"
  106. # The deployment URL.
  107. PRODUCTION_BACKEND_URL = "https://{username}-{app_name}.api.pynecone.app"
  108. # Token expiration time in seconds.
  109. TOKEN_EXPIRATION = 60 * 60
  110. # Env modes
  111. class Env(str, Enum):
  112. """The environment modes."""
  113. DEV = "dev"
  114. PROD = "prod"
  115. class Endpoint(Enum):
  116. """Endpoints for the pynecone backend API."""
  117. PING = "ping"
  118. EVENT = "event"
  119. def __str__(self) -> str:
  120. """Get the string representation of the endpoint.
  121. Returns:
  122. The path for the endpoint.
  123. """
  124. return f"/{self.value}"
  125. def get_url(self) -> str:
  126. """Get the URL for the endpoint.
  127. Returns:
  128. The full URL for the endpoint.
  129. """
  130. from pynecone import utils
  131. config = utils.get_config()
  132. return "".join([config.api_url, str(self)])