python - 如何使用 flask 创建进度条?

标签 python flask progress-bar

<分区>

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

应用.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 的 Bootstrap 进度条在我的代码中

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>

有什么帮助吗?

最佳答案

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

关于python - 如何使用 flask 创建进度条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37531829/

相关文章:

python - 在 Python 3 中将 Unicode 序列转换为字符串

python - 提取字典键并将其分配给python中的变量

python - 类型错误 : object supporting the buffer API required in flask api

python - 将 sqlalchemy-continuum 与 flask-sqlalchemy 和 flask-migrate 一起使用

Silverlight MediaElement 进度

python - 如何将 django rest 框架响应传递给 html?

python - 第二次尝试读取打开的文件时没有数据

javascript - CSS 不适用于 Flask

javascript - Response.write(脚本) 不起作用。

c# XAML ProgressBar 正确设置渐变填充