当Flask中的函数在后台运行时,如何向页面添加加载gif?(How to add a loading gif to the page when a function runs in the back

编程入门 行业动态 更新时间:2024-10-25 13:28:20
当Flask中的函数在后台运行时,如何向页面添加加载gif?(How to add a loading gif to the page when a function runs in the background in Flask?)

我搜索了很长一段时间,但无法找到答案。 我想提一下,我是Web开发的完全初学者,不知道AJAX或jQuery是什么。 所以,让我解决我的问题。

我写了一个Flask应用程序,它显示一个带有按钮的页面,当用户点击该按钮时,会调用一个执行一些处理(通常需要大约2分钟)的Python函数。 处理结束后,将打开一个新页面显示结果。 现在我想包含一个'加载gif',指示用户处理正在进行中。 我该怎么做呢? 这是我的代码看起来像的一个例子。

app.py

from flask import Flask, render_template import time app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') @app.route('/result', methods=['POST']) def result(): time.sleep(5) # indicates the time delay caused due to processing return render_template('result.html') if __name__ == '__main__': app.run(debug=True)

的index.html

<html> <body> <form method=post action="/result"> <input type=submit value='Start Processing' name='submit_btn'> </form> </body> </html>

result.html

<html> <body> <h1> Processing Done </h1> </body> </html>

如果有人能指引我朝正确的方向发展,那将会非常有帮助。

I have written a Flask app that displays a page with a button, when the user clicks on that button a Python function is called which does some processing (which usually takes about 2 minutes). After this processing is over, a new page opens displaying the results. Now I want to include a 'loading gif' that indicates the user that the processing is in progress. How do I do this? Here is an example of what my code looks like.

app.py

from flask import Flask, render_template import time app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') @app.route('/result', methods=['POST']) def result(): time.sleep(5) # indicates the time delay caused due to processing return render_template('result.html') if __name__ == '__main__': app.run(debug=True)

index.html

<html> <body> <form method=post action="/result"> <input type=submit value='Start Processing' name='submit_btn'> </form> </body> </html>

result.html

<html> <body> <h1> Processing Done </h1> </body> </html>

最满意答案

这只需要在客户端完成。

在标题部分添加jquery:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

将其添加到您的提交按钮代码:

onclick="$('#loading').show();"

获取一个load.gif图像在html中的表单后面添加下面的代码

<div id="loading" style="display:none;"><img src="loading.gif" alt="" />Loading!</div>

参考文献: 1 2

This needs to be done on the client side only.

Add jquery in your header section:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>

Add this to your submit button code:

onclick="$('#loading').show();"

Get a loading.gif image Add below code after your form in html

<div id="loading" style="display:none;"><img src="loading.gif" alt="" />Loading!</div>

References: 1 2

更多推荐

本文发布于:2023-07-05 01:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031248.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   后台   加载   页面   gif

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!