如何从html运行python脚本

时间:2017-02-15 23:57:21

标签: html python-3.x raspberry-pi

我一直试图通过HTML按钮运行python脚本(rainbow.py)。我不知道怎么做这个,我找到的所有答案对我都没有意义。我只想简单解释如何通过常规HTML脚本运行python脚本。

我正在尝试在Raspberry Pi上执行此操作。

1 个答案:

答案 0 :(得分:2)

您可以通过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