Skip to content

Commit

Permalink
allow setting run mode via env, add helpers to determine it (#4168)
Browse files Browse the repository at this point in the history
  • Loading branch information
benedikt-bartscher authored Oct 21, 2024
1 parent fcc97b0 commit 7560bf6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
2 changes: 2 additions & 0 deletions reflex/constants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions reflex/constants/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ class Ping(SimpleNamespace):
# 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"
Expand Down
18 changes: 16 additions & 2 deletions reflex/reflex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."
),
Expand All @@ -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)


Expand Down
18 changes: 18 additions & 0 deletions reflex/utils/exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 7560bf6

Please sign in to comment.