allow setting run mode via env, add helpers to determine it (#4168)

This commit is contained in:
benedikt-bartscher 2024-10-21 21:26:09 +02:00 committed by GitHub
parent fcc97b0402
commit 7560bf6429
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 39 additions and 2 deletions

View File

@ -2,6 +2,8 @@
from .base import (
COOKIES,
ENV_BACKEND_ONLY_ENV_VAR,
ENV_FRONTEND_ONLY_ENV_VAR,
ENV_MODE_ENV_VAR,
IS_WINDOWS,
LOCAL_STORAGE,

View File

@ -226,6 +226,9 @@ SKIP_COMPILE_ENV_VAR = "__REFLEX_SKIP_COMPILE"
# This env var stores the execution mode of the app
ENV_MODE_ENV_VAR = "REFLEX_ENV_MODE"
ENV_BACKEND_ONLY_ENV_VAR = "REFLEX_BACKEND_ONLY"
ENV_FRONTEND_ONLY_ENV_VAR = "REFLEX_FRONTEND_ONLY"
# Testing variables.
# Testing os env set by pytest when running a test case.
PYTEST_CURRENT_TEST = "PYTEST_CURRENT_TEST"

View File

@ -274,9 +274,17 @@ def run(
constants.Env.DEV, help="The environment to run the app in."
),
frontend: bool = typer.Option(
False, "--frontend-only", help="Execute only frontend."
False,
"--frontend-only",
help="Execute only frontend.",
envvar=constants.ENV_FRONTEND_ONLY_ENV_VAR,
),
backend: bool = typer.Option(
False,
"--backend-only",
help="Execute only backend.",
envvar=constants.ENV_BACKEND_ONLY_ENV_VAR,
),
backend: bool = typer.Option(False, "--backend-only", help="Execute only backend."),
frontend_port: str = typer.Option(
config.frontend_port, help="Specify a different frontend port."
),
@ -291,6 +299,12 @@ def run(
),
):
"""Run the app in the current directory."""
if frontend and backend:
console.error("Cannot use both --frontend-only and --backend-only options.")
raise typer.Exit(1)
os.environ[constants.ENV_BACKEND_ONLY_ENV_VAR] = str(backend).lower()
os.environ[constants.ENV_FRONTEND_ONLY_ENV_VAR] = str(frontend).lower()
_run(env, frontend, backend, frontend_port, backend_port, backend_host, loglevel)

View File

@ -496,6 +496,24 @@ def is_prod_mode() -> bool:
return current_mode == constants.Env.PROD.value
def is_frontend_only() -> bool:
"""Check if the app is running in frontend-only mode.
Returns:
True if the app is running in frontend-only mode.
"""
return os.environ.get(constants.ENV_FRONTEND_ONLY_ENV_VAR, "").lower() == "true"
def is_backend_only() -> bool:
"""Check if the app is running in backend-only mode.
Returns:
True if the app is running in backend-only mode.
"""
return os.environ.get(constants.ENV_BACKEND_ONLY_ENV_VAR, "").lower() == "true"
def should_skip_compile() -> bool:
"""Whether the app should skip compile.