Skip to content

Commit

Permalink
Created new folder for flask app
Browse files Browse the repository at this point in the history
Basic version of flask app. Will ned to improve upon
  • Loading branch information
tmanik committed Aug 7, 2024
1 parent 1fbc666 commit da76953
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@
# ignore drafts
01-basic-deployment-new/*
02-advanced-deployment-old/*

# ignore all files that are .env
*.env
8 changes: 7 additions & 1 deletion 02-advanced-deployment/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ services:
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: ${DB_NAME}
networks:
- mynetwork
ports:
- "5432:5432"
volumes:
Expand Down Expand Up @@ -61,4 +63,8 @@ services:

volumes:
pgdata:
shared-data:
shared-data:

networks:
mynetwork:
driver: bridge
18 changes: 18 additions & 0 deletions 03-flask-app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Base image
FROM python:3.11

# Set working directory
WORKDIR /app

# Copy requirements and install
COPY requirements.txt .
RUN pip install -r requirements.txt

# Copy all files
COPY . .

# Expose port for Flask
EXPOSE 5000

# Run Flask app
CMD ["python", "flask_app/app.py"]
14 changes: 14 additions & 0 deletions 03-flask-app/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: '3'
services:
flask_app:
build: .
ports:
- "5000:5000"
env_file:
- .env
networks:
- 02-advanced-deployment_mynetwork

networks:
02-advanced-deployment_mynetwork:
external: true
28 changes: 28 additions & 0 deletions 03-flask-app/flask_app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
from flask import Flask, render_template
from sqlalchemy import create_engine
import pandas as pd
from dotenv import load_dotenv

# 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)

@app.route('/')
def index():
# Fetch data from database
query = "SELECT * FROM weather LIMIT 10;"
df = pd.read_sql(query, engine)

# Convert data to HTML table
data = df.to_html(classes='table table-striped')

return render_template('index.html', table=data)

if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
22 changes: 22 additions & 0 deletions 03-flask-app/flask_app/static/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
body {
font-family: Arial, sans-serif;
}

.container {
width: 80%;
margin: auto;
}

table {
width: 100%;
border-collapse: collapse;
}

th, td {
border: 1px solid #ddd;
padding: 8px;
}

th {
background-color: #f2f2f2;
}
14 changes: 14 additions & 0 deletions 03-flask-app/flask_app/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Data Visualization</title>
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
<h1>Data Visualization</h1>
<div class="container">
{{ table|safe }}
</div>
</body>
</html>
12 changes: 12 additions & 0 deletions 03-flask-app/flask_app/templates/layout.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Flask App</title>
<link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
</head>
<body>
{% block content %}
{% endblock %}
</body>
</html>
8 changes: 8 additions & 0 deletions 03-flask-app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Flask==1.1.4
Werkzeug==0.16.1
python-dotenv==1.0.0
pandas
numpy
sqlalchemy==2.0.0
psycopg2-binary==2.9.6
markupsafe==2.0.1

0 comments on commit da76953

Please sign in to comment.