-------
Perfekt 👍
Hier ist ein Git-Patch im unified diff Format, der:
- Upload-Ordner ergänzt
- Upload-Route hinzufügt
- Reload-Funktion ergänzt
- HTML-Formular einbaut
Ich gehe davon aus, dass:
- knx-lens-web.py im Root liegt
- Templates unter templates/index.html liegen
- Das Projekt aktuell beim Start bereits einmal eingelesen wird
📦 Git Patch (unified diff)
Code:
diff --git a/knx-lens-web.py b/knx-lens-web.py
index 1234567..abcdef0 100644
--- a/knx-lens-web.py
+++ b/knx-lens-web.py
@@ -1,6 +1,16 @@
import os
+from werkzeug.utils import secure_filename
+from flask import request, redirect, flash
app = Flask(__name__)
+# --------------------------------------------------
+# Upload-Konfiguration
+# --------------------------------------------------
+UPLOAD_FOLDER = os.path.join(os.getcwd(), "uploaded_projects")
+ALLOWED_EXTENSIONS = {"knxproj"}
+os.makedirs(UPLOAD_FOLDER, exist_ok=True)
+app.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
+app.config["MAX_CONTENT_LENGTH"] = 50 * 1024 * 1024 # 50 MB
# --------------------------------------------------
# Bestehende Projekt-Initialisierung
@@ -20,6 +30,41 @@ knx_project = load_project_from_env()
+# --------------------------------------------------
+# Hilfsfunktion: Dateiendung prüfen
+# --------------------------------------------------
+def allowed_file(filename):
+ return "." in filename and \
+ filename.rsplit(".", 1)[1].lower() in ALLOWED_EXTENSIONS
+
+
+# --------------------------------------------------
+# Projekt neu laden
+# --------------------------------------------------
+def reload_project(path):
+ global knx_project
+ try:
+ knx_project = None
+ knx_project = load_project(path) # Bestehende Projekt-Ladefunktion verwenden!
+ print(f"Projekt neu geladen: {path}")
+ except Exception as e:
+ print(f"Fehler beim Neuladen: {e}")
+
+
+# --------------------------------------------------
+# Upload-Route
+# --------------------------------------------------
+@app.route("/upload_project", methods=["POST"])
+def upload_project():
+ if "project_file" not in request.files:
+ flash("Keine Datei ausgewählt")
+ return redirect("/")
+
+ file = request.files["project_file"]
+
+ if file.filename == "":
+ flash("Dateiname leer")
+ return redirect("/")
+
+ if not allowed_file(file.filename):
+ flash("Nur .knxproj Dateien erlaubt")
+ return redirect("/")
+
+ # Alte Dateien löschen (optional: nur eine Projektdatei aktiv)
+ for f in os.listdir(app.config["UPLOAD_FOLDER"]):
+ os.remove(os.path.join(app.config["UPLOAD_FOLDER"], f))
+
+ filename = secure_filename(file.filename)
+ save_path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
+ file.save(save_path)
+
+ reload_project(save_path)
+
+ flash("Projekt erfolgreich hochgeladen und neu geladen")
+ return redirect("/")
+
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
diff --git a/templates/index.html b/templates/index.html
index 7654321..fedcba9 100644
--- a/templates/index.html
+++ b/templates/index.html
@@ -10,6 +10,29 @@
<body>
+ <!-- Flash-Messages -->
+ {% with messages = get_flashed_messages() %}
+ {% if messages %}
+ <ul style="color: green;">
+ {% for message in messages %}
+ <li>{{ message }}</li>
+ {% endfor %}
+ </ul>
+ {% endif %}
+ {% endwith %}
+
+ <hr>
+ <h3>Neues ETS-Projekt hochladen</h3>
+
+ <form action="/upload_project" method="POST" enctype="multipart/form-data">
+ <input type="file"
+ name="project_file"
+ accept=".knxproj"
+ required>
+ <button type="submit">Projekt hochladen</button>
+ </form>
+
<!-- Bestehender Seiteninhalt -->
</body>
</html>
🛠 Anwendung des Patches
Datei z. B. speichern als:
Code:
knx-lens-upload.patch
Code:
git apply knx-lens-upload.patch
Code:
patch -p1 < knx-lens-upload.patch
⚠ Wichtiger Hinweis
Du musst in dieser Zeile:
Code:
knx_project = load_project(path)
(z. B. load_project_from_env() oder wie sie im Original heißt).




Einen Kommentar schreiben: