Browse Source

HOS-93: add support for .env file (#4219)

* HOS-93: add support for .env file

* HOS-93: remove stray print

* HOS-93: poetry lock

* HOS-93: update comment

---------

Co-authored-by: simon <simon@reflex.dev>
Simon Young 6 months ago
parent
commit
227fb2cb75
3 changed files with 36 additions and 11 deletions
  1. 25 11
      poetry.lock
  2. 1 0
      pyproject.toml
  3. 10 0
      reflex/config.py

+ 25 - 11
poetry.lock

@@ -570,18 +570,18 @@ test = ["pytest (>=6)"]
 
 
 [[package]]
 [[package]]
 name = "fastapi"
 name = "fastapi"
-version = "0.115.2"
+version = "0.115.3"
 description = "FastAPI framework, high performance, easy to learn, fast to code, ready for production"
 description = "FastAPI framework, high performance, easy to learn, fast to code, ready for production"
 optional = false
 optional = false
 python-versions = ">=3.8"
 python-versions = ">=3.8"
 files = [
 files = [
-    {file = "fastapi-0.115.2-py3-none-any.whl", hash = "sha256:61704c71286579cc5a598763905928f24ee98bfcc07aabe84cfefb98812bbc86"},
-    {file = "fastapi-0.115.2.tar.gz", hash = "sha256:3995739e0b09fa12f984bce8fa9ae197b35d433750d3d312422d846e283697ee"},
+    {file = "fastapi-0.115.3-py3-none-any.whl", hash = "sha256:8035e8f9a2b0aa89cea03b6c77721178ed5358e1aea4cd8570d9466895c0638c"},
+    {file = "fastapi-0.115.3.tar.gz", hash = "sha256:c091c6a35599c036d676fa24bd4a6e19fa30058d93d950216cdc672881f6f7db"},
 ]
 ]
 
 
 [package.dependencies]
 [package.dependencies]
 pydantic = ">=1.7.4,<1.8 || >1.8,<1.8.1 || >1.8.1,<2.0.0 || >2.0.0,<2.0.1 || >2.0.1,<2.1.0 || >2.1.0,<3.0.0"
 pydantic = ">=1.7.4,<1.8 || >1.8,<1.8.1 || >1.8.1,<2.0.0 || >2.0.0,<2.0.1 || >2.0.1,<2.1.0 || >2.1.0,<3.0.0"
-starlette = ">=0.37.2,<0.41.0"
+starlette = ">=0.40.0,<0.42.0"
 typing-extensions = ">=4.8.0"
 typing-extensions = ">=4.8.0"
 
 
 [package.extras]
 [package.extras]
@@ -1977,6 +1977,20 @@ files = [
 [package.dependencies]
 [package.dependencies]
 six = ">=1.5"
 six = ">=1.5"
 
 
+[[package]]
+name = "python-dotenv"
+version = "1.0.1"
+description = "Read key-value pairs from a .env file and set them as environment variables"
+optional = false
+python-versions = ">=3.8"
+files = [
+    {file = "python-dotenv-1.0.1.tar.gz", hash = "sha256:e324ee90a023d808f1959c46bcbc04446a10ced277783dc6ee09987c37ec10ca"},
+    {file = "python_dotenv-1.0.1-py3-none-any.whl", hash = "sha256:f7b63ef50f1b690dddf550d03497b66d609393b40b564ed0d674909a68ebf16a"},
+]
+
+[package.extras]
+cli = ["click (>=5.0)"]
+
 [[package]]
 [[package]]
 name = "python-engineio"
 name = "python-engineio"
 version = "4.10.1"
 version = "4.10.1"
@@ -2253,13 +2267,13 @@ idna2008 = ["idna"]
 
 
 [[package]]
 [[package]]
 name = "rich"
 name = "rich"
-version = "13.9.2"
+version = "13.9.3"
 description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
 description = "Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal"
 optional = false
 optional = false
 python-versions = ">=3.8.0"
 python-versions = ">=3.8.0"
 files = [
 files = [
-    {file = "rich-13.9.2-py3-none-any.whl", hash = "sha256:8c82a3d3f8dcfe9e734771313e606b39d8247bb6b826e196f4914b333b743cf1"},
-    {file = "rich-13.9.2.tar.gz", hash = "sha256:51a2c62057461aaf7152b4d611168f93a9fc73068f8ded2790f29fe2b5366d0c"},
+    {file = "rich-13.9.3-py3-none-any.whl", hash = "sha256:9836f5096eb2172c9e77df411c1b009bace4193d6a481d534fea75ebba758283"},
+    {file = "rich-13.9.3.tar.gz", hash = "sha256:bc1e01b899537598cf02579d2b9f4a415104d3fc439313a7a2c165d76557a08e"},
 ]
 ]
 
 
 [package.dependencies]
 [package.dependencies]
@@ -2525,13 +2539,13 @@ SQLAlchemy = ">=2.0.14,<2.1.0"
 
 
 [[package]]
 [[package]]
 name = "starlette"
 name = "starlette"
-version = "0.40.0"
+version = "0.41.0"
 description = "The little ASGI library that shines."
 description = "The little ASGI library that shines."
 optional = false
 optional = false
 python-versions = ">=3.8"
 python-versions = ">=3.8"
 files = [
 files = [
-    {file = "starlette-0.40.0-py3-none-any.whl", hash = "sha256:c494a22fae73805376ea6bf88439783ecfba9aac88a43911b48c653437e784c4"},
-    {file = "starlette-0.40.0.tar.gz", hash = "sha256:1a3139688fb298ce5e2d661d37046a66ad996ce94be4d4983be019a23a04ea35"},
+    {file = "starlette-0.41.0-py3-none-any.whl", hash = "sha256:a0193a3c413ebc9c78bff1c3546a45bb8c8bcb4a84cae8747d650a65bd37210a"},
+    {file = "starlette-0.41.0.tar.gz", hash = "sha256:39cbd8768b107d68bfe1ff1672b38a2c38b49777de46d2a592841d58e3bf7c2a"},
 ]
 ]
 
 
 [package.dependencies]
 [package.dependencies]
@@ -3033,4 +3047,4 @@ type = ["pytest-mypy"]
 [metadata]
 [metadata]
 lock-version = "2.0"
 lock-version = "2.0"
 python-versions = "^3.9"
 python-versions = "^3.9"
-content-hash = "8090ccaeca173bd8612e17a0b8d157d7492618e49450abd1c8373e2976349db0"
+content-hash = "c5da15520cef58124f6699007c81158036840469d4f9972592d72bd456c45e7e"

+ 1 - 0
pyproject.toml

@@ -33,6 +33,7 @@ jinja2 = ">=3.1.2,<4.0"
 psutil = ">=5.9.4,<7.0"
 psutil = ">=5.9.4,<7.0"
 pydantic = ">=1.10.2,<3.0"
 pydantic = ">=1.10.2,<3.0"
 python-multipart = ">=0.0.5,<0.1"
 python-multipart = ">=0.0.5,<0.1"
+python-dotenv = ">=1.0.1"
 python-socketio = ">=5.7.0,<6.0"
 python-socketio = ">=5.7.0,<6.0"
 redis = ">=4.3.5,<6.0"
 redis = ">=4.3.5,<6.0"
 rich = ">=13.0.0,<14.0"
 rich = ">=13.0.0,<14.0"

+ 10 - 0
reflex/config.py

@@ -436,6 +436,9 @@ class Config(Base):
     # Attributes that were explicitly set by the user.
     # Attributes that were explicitly set by the user.
     _non_default_attributes: Set[str] = pydantic.PrivateAttr(set())
     _non_default_attributes: Set[str] = pydantic.PrivateAttr(set())
 
 
+    # Path to file containing key-values pairs to override in the environment; Dotenv format.
+    env_file: Optional[str] = None
+
     def __init__(self, *args, **kwargs):
     def __init__(self, *args, **kwargs):
         """Initialize the config values.
         """Initialize the config values.
 
 
@@ -477,6 +480,7 @@ class Config(Base):
 
 
     def update_from_env(self) -> dict[str, Any]:
     def update_from_env(self) -> dict[str, Any]:
         """Update the config values based on set environment variables.
         """Update the config values based on set environment variables.
+        If there is a set env_file, it is loaded first.
 
 
         Returns:
         Returns:
             The updated config values.
             The updated config values.
@@ -486,6 +490,12 @@ class Config(Base):
         """
         """
         from reflex.utils.exceptions import EnvVarValueError
         from reflex.utils.exceptions import EnvVarValueError
 
 
+        if self.env_file:
+            from dotenv import load_dotenv
+
+            # load env file if exists
+            load_dotenv(self.env_file, override=True)
+
         updated_values = {}
         updated_values = {}
         # Iterate over the fields.
         # Iterate over the fields.
         for key, field in self.__fields__.items():
         for key, field in self.__fields__.items():