2017-02-15 265 views
0

我一直在试图通过HTML按钮运行python脚本(rainbow.py)。我不知道如何做到这一点,我发现的所有答案对我来说都没有意义。我想要的只是一个简单的解释,说明如何通过普通的HTML脚本运行python脚本。如何从html运行python脚本

我想在树莓派上做到这一点。

+0

你用什么样的HTTP服务器? –

+0

在你的文章(代码)中加入更多内容,也许你想要一个[framework](http://stackoverflow.com/a/5615268/5645103) –

回答

1

你可以把它通过Flask,一个Python的网络Microframework

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