Skip to content

Commit 44232fb

Browse files
authored
Read environment secrets in state at import time (#5120)
1 parent ca7f663 commit 44232fb

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

reflex/config.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,29 @@
4949
load_dotenv = None
5050

5151

52+
def _load_dotenv_from_str(env_files: str) -> None:
53+
if not env_files:
54+
return
55+
56+
if load_dotenv is None:
57+
console.error(
58+
"""The `python-dotenv` package is required to load environment variables from a file. Run `pip install "python-dotenv>=1.0.1"`."""
59+
)
60+
return
61+
62+
# load env files in reverse order if they exist
63+
for env_file_path in [
64+
Path(p) for s in reversed(env_files.split(os.pathsep)) if (p := s.strip())
65+
]:
66+
if env_file_path.exists():
67+
load_dotenv(env_file_path, override=True)
68+
69+
70+
# Load the env files at import time if they are set in the ENV_FILE environment variable.
71+
if load_dotenv is not None and (env_files := os.getenv("ENV_FILE")):
72+
_load_dotenv_from_str(env_files)
73+
74+
5275
class DBConfig(Base):
5376
"""Database config."""
5477

@@ -936,21 +959,8 @@ def update_from_env(self) -> dict[str, Any]:
936959
Returns:
937960
The updated config values.
938961
"""
939-
env_file = self.env_file or os.environ.get("ENV_FILE", None)
940-
if env_file:
941-
if load_dotenv is None:
942-
console.error(
943-
"""The `python-dotenv` package is required to load environment variables from a file. Run `pip install "python-dotenv>=1.0.1"`."""
944-
)
945-
else:
946-
# load env files in reverse order if they exist
947-
for env_file_path in [
948-
Path(p)
949-
for s in reversed(env_file.split(os.pathsep))
950-
if (p := s.strip())
951-
]:
952-
if env_file_path.exists():
953-
load_dotenv(env_file_path, override=True)
962+
if self.env_file:
963+
_load_dotenv_from_str(self.env_file)
954964

955965
updated_values = {}
956966
# Iterate over the fields.

0 commit comments

Comments
 (0)