基本上我想在发生耗时的过程时显示加载页面,然后重定向到复杂的其他页面。
虽然并非不可能实现,但我建议使用 javascript 来完成此任务。
这是一个小例子。首先让我们编写一个非常简单的 Flask 服务器,它有一个非常慢的端点。
from flask import Flask, render_template, jsonify
app = Flask(__name__)
@app.route("/")
def hello():
return render_template('redirect.html')
@app.route("/done")
def done():
return "Done!"
@app.route("/slow")
def slow():
import time
time.sleep(5)
return jsonify("oh so slow")
if __name__ == "__main__":
app.run()
Run Code Online (Sandbox Code Playgroud)
现在,我们可以通过从 javascript 调用端点来打造优美的用户体验。templates/redirect.html
像往常一样保存它。
<html>
<head>
<script>
function navigate() {
window.location.href = 'done'; // redirect when done!
}
fetch('slow').then(navigate); // load the slow url then navigate
</script>
</head>
<body>
Loading... <!-- Display a fancy loading screen -->
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3219 次 |
最近记录: |