【问题标题】:Updating progress bar with Flask使用 Flask 更新进度条
【发布时间】:2019-07-10 11:40:45
【问题描述】:

我正在尝试更新进度条 - 我尝试在下面的代码周围实现代码 here,但无法使其正常工作。当我运行我的代码时,进度条会出现但不会更新。

我还检查了其他答案,但仍然无法弄清楚如何让它发挥作用。如果有人可以帮助解决这个问题,我将不胜感激。

server.py

def create_app():
    app = Flask(__name__)

     app.add_url_rule("/", view_func=views.home_page)

    app.add_url_rule("/display", view_func=views.display_page)
    app.add_url_rule("/display_progress", methods=['GET', 'POST'], 
view_func=views.progress) 

return app

views.py

def display_page():      
    return send_file("templates/display.html")


def progress():
    def generate():
        x = 0
        while x <= 100:
            print(x)
            x = x + 10
            time.sleep(0.2)
            yield "data:" + str(x) +"\n\n"
     return Response(generate(), mimetype='text/event-stream')

display.html

<html lang="en">
<head>
    <title>Display Update</title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="/static/bootstrap.css"></link>
    <script src="/static/jquery.js"></script>
    <script src="/static/bootstrap.js"></script>
    <script src="/static/websocket.js"></script>

    <script>
    var source = new EventSource("/display_progress");
    source.onmessage = function(event) {
        $('.progress-bar').css('width', event.data + '%').attr('aria- 
   valuenow', event.data);
    }
    </script>

</head>

<body>
        <h3>Update DTS Displays</h3>
        <div class="progress" style="width: 750px; height: 22px; margin: 
10px;">
            <div class="progress-bar progress-bar-striped active" 
role="progressbar" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100" 
style="width: 0%">
                <span class="progress-bar-label">0%</span>
            </div>

        </div>
        <button type="submit" class="btn btn-default" formaction="{{ 
url_for('progress') }}">Progress Bar</button>

</body>
</html>

【问题讨论】:

    标签: python-3.x flask progress-bar


    【解决方案1】:

    display.html 中更新你的脚本标签:

    <script>
            var source = new EventSource("/display_progress");
            source.onmessage = function (event) {
                $('.progress-bar').css('width', event.data + '%').attr('aria-valuenow',event.data);
                $('.progress-bar-label').text(event.data + '%');
                if (event.data == 100) {
                    source.close()
                }
        }
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-29
      • 2013-03-08
      • 1970-01-01
      • 2020-11-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多