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

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

问题描述

我只想在任务运行时显示一个 静态 模板,上面写着请稍候...".这是路由功能的简化版本:

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'

它不会让我在不返回的情况下使用 render_template,这会跳过我需要完成的处理.我需要它来显示进度屏幕 同时 进程运行然后在完成后显示成功".我该怎么做?

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?

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

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

在 html 之后,我在模板中有这个:

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>

它可以很好地替换内容以在提交时显示进度页面,但现在表单未提交用于服务器端处理.我尝试在表单属性中设置 data-ajax="false" 但这并没有解决问题.我还尝试在清除内容之前简单地使用 this.submit(); 但这使它等待服务器端处理,从而击败了进度/等待屏幕的要点.如果我使用 Javascript,我仍然需要为服务器端处理提交的表单数据.

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.

推荐答案

您可以流式传输响应以获得非常的进度报告.有关更多信息,请参阅有关流媒体的文档.此示例在等待 5 秒时输出完成百分比.您将处理 csv 或任何您需要做的事情,而不是睡觉.

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
'

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

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

        yield 'done
'

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

app.run()

这会在 5 秒内输出以下内容:

This outputs the following over 5 seconds:

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

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

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.

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

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