constants.py 4.6 KB

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