簡體   English   中英

Python進度條未正確顯示進度

[英]Python progressbar is not showing progress correctly

我在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()

進度欄在視覺上按預期方式工作:它在進度欄中顯示進度以及進度條后面的百分比。 但是,進度欄的計算不正確。

它首先以增加的百分比顯示條,但是當條為100%時,程序開始運行。 我在代碼中放置了幾個“打印”定義,以查看當時的代碼位置,但是現在我首先看到百分比條增加到100%,然后是代碼中的第一個“打印”。 打印之后,代碼開始計算。

有人知道我在這里做錯了嗎?

問候,Ganesh

編輯:這看起來像這樣 這個

因此,訓練部分在標准為100%時開始。

Etene,我有這里的代碼。 我刪除了進度條中我嘗試過的東西,以便也許您可以嘗試一些。

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)

這比我的小,但結構相同。 我希望進度條在導入模數后立即開始,並在最后一行(一切完成后)結束。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM