Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
5cfba1c068 | |||
09a670727f | |||
f4a4597223
|
|||
70815f0172
|
|||
e4e8e19466 | |||
2228f0cfb5 | |||
24fb1c2941
|
|||
cc3a6381d5
|
|||
938562b9d8
|
|||
103d60764b
|
|||
8a446b8a8d
|
|||
5948cf3840
|
|||
63dbfcc89f
|
|||
439f6085e7
|
@ -7,9 +7,12 @@ COPY start.sh ${APP_DIR}/
|
||||
|
||||
WORKDIR ${APP_DIR}
|
||||
|
||||
RUN pip install -r requirements.txt
|
||||
RUN \
|
||||
apk add --no-cache build-base libpq-dev && \
|
||||
pip install -r requirements.txt
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
CMD "./start.sh"
|
||||
|
||||
|
||||
|
125
src/Run.py
125
src/Run.py
@ -4,6 +4,7 @@ from flask_oidc import OpenIDConnect
|
||||
from werkzeug.middleware.proxy_fix import ProxyFix
|
||||
import os
|
||||
import json
|
||||
import psycopg2
|
||||
|
||||
app = Flask(__name__)
|
||||
app.config.update({
|
||||
@ -19,76 +20,35 @@ app.config.update({
|
||||
oidc = OpenIDConnect(app)
|
||||
|
||||
|
||||
# Datenbankverbindung konfigurieren
|
||||
def get_db_connection():
|
||||
conn = sqlite3.connect('nutrition.db') # 'nutrition.db' ist der Name der Datenbankdatei
|
||||
conn.row_factory = sqlite3.Row # Ermöglicht den Zugriff auf Daten durch Spaltennamen
|
||||
return conn
|
||||
|
||||
|
||||
#def init_db():
|
||||
# conn = get_db_connection()
|
||||
# cursor = conn.cursor()
|
||||
#
|
||||
# # Erstellen der Tabelle
|
||||
# cursor.execute('''
|
||||
# CREATE TABLE IF NOT EXISTS nutrition_table (
|
||||
# id INTEGER PRIMARY KEY,
|
||||
# name TEXT NOT NULL,
|
||||
# kcal REAL,
|
||||
# EW REAL,
|
||||
# Fett REAL,
|
||||
# KH REAL,
|
||||
# BST REAL,
|
||||
# CA REAL
|
||||
# )
|
||||
# ''')
|
||||
#
|
||||
# # Testdaten einfügen
|
||||
# test_data = [
|
||||
# ('Apfel', 52, 0.3, 0.2, 14, 0.2, 6),
|
||||
# ('Banane', 89, 1.1, 0.3, 23, 0.3, 5),
|
||||
# ('Karotte', 41, 0.9, 0.2, 10, 0.2, 3),
|
||||
# ('Tomate', 18, 0.9, 0.2, 3.9, 0.2, 4),
|
||||
# ('Brokkoli', 34, 2.8, 0.4, 6.6, 0.4, 2),
|
||||
# ('Spinat', 23, 2.9, 0.4, 3.6, 0.4, 99),
|
||||
# ('Kartoffel', 77, 2, 0.1, 17, 0.1, 12),
|
||||
# ('Huhn', 239, 27, 14, 0, 0, 2),
|
||||
# ('Lachs', 208, 20, 13, 0, 0, 1),
|
||||
# ('Ei', 155, 13, 11, 1.1, 1, 1)
|
||||
# ]
|
||||
#
|
||||
# cursor.executemany('INSERT INTO nutrition_table (name, kcal, EW, Fett, KH, BST, CA) VALUES (?, ?, ?, ?, ?, ?, ?)', test_data)
|
||||
#
|
||||
# conn.commit()
|
||||
# conn.close()
|
||||
|
||||
|
||||
|
||||
def calculate_nutrition(food, weight):
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
conn = psycopg2.connect()
|
||||
|
||||
# Abfrage der Nährwertdaten aus der Datenbank
|
||||
cursor.execute('SELECT kcal, EW, Fett, KH, BST, CA FROM nutrition_table WHERE name = ?', (food,))
|
||||
with conn.cursor() as cursor:
|
||||
# Abfrage der Nährwertdaten aus der Datenbank
|
||||
cursor.execute('SELECT kcal, EW, Fett, KH, BST, CA FROM nutrition_table WHERE name = %s', (food,))
|
||||
|
||||
result = cursor.fetchone()
|
||||
conn.close()
|
||||
result = cursor.fetchone()
|
||||
|
||||
if result:
|
||||
# Runden und Berechnen der Nährwerte basierend auf dem Gewicht
|
||||
kcal, ew, fett, kh, bst, ca = result
|
||||
nutrition_values = [
|
||||
round(kcal * weight / 100), # kcal gerundet auf ganze Zahl
|
||||
round(ew * weight / 100, 1), # EW gerundet auf eine Dezimalstelle
|
||||
round(fett * weight / 100, 1), # Fett gerundet auf eine Dezimalstelle
|
||||
round(kh * weight / 100, 1), # KH gerundet auf eine Dezimalstelle
|
||||
round(bst * weight / 100, 1), # BST gerundet auf eine Dezimalstelle
|
||||
round(ca * weight / 100) # CA gerundet auf ganze Zahl
|
||||
]
|
||||
return nutrition_values
|
||||
else:
|
||||
return None
|
||||
if result:
|
||||
# Runden und Berechnen der Nährwerte basierend auf dem Gewicht
|
||||
kcal, ew, fett, kh, bst, ca = result
|
||||
nutrition_values = [
|
||||
round(kcal * weight / 100), # kcal gerundet auf ganze Zahl
|
||||
round(ew * weight / 100, 1), # EW gerundet auf eine Dezimalstelle
|
||||
round(fett * weight / 100, 1), # Fett gerundet auf eine Dezimalstelle
|
||||
round(kh * weight / 100, 1), # KH gerundet auf eine Dezimalstelle
|
||||
round(bst * weight / 100, 1), # BST gerundet auf eine Dezimalstelle
|
||||
round(ca * weight / 100) # CA gerundet auf ganze Zahl
|
||||
]
|
||||
return nutrition_values
|
||||
else:
|
||||
return None
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
# Index-Route
|
||||
@ -102,16 +62,16 @@ def index():
|
||||
@app.route('/get_products')
|
||||
@oidc.require_login
|
||||
def get_products():
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('SELECT name FROM nutrition_table')
|
||||
products = cursor.fetchall()
|
||||
conn.close()
|
||||
print("ter")
|
||||
return {'products': [product[0] for product in products]}
|
||||
|
||||
|
||||
|
||||
try:
|
||||
conn = psycopg2.connect()
|
||||
with conn.cursor() as cursor:
|
||||
cursor.execute('SELECT name FROM nutrition_table')
|
||||
products = cursor.fetchall()
|
||||
print("ter")
|
||||
return {'products': [product[0] for product in products]}
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
# Route zum Hinzufügen und Berechnen von Lebensmitteln
|
||||
@ -152,14 +112,17 @@ def add_nutrition():
|
||||
|
||||
print("test")
|
||||
# Verbindung zur Datenbank herstellen und Daten einfügen
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("INSERT INTO nutrition_table (name, kcal, ew, fett, kh, bst, ca) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
(food, kcal, ew, fett, kh, bst, ca))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
try:
|
||||
conn = psycopg2.connect()
|
||||
with conn.cursor() as cursor:
|
||||
cursor.execute("INSERT INTO nutrition_table (name, kcal, ew, fett, kh, bst, ca) VALUES (%s, %s, %s, %s, %s, %s, %s)",
|
||||
(food, kcal, ew, fett, kh, bst, ca))
|
||||
|
||||
return redirect(url_for('nutrition'))
|
||||
|
||||
return redirect(url_for('nutrition'))
|
||||
finally:
|
||||
if conn:
|
||||
conn.close()
|
||||
|
||||
|
||||
@app.route('/nutrition')
|
||||
|
@ -1,14 +1,13 @@
|
||||
import csv
|
||||
import sqlite3
|
||||
import psycopg2
|
||||
|
||||
# Pfad zur Ihrer CSV-Datei
|
||||
csv_file_path = 'nu.csv'
|
||||
|
||||
# Pfad zur Ihrer SQLite-Datenbank
|
||||
sqlite_db_path = 'nutrition.db'
|
||||
|
||||
# Verbindung zur SQLite-Datenbank herstellen
|
||||
conn = sqlite3.connect(sqlite_db_path)
|
||||
conn = psycopg2.connect()
|
||||
cursor = conn.cursor()
|
||||
|
||||
# Erstellen der Tabelle (falls noch nicht vorhanden)
|
||||
@ -29,8 +28,9 @@ with open(csv_file_path, newline='', encoding='utf-8') as csvfile:
|
||||
reader = csv.reader(csvfile)
|
||||
next(reader, None) # Überspringen der Kopfzeile
|
||||
for row in reader:
|
||||
cursor.execute('INSERT INTO nutrition_table (name, kcal, EW, Fett, KH, BST, Ca) VALUES (?, ?, ?, ?, ?, ?, ?)', row)
|
||||
cursor.execute('INSERT INTO nutrition_table (name, kcal, EW, Fett, KH, BST, Ca) VALUES (%s, %s, %s, %s, %s, %s, %s)', row)
|
||||
|
||||
# Änderungen speichern und Verbindung schließen
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
@ -13,6 +13,8 @@ itsdangerous==2.1.2
|
||||
Jinja2==3.1.3
|
||||
MarkupSafe==2.1.4
|
||||
packaging==23.2
|
||||
psycopg==3.1.17
|
||||
psycopg2==2.9.9
|
||||
pycparser==2.21
|
||||
requests==2.31.0
|
||||
typing_extensions==4.9.0
|
||||
|
BIN
src/static/images/favicon.ico
Normal file
BIN
src/static/images/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 13 KiB |
@ -121,3 +121,27 @@ tr:hover:not(.selected) {
|
||||
padding: 20px;
|
||||
margin: 20px 0;
|
||||
}
|
||||
|
||||
|
||||
#nutrition-input-table {
|
||||
width: 100%; /* Tabelle nimmt die volle Breite ein */
|
||||
border-collapse: collapse; /* Entfernt doppelte Ränder */
|
||||
}
|
||||
|
||||
#nutrition-input-table th, #nutrition-input-table td {
|
||||
border: 1px solid #ddd; /* Fügt Ränder hinzu */
|
||||
padding: 8px; /* Fügt Innenabstand hinzu */
|
||||
text-align: left; /* Ausrichtung des Textes */
|
||||
}
|
||||
|
||||
#nutrition-input-table input {
|
||||
width: 100%; /* Eingabefelder nehmen die volle Breite der Zelle ein */
|
||||
box-sizing: border-box; /* Box-Modell für die Breitenberechnung */
|
||||
}
|
||||
|
||||
@media screen and (max-width: 600px) {
|
||||
#nutrition-input-table {
|
||||
display: block;
|
||||
overflow-x: auto; /* Ermöglicht horizontales Scrollen auf kleinen Bildschirmen */
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@
|
||||
<meta charset="UTF-8">
|
||||
<title>Nährwertberechnungs-App</title>
|
||||
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
|
||||
<link rel="shortcut icon" href="../static/images/favicon.ico">
|
||||
|
||||
<script>
|
||||
|
||||
|
@ -4,6 +4,8 @@
|
||||
<meta charset="UTF-8">
|
||||
<title>Neue Lebensmittel hinzufügen</title>
|
||||
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
|
||||
<link rel="shortcut icon" href="../static/images/favicon.ico">
|
||||
|
||||
<script>
|
||||
function updateSubmitButtonState() {
|
||||
const inputs = document.querySelectorAll('#nutrition-form input');
|
||||
@ -62,13 +64,13 @@
|
||||
<th>CA</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><input type="text" name="food" oninput="updateSubmitButtonState()"></td>
|
||||
<td><input <input type="text" name="kcal" pattern="\d+(\.\d{1,2})?" oninput="updateSubmitButtonState()"></td>
|
||||
<td><input <input type="text" name="ew" pattern="\d+(\.\d{1,2})?" oninput="updateSubmitButtonState()"></td>
|
||||
<td><input <input type="text" name="fett" pattern="\d+(\.\d{1,2})?" oninput="updateSubmitButtonState()"></td>
|
||||
<td><input <input type="text" name="kh" pattern="\d+(\.\d{1,2})?" oninput="updateSubmitButtonState()"></td>
|
||||
<td><input <input type="text" name="bst" pattern="\d+(\.\d{1,2})?" oninput="updateSubmitButtonState()"></td>
|
||||
<td><input <input type="text" name="ca" pattern="\d+(\.\d{1,2})?" oninput="updateSubmitButtonState()"></td>
|
||||
<td><input type="text" name="food" placeholder="Lebensmittel" oninput="updateSubmitButtonState()"></td>
|
||||
<td><input <input type="text" name="kcal" pattern="\d+(\.\d{1,2})?" placeholder="g" oninput="updateSubmitButtonState()"></td>
|
||||
<td><input <input type="text" name="ew" pattern="\d+(\.\d{1,2})?" placeholder="g" oninput="updateSubmitButtonState()"></td>
|
||||
<td><input <input type="text" name="fett" pattern="\d+(\.\d{1,2})?" placeholder="g" oninput="updateSubmitButtonState()"></td>
|
||||
<td><input <input type="text" name="kh" pattern="\d+(\.\d{1,2})?" placeholder="g" oninput="updateSubmitButtonState()"></td>
|
||||
<td><input <input type="text" name="bst" pattern="\d+(\.\d{1,2})?" placeholder="g" oninput="updateSubmitButtonState()"></td>
|
||||
<td><input <input type="text" name="ca" pattern="\d+(\.\d{1,2})?" placeholder="mg" oninput="updateSubmitButtonState()"></td>
|
||||
</tr>
|
||||
</table>
|
||||
<button type="submit" id="submit-button" disabled>Hinzufügen</button>
|
||||
|
Reference in New Issue
Block a user