2 回答
TA贡献1820条经验 获得超10个赞
下面是在按钮按下时运行代码的简单解决方案。如果要在后台运行任务,请查看芹菜或烧瓶中的线程。
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条经验 获得超9个赞
一些使用Flask函数,Jquery和Bootstrap的示例(不是必需的,仅用于按钮格式设置)。希望这可以对您有所帮助。Python(在应用脚本中):
@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中,按功能的结果(如果需要): <p id="resBtnHere"></p>
添加回答
举报