【发布时间】:2020-04-02 07:22:54
【问题描述】:
我是 Flask 的新手,但我试图在页面加载时显示“滚动球”。
此链接:Display a ‘loading’ message while a time consuming function is executed in Flask 很有帮助,但没有给我想要的结果。
from flask import Flask
from flask import request
from flask import render_template
import time
app = Flask(__name__)
def long_load(typeback):
time.sleep(5) #just simulating the waiting period
return "You typed: %s" % typeback
@app.route('/', methods=("POST", "GET"))
def test():
if request.method == 'GET':
return render_template('index.html')
elif request.method == 'POST':
query = request.form['anything']
outcome = long_load(query)
return render_template("post_template.html" , display=outcome)
if __name__ == '__main__':
app.run()
index.html 节选:
<head>
<style>
div#loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid #3498db;
width: 120px;
height: 120px;
-webkit-animation: spin 2s linear infinite; /* Safari */
animation: spin 2s linear infinite ;
}
/* Safari */
@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
<script type="text/javascript">// <![CDATA[
document.onreadystatechange = function() {
if (document.readyState !== "complete") {
document.querySelector("body").style.visibility = "hidden";
document.querySelector("#loader").style.visibility = "visible";
} else {
document.querySelector("#loader").style.display = "none";
document.querySelector("body").style.visibility = "visible";
}
};
// ]]></script>
</head>
<form action="." method="post">>
<body>
<div class="caption">
<table class="center">
<tr>
<td class="NEONpinktxt"> </td>
<td align = "center"> <input type="submit" name="anything_submit" href="#" value="Search Results" id="loader" > </td>
</tr>
<div id="loader"></div>
</table>
</div>
</body>
</form>
当页面加载或刷新时,滚动球会显示,但是当点击“搜索结果”时,什么也没有发生。
【问题讨论】:
-
输入没有href。您需要链接或表单
-
为什么脚本要包裹在样式标签中?
-
@mplungjan,写这篇文章的时候搞错了,脚本不在样式标签中,现在编辑。
-
表单也包裹了body标签。
-
您也将页面提交到服务器。为什么悸动者(也就是所谓的)会跑?如果你想搜索显示一个 throbber,你需要 Ajax
标签: html python-3.x flask