我要做的是从我定义的python脚本中调用一个函数来运行一个简单的gpio shell命令。
我不需要它返回或去任何地方,只需运行命令并留在页面上。
这就是我所拥有的。
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'])
我一直试图用{{pin0on}}或关闭来调用它。
带有按钮的页面是main.html,只有一个带有两个按钮的简单页面。我正在使用href来调用它。我似乎无法在任何地方找到任何东西。或者我可能只是做错了。
答案 0 :(得分:2)
您必须将代码放在视图函数中:
@app.route('/on')
def turn_on():
subprocess.call(['gpio write 0 1'], shell=True)
return '', 204 # no content
然后在模板中,按钮会是这样的:
<a href="{{ url_for('turn_on') }}">Turn on</a>
单击该按钮时,将触发turn_on
视图功能。