diff --git a/pynecone/constants.py b/pynecone/constants.py index d5326725f2..b8e723937b 100644 --- a/pynecone/constants.py +++ b/pynecone/constants.py @@ -63,8 +63,8 @@ BUN_PATH = "$HOME/.bun/bin/bun" # Command to install bun. INSTALL_BUN = "curl https://bun.sh/install | bash" -# Command to run the backend in dev mode. -RUN_BACKEND = "uvicorn --log-level debug --reload --host 0.0.0.0".split() +# Default host in dev mode. +BACKEND_HOST = "0.0.0.0" # The default timeout when launching the gunicorn server. TIMEOUT = 120 # The command to run the backend in production mode. @@ -135,6 +135,17 @@ class Env(str, Enum): PROD = "prod" +# Log levels +class LogLevel(str, Enum): + """The log levels.""" + + DEBUG = "debug" + INFO = "info" + WARNING = "warning" + ERROR = "error" + CRITICAL = "critical" + + class Endpoint(Enum): """Endpoints for the pynecone backend API.""" diff --git a/pynecone/pc.py b/pynecone/pc.py index 2623357a82..b14c568f26 100644 --- a/pynecone/pc.py +++ b/pynecone/pc.py @@ -52,6 +52,7 @@ def run( env: constants.Env = constants.Env.DEV, frontend: bool = True, backend: bool = True, + loglevel: constants.LogLevel = constants.LogLevel.ERROR, ): """Run the app. @@ -59,6 +60,7 @@ def run( env: The environment to run the app in. frontend: Whether to run the frontend. backend: Whether to run the backend. + loglevel: The log level to use. Raises: Exit: If the app is not initialized. @@ -93,7 +95,7 @@ def run( if frontend: frontend_cmd(app.app) if backend: - backend_cmd(app.__name__) + backend_cmd(app.__name__, loglevel=loglevel) @cli.command() diff --git a/pynecone/utils.py b/pynecone/utils.py index 6e83aefbf0..433ede80b9 100644 --- a/pynecone/utils.py +++ b/pynecone/utils.py @@ -13,6 +13,7 @@ import string import subprocess import sys +import uvicorn from collections import defaultdict from subprocess import PIPE from types import ModuleType @@ -532,23 +533,29 @@ def get_num_workers() -> int: return (os.cpu_count() or 1) * 2 + 1 -def run_backend(app_name: str): +def run_backend(app_name: str, loglevel: constants.LogLevel = constants.LogLevel.ERROR): """Run the backend. Args: app_name: The app name. + loglevel: The log level. """ - command = constants.RUN_BACKEND + [ - f"{app_name}:{constants.APP_VAR}.{constants.API_VAR}" - ] - subprocess.run(command) + uvicorn.run( + f"{app_name}:{constants.APP_VAR}.{constants.API_VAR}", + host=constants.BACKEND_HOST, + log_level=loglevel, + reload=True, + ) -def run_backend_prod(app_name: str): +def run_backend_prod( + app_name: str, loglevel: constants.LogLevel = constants.LogLevel.ERROR +): """Run the backend. Args: app_name: The app name. + loglevel: The log level. """ num_workers = get_num_workers() command = constants.RUN_BACKEND_PROD + [ @@ -556,6 +563,8 @@ def run_backend_prod(app_name: str): str(num_workers), "--threads", str(num_workers), + "--log-level", + str(loglevel), f"{app_name}:{constants.APP_VAR}()", ] subprocess.run(command)