From 4de2970b9e086194a2fd1941b3dd04cf99f39ef1 Mon Sep 17 00:00:00 2001 From: Pei-Yao Hung Date: Thu, 20 Nov 2025 18:25:54 -0500 Subject: [PATCH 1/2] Update README with Mac conda instructions and DB setup Added Mac-specific conda environment creation instructions and expanded the PostgreSQL setup section with download link and example commands for creating a database. --- README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0ab1c6e..605cb62 100644 --- a/README.md +++ b/README.md @@ -87,12 +87,28 @@ This template utilizes PostgreSQL as the database backend. 2. **Create and activate the conda environment**: ```sh + # Windows conda env create -f environment.yml + # Mac + conda env create -f environment_mac.yml conda activate justin_rl_api ``` 3. **Configure the database**: - Setup a PostgreSQL database and create a new database for the application. + Setup a PostgreSQL database ([download](https://www.postgresql.org/download/ )) and create a new database for the application. + + Create a database + ```sh + # in shell + psql -U postgres + + # in psql + CREATE DATABASE your_db_name; + + ``` + + + Update the database connection string in ```config.py```. For PostgreSQL, the string looks like: ```sh From 3a3e5abf489671b91d60382edd3019cbe74b4e23 Mon Sep 17 00:00:00 2001 From: Pei-Yao Hung Date: Thu, 20 Nov 2025 19:28:39 -0500 Subject: [PATCH 2/2] Add dotenv support for database config and update docs Added python-dotenv to environment files and updated config.py to load environment variables from a .env file. Updated README to instruct users to set the database URI in a .env file instead of editing config.py directly. Also updated .gitignore to exclude .env files. Using a .env file provides a secure and flexible way to manage configuration variables (like the database connection string) separately from your source code. This is essential because the connection string could contain sensitive credentials and will be different for every user. --- .gitignore | 3 ++- README.md | 6 ++---- config.py | 4 +++- environment.yml | 1 + environment_mac.yml | 1 + 5 files changed, 9 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index b7c4c7b..0919105 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ logs/ __pycache__/ *.pyc .pytest_cache/ -backups/ \ No newline at end of file +backups/ +.env \ No newline at end of file diff --git a/README.md b/README.md index 605cb62..e3f975d 100644 --- a/README.md +++ b/README.md @@ -107,12 +107,10 @@ This template utilizes PostgreSQL as the database backend. ``` - - - Update the database connection string in ```config.py```. For PostgreSQL, the string looks like: + Create a file ".env" at the root folder of your project and add the database connection string. For PostgreSQL, the string looks like: ```sh - SQLALCHEMY_DATABASE_URI = "postgresql://:@:/" + SQLALCHEMY_DATABASE_URI="postgresql://:@:/" ``` 4. **Initialize the database**: diff --git a/config.py b/config.py index d4ecd94..ab1a2e6 100644 --- a/config.py +++ b/config.py @@ -1,5 +1,7 @@ import os +from dotenv import load_dotenv +load_dotenv() class Config: # General Configuration @@ -25,7 +27,7 @@ class Config: # "DATABASE_URL", "postgresql://myuser:mypassword@localhost:5432/mydatabase" # ) SQLALCHEMY_TRACK_MODIFICATIONS = False - SQLALCHEMY_DATABASE_URI = "postgresql://zipingxu@localhost:5432/justin_rl_db" + SQLALCHEMY_DATABASE_URI = os.getenv('SQLALCHEMY_DATABASE_URI') or "postgresql://zipingxu@localhost:5432/justin_rl_db" SQLALCHEMY_ENGINE_OPTIONS = { "pool_recycle": 3600, # Recycle connections after 3600 seconds (1 hour) } diff --git a/environment.yml b/environment.yml index 916a8a8..6d7fcec 100644 --- a/environment.yml +++ b/environment.yml @@ -18,6 +18,7 @@ dependencies: - pip=24.2 - psycopg2=2.9.9 - python=3.11.11 + - python-dotenv=1.1.0 - setuptools=75.1.0 - sqlalchemy=2.0.34 - sqlite=3.45.3 diff --git a/environment_mac.yml b/environment_mac.yml index 7363c2a..aeb85ed 100644 --- a/environment_mac.yml +++ b/environment_mac.yml @@ -18,6 +18,7 @@ dependencies: - pip=24.2 - psycopg2=2.9.9 - python=3.11.11 + - python-dotenv=1.1.0 - setuptools=75.1.0 - sqlalchemy=2.0.34 - sqlite=3.45.3