2

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!

1
  • can you do other things with an ajax request? usually ajax requests are made to a server location (e.g. /index.html) and not actual directories Commented Sep 9, 2016 at 16:45

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

I'll give this a try! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.