Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Updated code in several modules
tmanik committed Aug 26, 2024
1 parent 6283fce commit 6c392a4
Showing 3 changed files with 39 additions and 22 deletions.
22 changes: 12 additions & 10 deletions 03-data-processing/README.md
@@ -80,11 +80,12 @@ Before starting this lesson, please ensure that you have:
import os
from sqlalchemy import create_engine, text

def load_data_to_postgres(input_dir='/data', db_name='weather_data'):
db_user = os.getenv('DB_USER', 'your_user')
db_password = os.getenv('DB_PASSWORD', 'your_password')
db_host = os.getenv('DB_HOST', 'postgres') # Ensure this is 'postgres' in Docker
db_port = os.getenv('DB_PORT', '5432')
def load_data_to_postgres(input_dir='/data'):
db_name = os.getenv('DB_NAME')
db_user = os.getenv('DB_USER')
db_password = os.getenv('DB_PASSWORD')
db_host = os.getenv('DB_HOST') # Ensure this is 'postgres' in Docker
db_port = os.getenv('DB_PORT')

# Create SQLAlchemy engine
engine = create_engine(f'postgresql+psycopg2://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}')
@@ -160,11 +161,12 @@ Before starting this lesson, please ensure that you have:
from sqlalchemy import create_engine, text
import os

def transform_and_load_final(db_name='weather_data'):
db_user = os.getenv('DB_USER', 'your_user')
db_password = os.getenv('DB_PASSWORD', 'your_password')
db_host = os.getenv('DB_HOST', 'postgres') # Ensure this is 'postgres' in Docker
db_port = os.getenv('DB_PORT', '5432')
def transform_and_load_final():
db_name = os.getenv('DB_NAME')
db_user = os.getenv('DB_USER')
db_password = os.getenv('DB_PASSWORD')
db_host = os.getenv('DB_HOST') # Ensure this is 'postgres' in Docker
db_port = os.getenv('DB_PORT')

# Create SQLAlchemy engine
engine = create_engine(f'postgresql+psycopg2://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}')
24 changes: 18 additions & 6 deletions 04-user-interface/README.md
@@ -100,14 +100,26 @@ Before starting this lesson, please ensure that you have:

8. Finally, open up the file named `.env`.

9. Copy the code below into `.env` and save it.
```text
# Database URL for PostgreSQL
DATABASE_URL=postgresql+psycopg2://your_user:your_password@postgres/weather_data
9. Add the following content to the `.env` file:

```
DB_NAME=weather_data
DB_USER=your_user
DB_PASSWORD=your_password
DB_HOST=postgres
DB_PORT=5432
```
This .env file contains environment variables for our Flask application. It defines the DATABASE_URL, which specifies how to connect to the PostgreSQL database containing our weather data. Using environment variables allows for easy configuration changes without modifying the application code, enhancing the portability and security of our containerized application.

Note: Make sure to replace 'your_user' and 'your_password' with the actual credentials you set up for your PostgreSQL database in the earlier steps of this workshop.
This `.env` file contains key-value pairs for our environment variables:

- `DB_NAME`: The name of our PostgreSQL database
- `DB_USER` and `DB_PASSWORD`: Credentials for accessing the database
- `DB_HOST`: Set to `postgres`, which is the service name of our PostgreSQL container
- `DB_PORT`: The port on which PostgreSQL is running (default is 5432)

Using a `.env` file allows us to keep sensitive information out of our version-controlled files and easily change configurations without modifying our Docker Compose file.

> **Warning:** While .env files are convenient for development, it's important to follow best practices, especially when moving towards production. We're using .env files for simplicity in this workshop, production deployments often require more robust security measures. Take a look at [.env Best Practices](../02-containerized-environment/README.md#env-best-practices) for more guidance.
## Conclusion

15 changes: 9 additions & 6 deletions flask-app/app.py
@@ -10,14 +10,17 @@
# Set the Matplotlib backend to Agg
plt.switch_backend('Agg')

# Load environment variables from .env file
load_dotenv()

app = Flask(__name__)

# Retrieve the database URL from environment variables
DATABASE_URL = os.getenv('DATABASE_URL')
engine = create_engine(DATABASE_URL)
db_name = os.getenv('DB_NAME')
db_user = os.getenv('DB_USER')
db_password = os.getenv('DB_PASSWORD')
db_host = os.getenv('DB_HOST') # Ensure this is 'postgres' in Docker
db_port = os.getenv('DB_PORT')

# Create SQLAlchemy engine
engine = create_engine(f'postgresql+psycopg2://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}')

@app.route('/')
def index():
@@ -63,4 +66,4 @@ def plot():
return send_file(img, mimetype='image/png')

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
app.run(host='0.0.0.0', port=5000)

0 comments on commit 6c392a4

Please sign in to comment.