WebApp-Kategorien anzeigen
This commit is contained in:
parent
af9e2fc138
commit
9205d11802
2 changed files with 152 additions and 0 deletions
79
app.py
79
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/<category>")
|
||||
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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue