在Flask中执行耗时的功能时显示“加载”消息 [英] Display a ‘loading’ message while a time consuming function is executed in Flask
问题描述
问题是我不能让用户知道他们的查询是在这个时候加载的,因为结果页面只有在函数完成工作时才加载。有什么方法可以显示加载消息,而这是怎么回事?我发现一些JavaScript可以显示加载消息,而页面元素仍然加载,但我的等待期发生在'render_template'之前。
我敲了一些示例代码,只是以证明我的情况:
Python:
from flask import Flask
from flask导入请求$ b $ from flask导入render_template
导入时间
$ b $ app = Flask(__ name__)
$ b $ def long_load(typeback):
time.sleep(5)#just模拟等待期间
returnYou typed:%s%typeback
@ app.route('/')
def home():
return render_template(index.html)
@ app.route('/',methods = ['POST'])
def form (display = None):
query = request.form ['anything']
outcome = long_load(query)
return render_template(done.html,display = outcome)
if __name__ =='__main__':
#app.debug = True
app.run()
摘自index.html:
<身体GT;
< h3>输入任何内容:< / h3>
< p>
< input type =textname =anythingplaceholder =在此输入任何东西>
< input type =submitname =anything_submitvalue =Submit>
< / form>
< / p>
< / body>
摘自done.html:
<身体GT;
< h3>结果:< / h3>
< p>
{{display}}
< / p>
< / body>
任何帮助将不胜感激,我希望这个例子可以帮助。
这可以通过使用包含加载gif图像的
div
来完成。当提交按钮被点击时,div显示使用JavaScript。 要实现这一点,你可以看看这个网站: http://www.netavatar.co.in/2011/05/31/how-to-show-a-loading- gif-image-while-a-page-loads-using-javascript-and-css /
I’m still relatively new to Flask, and a bit of a web noob in general, but I’ve had some good results so far. Right now I’ve got a form in which users enter a query, which is given to a function that can take anywhere between 5 and 30 seconds to return a result (looking up data with the Freebase API).
The problem is that I can’t let the user know that their query is loading during this time, as the results page only loads once the function finishes its work. Is there a way I can display a loading message while that's going on? I found some Javascript that could display a loading message while page elements are still loading, but my waiting period happens before ‘render_template’.
I knocked together some example code, just to demonstrate my situation:
Python:
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('/')
def home():
return render_template("index.html")
@app.route('/', methods=['POST'])
def form(display=None):
query = request.form['anything']
outcome = long_load(query)
return render_template("done.html", display=outcome)
if __name__ == '__main__':
#app.debug = True
app.run()
Excerpt from index.html:
<body>
<h3>Type anything:</h3>
<p>
<form action="." method="POST">
<input type="text" name="anything" placeholder="Type anything here">
<input type="submit" name="anything_submit" value="Submit">
</form>
</p>
</body>
Excerpt from done.html:
<body>
<h3>Results:</h3>
<p>
{{ display }}
</p>
</body>
Any help would be greatly appreciated, I hope this example helps.
This can be done by using a div
that contains a 'loading gif' image. When the submit button is clicked, the div is displayed using javascript.
To implement this, you can take a look at this website: http://www.netavatar.co.in/2011/05/31/how-to-show-a-loading-gif-image-while-a-page-loads-using-javascript-and-css/
这篇关于在Flask中执行耗时的功能时显示“加载”消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!