【问题标题】:How to create a progress bar using flask? [duplicate]如何使用烧瓶创建进度条? [复制]
【发布时间】:2019-06-14 15:56:27
【问题描述】:

只想在我的 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>

有什么帮助吗?

【问题讨论】:

  • 您需要在单独的进程或后台线程中实际执行一些需要时间的操作,并让它在某处存储任务的进度,以便/progress 可以获取它。你展示了什么进展?

标签: python flask progress-bar


【解决方案1】:

这很简单:轮询您的 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
     });
}

【讨论】:

  • 您能否提供一个工作示例?我面临同样的问题,但我无法实现这个解决方案,因为我对 jquery 和烧瓶相当陌生
猜你喜欢
  • 1970-01-01
  • 2019-03-28
  • 1970-01-01
  • 2019-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-12
  • 1970-01-01
相关资源
最近更新 更多