简体   繁体   中英

Python progressbar is not showing progress correctly

I have a progressbar in 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. 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. After this print, the code starts its calculations.

Does someone know what I did wrong here?

Regards, Ganesh

EDIT: This looks like this 这个

So the training part starts when the bar is 100%.

Etene, I have the code which I have here. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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