Skip to content

Commit 33de56b

Browse files
authored
use granian env_files option instead of reload hook (#5512)
1 parent e9c2e4b commit 33de56b

3 files changed

Lines changed: 52 additions & 23 deletions

File tree

reflex/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
from reflex.environment import EnvVar as EnvVar
2222
from reflex.environment import (
2323
ExistingPath,
24-
_load_dotenv_from_str,
24+
_load_dotenv_from_files,
25+
_paths_from_env_files,
2526
interpret_env_var_value,
2627
)
2728
from reflex.environment import env_var as env_var
@@ -328,7 +329,7 @@ def update_from_env(self) -> dict[str, Any]:
328329
The updated config values.
329330
"""
330331
if self.env_file:
331-
_load_dotenv_from_str(self.env_file)
332+
_load_dotenv_from_files(_paths_from_env_files(self.env_file))
332333

333334
updated_values = {}
334335
# Iterate over the fields.

reflex/environment.py

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,36 @@ class EnvironmentVariables:
613613
load_dotenv = None
614614

615615

616-
def _load_dotenv_from_str(env_files: str) -> None:
616+
def _paths_from_env_files(env_files: str) -> list[Path]:
617+
"""Convert a string of paths separated by os.pathsep into a list of Path objects.
618+
619+
Args:
620+
env_files: The string of paths.
621+
622+
Returns:
623+
A list of Path objects.
624+
"""
625+
# load env files in reverse order
626+
return list(
627+
reversed(
628+
[
629+
Path(path)
630+
for path_element in env_files.split(os.pathsep)
631+
if (path := path_element.strip())
632+
]
633+
)
634+
)
635+
636+
637+
def _load_dotenv_from_files(files: list[Path]):
638+
"""Load environment variables from a list of files.
639+
640+
Args:
641+
files: A list of Path objects representing the environment variable files.
642+
"""
617643
from reflex.utils import console
618644

619-
if not env_files:
645+
if not files:
620646
return
621647

622648
if load_dotenv is None:
@@ -625,19 +651,27 @@ def _load_dotenv_from_str(env_files: str) -> None:
625651
)
626652
return
627653

628-
# load env files in reverse order if they exist
629-
for env_file_path in [
630-
Path(p) for s in reversed(env_files.split(os.pathsep)) if (p := s.strip())
631-
]:
632-
if env_file_path.exists():
633-
load_dotenv(env_file_path, override=True)
654+
for env_file in files:
655+
if env_file.exists():
656+
load_dotenv(env_file, override=True)
657+
658+
659+
def _paths_from_environment() -> list[Path]:
660+
"""Get the paths from the REFLEX_ENV_FILE environment variable.
661+
662+
Returns:
663+
A list of Path objects.
664+
"""
665+
env_files = os.environ.get("REFLEX_ENV_FILE")
666+
if not env_files:
667+
return []
668+
669+
return _paths_from_env_files(env_files)
634670

635671

636672
def _load_dotenv_from_env():
637673
"""Load environment variables from paths specified in REFLEX_ENV_FILE."""
638-
env_env_file = os.environ.get("REFLEX_ENV_FILE")
639-
if env_env_file:
640-
_load_dotenv_from_str(env_env_file)
674+
_load_dotenv_from_files(_paths_from_environment())
641675

642676

643677
# Load the env files at import time if they are set in the ENV_FILE environment variable.

reflex/utils/exec.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -502,13 +502,6 @@ def run_uvicorn_backend(host: str, port: int, loglevel: LogLevel):
502502
)
503503

504504

505-
def _reload_hook():
506-
"""Hook to load environment variables from .env file."""
507-
from reflex.environment import _load_dotenv_from_env
508-
509-
_load_dotenv_from_env()
510-
511-
512505
def run_granian_backend(host: str, port: int, loglevel: LogLevel):
513506
"""Run the backend in development mode using Granian.
514507
@@ -526,7 +519,9 @@ def run_granian_backend(host: str, port: int, loglevel: LogLevel):
526519

527520
from granian.constants import Interfaces
528521
from granian.log import LogLevels
529-
from granian.server import MPServer as Granian
522+
from granian.server import Server as Granian
523+
524+
from reflex.environment import _paths_from_environment
530525

531526
granian_app = Granian(
532527
target=get_app_instance_from_file(),
@@ -540,11 +535,10 @@ def run_granian_backend(host: str, port: int, loglevel: LogLevel):
540535
reload_ignore_worker_failure=True,
541536
reload_ignore_patterns=HOTRELOAD_IGNORE_PATTERNS,
542537
reload_tick=100,
538+
env_files=_paths_from_environment() or None,
543539
workers_kill_timeout=2,
544540
)
545541

546-
granian_app.on_reload(_reload_hook)
547-
548542
granian_app.serve()
549543

550544

0 commit comments

Comments
 (0)