From 2ffe4e0d78127b148312e9f145aedf9bbe6109c0 Mon Sep 17 00:00:00 2001 From: Masen Furer Date: Fri, 1 Mar 2024 11:06:29 -0800 Subject: [PATCH] Expose reflex.utils.exec.is_prod_mode Formalize runtime checking of the app's `--env` parameter. --- reflex/compiler/compiler.py | 3 ++- reflex/constants/__init__.py | 1 + reflex/constants/base.py | 3 +++ reflex/reflex.py | 2 +- reflex/utils/exec.py | 13 +++++++++++++ 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/reflex/compiler/compiler.py b/reflex/compiler/compiler.py index 6256b7ab1..2c3886cc1 100644 --- a/reflex/compiler/compiler.py +++ b/reflex/compiler/compiler.py @@ -18,6 +18,7 @@ from reflex.components.component import ( from reflex.config import get_config from reflex.state import BaseState from reflex.style import LIGHT_COLOR_MODE +from reflex.utils.exec import is_prod_mode from reflex.utils.imports import ImportVar @@ -66,7 +67,7 @@ def _compile_theme(theme: dict) -> str: def _is_dev_mode() -> bool: - return os.environ.get("REFLEX_ENV_MODE", "dev") == "dev" + return not is_prod_mode() def _compile_contexts(state: Optional[Type[BaseState]], theme: Component) -> str: diff --git a/reflex/constants/__init__.py b/reflex/constants/__init__.py index 74baf3043..1f3325a8a 100644 --- a/reflex/constants/__init__.py +++ b/reflex/constants/__init__.py @@ -2,6 +2,7 @@ from .base import ( COOKIES, + ENV_MODE_ENV_VAR, IS_WINDOWS, LOCAL_STORAGE, POLLING_MAX_HTTP_BUFFER_SIZE, diff --git a/reflex/constants/base.py b/reflex/constants/base.py index 272756ec0..11d73a7c1 100644 --- a/reflex/constants/base.py +++ b/reflex/constants/base.py @@ -183,6 +183,9 @@ LOCAL_STORAGE = "local_storage" # If this env var is set to "yes", App.compile will be a no-op SKIP_COMPILE_ENV_VAR = "__REFLEX_SKIP_COMPILE" +# This env var stores the execution mode of the app +ENV_MODE_ENV_VAR = "REFLEX_ENV_MODE" + # 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 f45bd869e..e130a17df 100644 --- a/reflex/reflex.py +++ b/reflex/reflex.py @@ -151,7 +151,7 @@ def _run( console.set_log_level(loglevel) # Set env mode in the environment - os.environ["REFLEX_ENV_MODE"] = env.value + os.environ[constants.ENV_MODE_ENV_VAR] = env.value # Show system info exec.output_system_info() diff --git a/reflex/utils/exec.py b/reflex/utils/exec.py index e3c7eb586..39326ff7f 100644 --- a/reflex/utils/exec.py +++ b/reflex/utils/exec.py @@ -294,3 +294,16 @@ def is_testing_env() -> bool: True if the app is running in under pytest. """ return constants.PYTEST_CURRENT_TEST in os.environ + + +def is_prod_mode() -> bool: + """Check if the app is running in production mode. + + Returns: + True if the app is running in production mode or False if running in dev mode. + """ + current_mode = os.environ.get( + constants.ENV_MODE_ENV_VAR, + constants.Env.DEV.value, + ) + return current_mode == constants.Env.PROD.value