烧瓶静态进度屏幕 [英] Flask Static Progress Screen

查看:139
本文介绍了烧瓶静态进度屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选项1:没有Javascript



我只想显示一个名为please wait ...的 static 模板在跑。这里是路由功能的简化版本:

  @ app.route('/ import / towers /< case_id> / < filename>方法= ['GET','POST'])
def import_towers(case_id = None,filename = None):
case_number = TollsCase.get_case_number(case_id)
打开(os.path.join(app.config ['UPLOAD_FOLDER'],filename)'rb')作为f:
csv_f = csv.reader(f)
headers = next(csv_f)
如果flask.request.method =='POST':
#在这里显示静态进度屏幕然后在csv文件中处理记录
在csv_f中的行:
#进程记录
return'success'

它不会让我使用 render_template 没有返回,这将跳过我需要做的处理。我需要它显示进度屏幕而进程运行,然后显示成功完成。






选项2:Javascript



如果无法完成,并且必须使用Javascript ,那么这就是我迄今为止所尝试的:

在html ,我有这样的模板:

 < script src =https://ajax.googleapis.com/ajax/库/ jquery的/ 2.1.3 / jquery.min.js>< /脚本> 
< script type =application / javascript> $('#content')。empty();
$('#content')。html ('请稍等...');
})
< / script>

它可以正常替换内容,以显示提交进度页面,但现在表单不是提交给服务器端处理。我尝试在表单属性中设置 data-ajax =false,但是没有解决问题。在清除内容之前,我也尝试过简单地使用 this.submit(); ,但是这使得它在服务器端处理上等待,从而破坏了进度/等待屏幕。如果我使用Javascript,我仍然需要为服务器端提交的表单数据

可以流式传输一个简单的进度报告来获得一个 。有关更多信息,请参阅流媒体文档。本示例在等待5秒钟的时间内输出完成百分比。

  from flask import烧瓶,回应
导入时间
$ b $ app = Flask(__ name__)

@ app.route('/')
def index():
def generate() :
yield'等待5秒\'

为范围内(1,101):
time.sleep(0.05)

if i%10 == 0:
yield'{}%\\\
'.format(i)

yield'done\\\
'

return响应(generate(),mimetype ='text / plain')

app.run()

输出以下内容超过5秒:

等待5秒
10%
20%
30%
40%
50%
60%
70%
80%
90%
100%
完成

这不是很复杂,但这也只是纯文本。一个更强大的解决方案是使用Celery在后台运行任务,并使用ajax请求轮询它的进度。


Option 1: No Javascript

I just want to display a static template that says 'please wait...' while a task is running. Here is a simplified version of the route function:

@app.route('/import/towers/<case_id>/<filename>', methods=['GET', 'POST'])
def import_towers(case_id=None, filename=None):
    case_number = TollsCase.get_case_number(case_id)
    with open(os.path.join(app.config['UPLOAD_FOLDER'], filename), 'rb') as f:
        csv_f = csv.reader(f)
        headers = next(csv_f)
        if flask.request.method == 'POST':
            # show static progress screen here then process records in csv file
            for row in csv_f:
                # process record
            return 'success'

It won't let me use render_template without returning, which skips the processing I need done. I need it to display a progress screen while the process runs then show 'success' when done. How do I do this?


Option 2: Javascript

If that can't be done and Javascript must be used, then this is what I've tried so far:

After the html, I have this in the template:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="application/javascript">
    $('form').on('submit', function() {
        $('#content').empty();
        $('#content').html('please wait...');
    })
</script>

It works fine for replacing the content to show a progress page on submit, but now the form is not submitted for server-side handling. I tried setting data-ajax="false" in the form attributes but that didn't resolve the issue. I also tried simply using this.submit(); before clearing the content but that makes it wait on the server-side processing, thus defeating the point of a progress/wait screen. I still need the form data submitted for server-side handling if I use Javascript.

解决方案

You can stream a response to get a very simple progress report. See the docs on streaming for more information. This example outputs a percent complete while waiting 5 seconds. Rather than sleeping, you would be processing csv or whatever you need to do.

from flask import Flask, Response
import time

app = Flask(__name__)

@app.route('/')
def index():
    def generate():
        yield 'waiting 5 seconds\n'

        for i in range(1, 101):
            time.sleep(0.05)

            if i % 10 == 0:
                yield '{}%\n'.format(i)

        yield 'done\n'

    return Response(generate(), mimetype='text/plain')

app.run()

This outputs the following over 5 seconds:

waiting 5 seconds
10%
20%
30%
40%
50%
60%
70%
80%
90%
100%
done

This isn't very complex, but it's also just plain text. A much more powerful solution would be to run the task in the background using Celery and poll it's progress using ajax requests.

这篇关于烧瓶静态进度屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆