Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions pynecone/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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."""

Expand Down
4 changes: 3 additions & 1 deletion pynecone/pc.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,15 @@ def run(
env: constants.Env = constants.Env.DEV,
frontend: bool = True,
backend: bool = True,
loglevel: constants.LogLevel = constants.LogLevel.ERROR,
):
"""Run the app.

Args:
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.
Expand Down Expand Up @@ -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()
Expand Down
21 changes: 15 additions & 6 deletions pynecone/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import string
import subprocess
import sys
import uvicorn
from collections import defaultdict
from subprocess import PIPE
from types import ModuleType
Expand Down Expand Up @@ -532,30 +533,38 @@ 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 + [
"--workers",
str(num_workers),
"--threads",
str(num_workers),
"--log-level",
str(loglevel),
f"{app_name}:{constants.APP_VAR}()",
]
subprocess.run(command)
Expand Down