2 回答

TA貢獻1820條經(jīng)驗 獲得超10個贊
下面是在按鈕按下時運行代碼的簡單解決方案。如果要在后臺運行任務(wù),請查看芹菜或燒瓶中的線程。
from flask import Flask, render_template, jsonify
import test
app = Flask(__name__)
@app.route('/', methods=['POST', 'GET'])
def index():
if request.method == "POST":
<insert python script here>
return render_template('index.html')
將您的模板更改為此:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form method = "post">
<input type="button" id='script' name="submit" value="Run Script">
</form>
</body>
</html>

TA貢獻1820條經(jīng)驗 獲得超9個贊
一些使用Flask函數(shù),Jquery和Bootstrap的示例(不是必需的,僅用于按鈕格式設(shè)置)。希望這可以對您有所幫助。Python(在應(yīng)用腳本中):
@app.route('/do_something')
def do_something():
"""
Do something on button press.
"""
# your code
return res
Javascript:
var btnName = function(){
$.get('/do_something',
function(x){
$("#resBtnHere").html(x);
}
)
}
btnName()
html中的按鈕: <button type="button" class="btn btn-primary" onclick="btnName()">Click here</button>
在html中,按功能的結(jié)果(如果需要): <p id="resBtnHere"></p>
添加回答
舉報