Reputation: 819
I am trying to implement this package http://code.google.com/p/python-progressbar/ when downloading a file like below:
def dlProgress(count, blockSize, totalSize):
widgets = ['Test: ', Percentage(), ' ', Bar(marker=RotatingMarker()), ' ', ETA(), ' ', FileTransferSpeed()]
pbar = ProgressBar(widgets=widgets, maxval=totalSize).start()
for count in range(totalSize):
#print count
pbar.update(int(count*blockSize*100/totalSize))
pbar.finish()
urllib.urlretrieve(url, fileName, reporthook=dlProgress)
The problem is that the progress bar goes to 100% before the real download progress finished, and it continues to begin the new bar, like this:
Test: 100% |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Time: 0:00:31 402.06 kB/s
Test: 100% |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Time: 0:00:31 408.39 kB/s
Test: 100% |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| Time: 0:00:32 389.47 kB/s
Upvotes: 3
Views: 3416
Reputation: 9578
You should initialize your progress bar outside of the progress callback. Then on the first time its called back, check and set the maxval and start it up:
widgets = ['Test: ', Percentage(), ' ', Bar(marker=RotatingMarker()), ' ', ETA(), ' ', FileTransferSpeed()]
pbar = ProgressBar(widgets=widgets)
def dlProgress(count, blockSize, totalSize):
if pbar.maxval is None:
pbar.maxval = totalSize
pbar.start()
pbar.update(min(count*blockSize, totalSize))
urllib.urlretrieve(url, fileName, reporthook=dlProgress)
pbar.finish()
As a side note, that last block will not be a full block, so you'll want to make sure your not going over the totalSize.
Upvotes: 5