-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·37 lines (24 loc) · 1022 Bytes
/
app.py
File metadata and controls
executable file
·37 lines (24 loc) · 1022 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env python3
"""Flask front-end app for magicframe"""
from flask import Flask, redirect, render_template, request, url_for
import vcgencmd
HOST="0.0.0.0"
PORT=8081
app = Flask(__name__, template_folder="templates", static_folder="static")
def on_or_off(newstatus: int) -> str:
return "on" if newstatus else "off"
@app.route("/", methods=["GET", "POST"])
def toggle():
if request.method == "GET":
newstatus = vcgencmd.get_status() ^ 1 # 1 or 0
return render_template("index.html", on_or_off=on_or_off(newstatus))
if request.method == "POST":
if "toggle" in request.form:
oldmsg = request.form.get("toggle") # previous value of toggle_message
action = oldmsg.split()[-1]
newstatus = 1 if action.lower() == "on" else 0 # "on" vs "off"
vcgencmd.set_status(newstatus)
return redirect(url_for("toggle"))
return render_template("index.html")
if __name__ == "__main__":
app.run(host=HOST, port=PORT)