From local HTML file, shutdown down computer with Python/AJAX
<p>Been working with this for a while and found a few helpful things, but I'm not good with AJAX yet... </p>

<p>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:</p>

<pre><code>#! /usr/bin/python -u

import os
import shutil
import sys

os.system('sudo shutdown -h now')
</code></pre>

<p>I found this:</p>

<pre><code>$.ajax({
  type: "POST",
  url: "~/shutdown.py",
  data: { param: text}
}).done(function( o ) {
   // do something
});
</code></pre>

<p>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.</p>

<p>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...</p>

<p>Thanks!</p>

<p>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.</p>

<p>You could do this with flask.</p>

<p>Create a directory like this</p>

<pre><code>/kiosk
  /templates
    index.html
  app.py
</code></pre>

<p>The <code>index.html</code> is your current html page.  Here is what <code>app.py</code> would look like.</p>

<pre><code>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()
</code></pre>

<p>Your ajax call would look like this</p>

<pre><code>$.ajax({
  type: "POST",
  url: "/shutdown"
});
</code></pre>

<p>Then just cd into the app directory and run <code>python app.py</code>, which starts the web application.  Then open a browser and go to <code>localhost:5000</code></p>

python html ajax raspberry-pi shutdown