我目前正在尝试使用Flask创建一个仅包含一个按钮的网站(目前)。如果我按下该按钮,我想从特定路径或仅在项目文件夹中运行python脚本。我已经看到了一些关于同一主题的帖子,但是它们都无法真正帮助我。
我已经有一些代码了。这是我的Flask app.py
from flask import Flask, render_template, jsonify
import test
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run(debug=True)
那就是我的index.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<input type="button" id='script' name="scriptbutton" value=" Run Script " onclick="goPython()">
<script src="http://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script>
function goPython(){
$.ajax({
type: 'POST',
url: "/test.py",
//or some other path like /scripts/test.py
dataType: "text",
success: function (data) {
alert(data)
},
error: function (data) {
alert(data)
}
});
}
</script>
</body>
</html>
如果我运行烧瓶网站,然后单击按钮,我要执行保存在项目文件夹或其他位置的test.py。我不知道这种方法是否可行,或者是否有更好的解决方案。现在,我仍在尝试使用flask,但是无法运行test.py。当我按下按钮时,它只向我显示[object Object]的警报
基本上,我要建立的网站是带有按钮的网站,例如一项在后台运行我的脚本的服务,有时可能需要更多时间才能完成。我不确定在这种情况下是否误解了ajax的使用。你能帮忙的话,我会很高兴。
拉莫斯之舞
慕妹3146593
相关分类