【问题标题】:How to run a python script from html如何从 html 运行 python 脚本
【发布时间】:2017-02-15 23:57:20
【问题描述】:
我一直在尝试通过 HTML 按钮运行 python 脚本 (rainbow.py)。我不知道该怎么做,我找到的所有答案对我来说毫无意义。我想要的只是简单解释一下如何通过常规 HTML 脚本运行 python 脚本。
我正在尝试在 Raspberry Pi 上执行此操作。
【问题讨论】:
标签:
html
python-3.x
raspberry-pi
【解决方案1】:
您可以通过Flask,一个Python Web 微框架:
HTML (index.html):
<a href="{{ url_for('do_something') }}">Your button</a>
Python (app.py):
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
# render your html template
return render_template('index.html')
@app.route('/something')
def do_something():
# when the button was clicked,
# the code below will be execute.
print 'do something here'
...
if __name__ == '__main__':
app.run()
启动服务器:$ python app.py
然后转到http://127.0.0.1:5000。
文件结构:
template\
-index.html
app.py