Been working with this for a while and found a few helpful things, but I'm not good with AJAX yet...
I'm working to make a chromium-browser kiosk on the Raspberry Pi (which has been fine) but when in kiosk mode, there's not a "shutdown" button. I'm displaying a local HTML file in the chromium-browser and I want to create a button in the local HTML file that will shutdown the computer using AJAX/JQuery by calling a simple python code I made:
#! /usr/bin/python -u
import os
import shutil
import sys
os.system('sudo shutdown -h now')
I found this:
$.ajax({
type: "POST",
url: "~/shutdown.py",
data: { param: text}
}).done(function( o ) {
// do something
});
How do I connect this though? there's no output from my python, just want to call the python code and have the raspberry pi shutdown. The python code when I run it in its own terminal shuts down the computer as expected.
Or if you have any other ideas for shutting down the Rpi while in Kiosk mode in the most "user friendly" way, let me know! The people using this won't know about using the terminal or SSH-ing in...
Thanks!
Probably the easiest way would be to run a local web server that listens for a request and then shuts down the computer. Instead of just displaying a local HTML file, actually serve it from flask. This gives you much more freedom, since you can run the commands server-side (even though the server and client are the same in this case) where you don't have restrictions like you do within a browser environment.
You could do this with flask.
Create a directory like this
/kiosk
/templates
index.html
app.py
The index.html
is your current html page. Here is what app.py
would look like.
from flask import Flask, render_template
app = Flask(__name__)
@app.route("/")
def index():
return render_template('index.html')
@app.route("/shutdown")
def shutdown():
os.system('sudo shutdown -h now')
if __name__ == "__main__":
app.run()
Your ajax call would look like this
$.ajax({
type: "POST",
url: "/shutdown"
});
Then just cd into the app directory and run python app.py
, which starts the web application. Then open a browser and go to localhost:5000