There is no
subprocess available in HTML/JavaScript. The simplest approach is to handle everything in Python. In this minimal example, a Python script starts a basic HTTP server, serves HTML with a "Shutdown" button, and executes the sudo halt command when the button is clicked.
The script below runs the HTTP server on port 9999 and serves a basic HTML page containing mentioned single "Shutdown" button.
Code: Select all
from http.server import BaseHTTPRequestHandler, HTTPServer
import subprocess
PORT = 9999
HTML_PAGE = b"""<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Shutdown</title>
</head>
<body style="font-family:sans-serif; text-align:center; margin-top:50px;">
<button onclick="sendShutdown()" style="font-size:24px; padding:10px 20px;">Shutdown</button>
<p id="status"></p>
<script>
function sendShutdown() {
fetch("/shutdown", { method: "POST" })
.then(r => r.text()).then(t => {
document.getElementById("status").innerText = t;
}).catch(e => {
document.getElementById("status").innerText = "Error: " + e;
});
}
</script>
</body>
</html>
"""
class ShutdownHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/":
self.send_response(200)
self.send_header("Content-Type", "text/html; charset=utf-8")
self.send_header("Content-Length", str(len(HTML_PAGE)))
self.end_headers()
self.wfile.write(HTML_PAGE)
else:
self.send_error(404, "Not Found")
def do_POST(self):
if self.path == "/shutdown":
self.send_response(200)
self.end_headers()
self.wfile.write(b"Shutdown command issued")
try:
subprocess.run(["sudo", "/sbin/halt"], check=True)
except Exception as e:
print(f"Error: {e}")
else:
self.send_error(404, "Not Found")
def log_message(self, format, *args):
return
if __name__ == "__main__":
print(f"Starting server on http://0.0.0.0:{PORT}")
with HTTPServer(("", PORT), ShutdownHandler) as httpd:
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")
Then, in applications.json (applications array), you can add the following entry (make sure the json syntax is correct):
Code: Select all
{
"action": "shutdown_pi",
"url": "http://127.0.0.1:9999",
"allowBackground": "false",
"controlAudioFocus": "false"
}
and in shortcuts.json (shortcuts array)
Code: Select all
{
"iconFontFamily": "Material Symbols Rounded",
"iconName": "power_settings_new",
"action": "shutdown_pi"
}
List of glyphs in Material Symbols Rounded font is available at
https://fonts.google.com/icons
To autostart the script you can add it to the /etc/xdg/labwc/autostart file, for example:
Note the
& at the end - it runs the script in the background, so it doesn't block the execution of other commands in the /etc/xdg/labwc/autostart file.
Of course, a more sophisticated HTML layout can be created to better match the Hudiy color scheme (see
https://github.com/wiboma/hudiy?tab=rea ... diy-object). However, the current version should serve as a good starting point. We've added the creation of such an enhanced example to our backlog, and it will be available on our GitHub soon.