From 9bf0df05940953ae13dee068f84d4ff5e17ef6b6 Mon Sep 17 00:00:00 2001 From: Antonin Filippi Date: Tue, 18 Mar 2025 13:27:22 +0000 Subject: [PATCH] add other folders --- z1/app/Dockerfile | 17 +++ z1/app/app.py | 233 ++++++++++++++++++++++++++++++++++++++++ z1/app/requirements.txt | 2 + 3 files changed, 252 insertions(+) create mode 100644 z1/app/Dockerfile create mode 100644 z1/app/app.py create mode 100644 z1/app/requirements.txt diff --git a/z1/app/Dockerfile b/z1/app/Dockerfile new file mode 100644 index 0000000..0a1261d --- /dev/null +++ b/z1/app/Dockerfile @@ -0,0 +1,17 @@ +FROM python:3.9-slim + +# Set the working directory +WORKDIR /app + +# Copy the requirements file and install Python dependencies +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the application source code +COPY app.py . + +# Expose port 5000 for the Flask application +EXPOSE 5000 + +# Start the Flask application +CMD ["python", "app.py"] \ No newline at end of file diff --git a/z1/app/app.py b/z1/app/app.py new file mode 100644 index 0000000..2012a70 --- /dev/null +++ b/z1/app/app.py @@ -0,0 +1,233 @@ +from flask import Flask, request, render_template_string, redirect, url_for +import os +import psycopg2 + +app = Flask(__name__) + +# Retrieve database connection settings from environment variables. +db_host = os.environ.get("DB_HOST", "postgres_db") +db_name = os.environ.get("POSTGRES_DB", "sampledb") +db_user = os.environ.get("POSTGRES_USER", "user") +db_password = os.environ.get("POSTGRES_PASSWORD", "password") + +# Function to get the database connection. +def get_db_connection(): + conn = psycopg2.connect( + host=db_host, + database=db_name, + user=db_user, + password=db_password + ) + return conn + +# Initialize the database and create the shopping list table if it doesn't exist. +def init_db(): + conn = get_db_connection() + cur = conn.cursor() + cur.execute(""" + CREATE TABLE IF NOT EXISTS shopping_list ( + id SERIAL PRIMARY KEY, + item VARCHAR(255) NOT NULL UNIQUE, + category VARCHAR(100) NOT NULL + ); + """) + conn.commit() + cur.close() + conn.close() + +# Function to categorize items intelligently. +def categorize_item(item_name): + item_name = item_name.lower() + + categories = { + 'fruits': ['apple', 'banana', 'orange', 'grape', 'pear'], + 'vegetables': ['carrot', 'broccoli', 'lettuce', 'spinach'], + 'dairy': ['milk', 'cheese', 'yogurt', 'butter'], + 'beverages': ['coffee', 'tea', 'water', 'juice'], + 'snacks': ['chips', 'cookies', 'chocolate', 'nuts'], + 'others': [] + } + + # Check each category and see if the item belongs. + for category, items in categories.items(): + if any(item in item_name for item in items): + return category + + # Default category if no match is found + return 'others' + +@app.route("/", methods=["GET", "POST"]) +def index(): + if request.method == "POST": + item = request.form.get("item") + if item: + # Check if item already exists in the database to avoid duplicates. + conn = get_db_connection() + cur = conn.cursor() + cur.execute("SELECT * FROM shopping_list WHERE item = %s", (item,)) + existing_item = cur.fetchone() + + if not existing_item: # Only insert if item doesn't exist already. + category = categorize_item(item) + cur.execute("INSERT INTO shopping_list (item, category) VALUES (%s, %s)", (item, category)) + conn.commit() + cur.close() + conn.close() + + conn = get_db_connection() + cur = conn.cursor() + cur.execute("SELECT * FROM shopping_list;") + items = cur.fetchall() + cur.close() + conn.close() + + # Group items by category for display purposes. + categorized_items = {} + for item in items: + if item[2] not in categorized_items: + categorized_items[item[2]] = [] + categorized_items[item[2]].append(item[1]) + + return render_template_string(""" + + + Smart Shopping List + + + +
+

Smart Shopping List

+
+ + +
+ {% for category, items in categorized_items.items() %} +
{{ category|capitalize }}
+ + {% endfor %} +
+ + + """, categorized_items=categorized_items) + +# Route to remove an item from the shopping list. +@app.route("/remove/") +def remove_item(item_name): + conn = get_db_connection() + cur = conn.cursor() + cur.execute("DELETE FROM shopping_list WHERE item = %s", (item_name,)) + conn.commit() + cur.close() + conn.close() + return redirect(url_for('index')) + +if __name__ == "__main__": + init_db() # Initialize the database and table + app.run(host='0.0.0.0', port=5000) diff --git a/z1/app/requirements.txt b/z1/app/requirements.txt new file mode 100644 index 0000000..88c17be --- /dev/null +++ b/z1/app/requirements.txt @@ -0,0 +1,2 @@ +Flask +psycopg2-binary \ No newline at end of file