如何使用flask创建进度条?

时间:2016-05-30 18:33:11

标签: python flask progress-bar

只想在我的html页面中插入进度条。它应该从我的app.py中的for加载。这就是我到目前为止所做的......

app.py

from flask import Flask, render_template
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/progress')
def ajax_index():

   for i in range(500):
      print("%d" % i)
      # I want to load this in a progress bar

if __name__ == "__main__":
    app.run(debug=True)

我在我的代码中使用w3schools的引导进度条

的index.html

<html>
    <head>
       <meta name="viewport" content="width=device-width, initial-scale=1">
       <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
       <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
       <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

       <script>
            $(function () { 
                $("#content").load("/progress"); 
            });
       </script>

    </head>
    <body>
        <div class="container">
           <h2>Progress Bar With Label</h2>
           <div class="progress">
              <div class="progress-bar" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" style="width:0%"></div>
           </div>
        </div>
    </body>
</html>

请帮忙吗?

2 个答案:

答案 0 :(得分:3)

这很简单:轮询你的api并更新进度条宽度和值,直到完成:

var interval = setInterval(update_progress, 1000);
function update_progress() {
     $.get('/progress').done(function(n){
         n = n / 5;  // percent value
         if (n == 100) {
            clearInterval(interval);
            callback(); // user defined
         }
         $('.progress-bar').animate({'width': n +'%'}).attr('aria-valuenow', n);    
     }).fail(function() {
         clearInterval(interval);
         displayerror(); // user defined
     });
}

答案 1 :(得分:0)

我使用以下代码,完全满足了我的要求。 这是完整的代码。

app.py

from flask import Flask, render_template, Response
import time
app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

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

if __name__ == '__main__':
    app.run()

index.html

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

    <script>
        $(document).ready(function () {
            event.preventDefault();
            var source = new EventSource("/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>

</head>
<body>
<h3>Progress Bar With Label</h3>
<div class="progress" style="width: 750px;height: 22px; margin: 10px;">
    <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" aria-valuenow="0"
         aria-valuemin="0" aria-valuemax="100" style="width: 0%">
        <span class="progress-bar-label">0%</span>
    </div>
</div>
</body>
</html>

这是屏幕截图

enter image description here