From 30933b0ca7f5742ebc4e59067a16e1068391aba4 Mon Sep 17 00:00:00 2001 From: "ishak jmilou.ishak" Date: Wed, 23 Oct 2024 15:08:23 +0200 Subject: [PATCH] Refactor button handling in index.html and app.py --- src/Python/flask/web/app.py | 24 +++++++++++------------ src/Python/flask/web/templates/index.html | 10 +++++----- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/Python/flask/web/app.py b/src/Python/flask/web/app.py index fef83d8..0383626 100644 --- a/src/Python/flask/web/app.py +++ b/src/Python/flask/web/app.py @@ -1,4 +1,4 @@ -from flask import Flask, request, render_template, redirect +from flask import Flask, request, render_template import paho.mqtt.client as mqtt app = Flask(__name__) @@ -9,19 +9,17 @@ mqtt_client = mqtt.Client() mqtt_client.username_pw_set("ishak", "kobuki") mqtt_client.connect("localhost", 1883, 60) mqtt_client.loop_start() -@app.route('/') -def index(): - return render_template('index.html') -@app.route('/move', methods=['POST']) -def move(): - direction = request.form['direction'] - result = mqtt_client.publish("home/commands", direction) - if result.rc == mqtt.MQTT_ERR_SUCCESS: - print("Bericht succesvol gepubliceerd") - else: - print("Fout bij het publiceren van bericht") - return redirect('/') +@app.route('/', methods=['GET', 'POST']) +def index(): + if request.method == 'POST': + direction = request.form['direction'] + result = mqtt_client.publish("home/commands", direction) + if result.rc == mqtt.MQTT_ERR_SUCCESS: + message = "Bericht succesvol gepubliceerd" + else: + message = "Fout bij het publiceren van bericht" + return render_template('index.html', message=message) if __name__ == '__main__': app.run(debug=True) diff --git a/src/Python/flask/web/templates/index.html b/src/Python/flask/web/templates/index.html index 0ab2680..28a99f2 100644 --- a/src/Python/flask/web/templates/index.html +++ b/src/Python/flask/web/templates/index.html @@ -4,11 +4,11 @@ Kobuki Robot
-
- - - - + + + + +