简体   繁体   English

如何使用烧瓶创建进度条?

[英]How to create a progress bar using flask?

Just want to insert a progress bar in my html page.只想在我的 html 页面中插入一个进度条。 It should load from a for in my app.py.它应该从我的 app.py 中的 for 加载。 That's what I did so far...这就是我到目前为止所做的......

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)

I'm using a bootstrap progress-bar from w3schools in my code我在我的代码中使用w3schools的引导进度条

index.html索引.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>

Any help, please?请问有什么帮助吗?

this is pretty simple: poll your api and update the progress bar width and valuenow until finished: 这非常简单:轮询你的api并更新进度条宽度和valuenow直到完成:

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
     });
}

I use the following code and it full fills my requirement. 我使用以下代码,它满足我的要求。 Here is the complete code. 这是完整的代码。

app.py 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 的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>

Here is the screenshot 这是截图

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM