简体   繁体   English

Python进度条未正确显示进度

[英]Python progressbar is not showing progress correctly

I have a progressbar in Python: 我在Python中有一个进度栏:

import os
def file_len(fname):
    with open(fname) as f:
        for i, l in enumerate(f):
            pass
    return i + 1

import progressbar
from time import sleep
bar = progressbar.ProgressBar(maxval=file_len(os.path.basename(__file__)), \
    widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
bar.start()
for i in range(file_len(os.path.basename(__file__))):
    bar.update(i+1)
    sleep(0.1)
bar.finish()

The progressbar works as expected visually: it displays the progress in a bar and the percentage behind it. 进度栏在视觉上按预期方式工作:它在进度栏中显示进度以及进度条后面的百分比。 However, the calculation of the progressbar is incorrect. 但是,进度栏的计算不正确。

It first shows the bar with an increasing percentage, but when the bar is 100%, the program starts to run. 它首先以增加的百分比显示条,但是当条为100%时,程序开始运行。 I have put several 'print' definitions in my code to see where the code is at that moment, but now I first see the percentage bar increasing to 100%, followed by the first 'print' in the code. 我在代码中放置了几个“打印”定义,以查看当时的代码位置,但是现在我首先看到百分比条增加到100%,然后是代码中的第一个“打印”。 After this print, the code starts its calculations. 打印之后,代码开始计算。

Does someone know what I did wrong here? 有人知道我在这里做错了吗?

Regards, Ganesh 问候,Ganesh

EDIT: This looks like this 编辑:这看起来像这样 这个

So the training part starts when the bar is 100%. 因此,训练部分在标准为100%时开始。

Etene, I have the code which I have here. Etene,我有这里的代码。 I removed the progress bar things I tried, so that maybe you can try something. 我删除了进度条中我尝试过的东西,以便也许您可以尝试一些。

import os
import numpy as np
from sklearn import cross_validation
import matplotlib.pyplot as plt
import seaborn as sb
import pylab as pl
from termcolor import colored
from sklearn import datasets

""" ===================================== Create New Folders ===================================== """
def create_folder(path):
    if not os.path.exists(path):
        os.makedirs(path) 
    return path

current_dir = os.getcwd()

path_graph = create_folder(current_dir + "\\Graphs")

""" ==================================== Function Information ==================================== """

def graph_heatmap(Plot, Filename, Annot, Mask_half):

    print (colored('---- Creating Heatmap: ' + Filename, 'blue'))

    plt.gcf().clear()

    plt.subplots(figsize=(40,40))
    mask = np.zeros_like(Plot)
    mask[np.triu_indices_from(mask)] = Mask_half
    sb.heatmap(Plot, annot=Annot, mask=mask, cmap="Blues", linewidths=0.5, linecolor='black', vmin=0, vmax=1)

    pl.savefig(path_graph + '\\' + Filename + '.png', bbox_inches='tight', dpi=600)
    plt.close()

""" ================================= Importing & Filtering Files ================================ """
diabetes = datasets.load_diabetes()


""" ======================================== Data Mining ======================================== """
#Training
print (colored('---- Training', 'blue'))

X = diabetes.data[:, np.newaxis, 2]
y = diabetes.target

X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size = 0.3, random_state=25)

This one is smaller than the one I have, but the structure is the same. 这比我的小,但结构相同。 I want that the progress bar starts directly after importing the modulus and ends at the last line (when everything is done). 我希望进度条在导入模数后立即开始,并在最后一行(一切完成后)结束。

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

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