diff --git a/reflex/constants/__init__.py b/reflex/constants/__init__.py index 8e61a3717..a540805b5 100644 --- a/reflex/constants/__init__.py +++ b/reflex/constants/__init__.py @@ -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, diff --git a/reflex/constants/base.py b/reflex/constants/base.py index b86f083cc..070ff7724 100644 --- a/reflex/constants/base.py +++ b/reflex/constants/base.py @@ -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" diff --git a/reflex/reflex.py b/reflex/reflex.py index bd6904d06..99850a9d2 100644 --- a/reflex/reflex.py +++ b/reflex/reflex.py @@ -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) diff --git a/reflex/utils/exec.py b/reflex/utils/exec.py index acb69ee19..6fa67a96f 100644 --- a/reflex/utils/exec.py +++ b/reflex/utils/exec.py @@ -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.