lektion 03 angepasst
This commit is contained in:
1 parent
c60910e505
commit
1293a0f6a0
5 files changed
+247
-187
No files matched your search
+27
-39
@@ -1,55 +1,43 @@
|
||||
from flask import Flask, redirect, render_template_string, request, url_for
|
||||
from flask import Flask, jsonify, render_template, request, send_from_directory
|
||||
from gpiozero import LED
|
||||
|
||||
app = Flask(__name__)
|
||||
# Alle vier Projektdateien liegen direkt im Hauptverzeichnis.
|
||||
app = Flask(__name__, template_folder=".", static_folder=None)
|
||||
led = LED(17) # GPIO17 = physischer Pin 11
|
||||
|
||||
HTML = """
|
||||
<!doctype html>
|
||||
<html lang="de">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Raspberry-Pi-Labor – LED</title>
|
||||
<style>
|
||||
body { font-family: sans-serif; max-width: 600px; margin: 3rem auto;
|
||||
padding: 0 1rem; text-align: center; background: #f3f5f7; color: #222; }
|
||||
main { background: white; padding: 2rem; border-radius: 1rem; }
|
||||
.status { font-size: 1.4rem; margin: 1.5rem 0; }
|
||||
button { font-size: 1.2rem; padding: .8rem 1.5rem; margin: .4rem;
|
||||
border: 0; border-radius: .5rem; cursor: pointer; }
|
||||
.on { background: #b8e6c1; }
|
||||
.off { background: #f2c4c4; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<main>
|
||||
<h1>Raspberry-Pi-Labor</h1>
|
||||
<h2>LED-Steuerung</h2>
|
||||
<p class="status">Status: <strong>{{ "AN" if is_on else "AUS" }}</strong></p>
|
||||
<form method="post" action="{{ url_for('switch_led') }}">
|
||||
<button class="on" type="submit" name="state" value="on">LED einschalten</button>
|
||||
<button class="off" type="submit" name="state" value="off">LED ausschalten</button>
|
||||
</form>
|
||||
</main>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def index():
|
||||
return render_template_string(HTML, is_on=led.is_lit)
|
||||
"""Zeigt die Webseite mit dem aktuellen LED-Zustand."""
|
||||
return render_template("index.html", is_on=led.is_lit)
|
||||
|
||||
|
||||
@app.post("/led")
|
||||
def switch_led():
|
||||
state = request.form.get("state")
|
||||
@app.get("/style.css")
|
||||
def stylesheet():
|
||||
"""Liefert die CSS-Datei aus dem Projektverzeichnis."""
|
||||
return send_from_directory(app.root_path, "style.css")
|
||||
|
||||
|
||||
@app.get("/script.js")
|
||||
def javascript():
|
||||
"""Liefert die JavaScript-Datei aus dem Projektverzeichnis."""
|
||||
return send_from_directory(app.root_path, "script.js")
|
||||
|
||||
|
||||
@app.post("/api/led")
|
||||
def control_led():
|
||||
"""Schaltet die LED und liefert den neuen Zustand als JSON zurück."""
|
||||
data = request.get_json(silent=True) or {}
|
||||
state = data.get("state")
|
||||
|
||||
if state == "on":
|
||||
led.on()
|
||||
elif state == "off":
|
||||
led.off()
|
||||
return redirect(url_for("index"))
|
||||
else:
|
||||
return jsonify(error="Unbekannter LED-Zustand"), 400
|
||||
|
||||
return jsonify(is_on=led.is_lit)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user