0

What I am trying to do is call a function from the python script I have defined to run a simple gpio shell command.

I don't need it to return or go anywhere, just run the command and stay on the page.

Here is what I have.

from flask import Flask, render_template
import subprocess

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/main')
def main():
    return render_template('main.html')

#setup calls for hardware to set state
subprocess.call(['./gpio-out.sh'])


def pin0on():
    subprocess.call(['gpio write 0 1'])

def pin0off():
    subprocess.call(['gpio write 0 0'])

I have been trying to call it with {{ pin0on }} or off.

The page with the buttons is main.html with just a simple page with two buttons. I am using href to call it. I can't seem to find anything along these lines anywhere. Or I may be just doing this wrong.

1 Answer 1

2

You have to put the code in a view function:

@app.route('/on')
def turn_on():
    subprocess.call(['gpio write 0 1'], shell=True)
    return '', 204  # no content

Then in the template, the button will like this:

<a href="{{ url_for('turn_on') }}">Turn on</a>

When the button was clicked, it will trigger the turn_on view function.

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

3 Comments

Just tried that, didn't work. came up with can not find error.
OSError: [Errno 2] No such file or directory
subprocess.call(['gpio', 'write', '0', '0'], shell = True) fixed it, thanks!

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.