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(
|
||||
|
|
|
|||
73
templates/categories.html
Normal file
73
templates/categories.html
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
<!DOCTYPE html>
|
||||
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Kategorien</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h1>Kategorien</h1>
|
||||
|
||||
<a href="/">Zurück</a>
|
||||
|
||||
<hr>
|
||||
|
||||
<h2>Neue Kategorie</h2>
|
||||
|
||||
<form action="/categories/add" method="post">
|
||||
|
||||
```
|
||||
<input
|
||||
type="text"
|
||||
name="category"
|
||||
placeholder="Neue Kategorie"
|
||||
required>
|
||||
|
||||
<button type="submit">
|
||||
Anlegen
|
||||
</button>
|
||||
```
|
||||
|
||||
</form>
|
||||
|
||||
<hr>
|
||||
|
||||
<table border="1" cellpadding="5">
|
||||
|
||||
<tr>
|
||||
<th>Kategorie</th>
|
||||
<th>Schlüsselwörter</th>
|
||||
<th>Aktion</th>
|
||||
</tr>
|
||||
|
||||
{% for category, words in categories.items() %}
|
||||
|
||||
<tr>
|
||||
|
||||
```
|
||||
<td>
|
||||
{{ category }}
|
||||
</td>
|
||||
|
||||
<td>
|
||||
{{ words|join(", ") }}
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<a
|
||||
href="/categories/delete/{{ category }}"
|
||||
onclick="return confirm('Kategorie wirklich löschen?')">
|
||||
Löschen
|
||||
</a>
|
||||
</td>
|
||||
```
|
||||
|
||||
</tr>
|
||||
|
||||
{% endfor %}
|
||||
|
||||
</table>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue