From 9205d11802e15d57cd7e4036b7143d655a0294c0 Mon Sep 17 00:00:00 2001 From: hubobel Date: Sat, 20 Jun 2026 16:22:45 +0200 Subject: [PATCH] WebApp-Kategorien anzeigen --- app.py | 79 +++++++++++++++++++++++++++++++++++++++ templates/categories.html | 73 ++++++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 templates/categories.html diff --git a/app.py b/app.py index 1850b83..258062f 100644 --- a/app.py +++ b/app.py @@ -2,10 +2,15 @@ from pathlib import Path from flask import Flask, render_template, request import subprocess import sys +import json +from pathlib import Path +from flask import redirect app = Flask(__name__) BASE_DIR = Path(__file__).parent +CATEGORIES_FILE = BASE_DIR / "Kategorien.json" + @app.route("/") @@ -82,7 +87,81 @@ def run_categorize(): output=result.stdout + result.stderr ) +@app.route("/categories") +def categories(): + with open( + CATEGORIES_FILE, + encoding="utf-8" + ) as f: + + categories = json.load(f) + print(categories) + return render_template( + "categories.html", + categories=categories + ) +@app.route("/categories/delete/") +def delete_category(category): + + with open( + CATEGORIES_FILE, + encoding="utf-8" + ) as f: + + categories = json.load(f) + + if category in categories: + del categories[category] + + with open( + CATEGORIES_FILE, + "w", + encoding="utf-8" + ) as f: + + json.dump( + categories, + f, + ensure_ascii=False, + indent=2 + ) + + return redirect("/categories") +@app.route( + "/categories/add", + methods=["POST"] +) +def add_category(): + + category = request.form["category"].strip() + + with open( + CATEGORIES_FILE, + encoding="utf-8" + ) as f: + + categories = json.load(f) + + if category not in categories: + categories[category] = [] + + with open( + CATEGORIES_FILE, + "w", + encoding="utf-8" + ) as f: + + json.dump( + categories, + f, + ensure_ascii=False, + indent=2 + ) + + + + return redirect("/categories") if __name__ == "__main__": app.run( diff --git a/templates/categories.html b/templates/categories.html new file mode 100644 index 0000000..9cc323a --- /dev/null +++ b/templates/categories.html @@ -0,0 +1,73 @@ + + + + + + Kategorien + + + +

Kategorien

+ +Zurück + +
+ +

Neue Kategorie

+ +
+ +``` + + + +``` + +
+ +
+ + + + + + + + + +{% for category, words in categories.items() %} + + + +``` + + + + + +``` + + + +{% endfor %} + +
KategorieSchlüsselwörterAktion
+ {{ category }} + + {{ words|join(", ") }} + + + Löschen + +
+ + +