2017-02-15 134 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