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)