%load_ext watermark
%watermark -v -d -u -p pyprind
last updated: 2017-03-01 CPython 3.6.0 IPython 5.3.0 pyprind 2.10.0
import pyprind
Using a for-loop
import time
n = 100
timesleep = 0.05
bar = pyprind.ProgBar(n)
for i in range(n):
time.sleep(timesleep) # your computation here
bar.update()
0% 100% [##############################] | ETA: 00:00:00 Total time elapsed: 00:00:05
In a while-loop
import random
random.seed(1)
collection = set()
n = 100
bar = pyprind.ProgBar(n, track_time=False, title='while example')
while len(collection) < n:
r = random.randint(0, 10**5)
if r % 7 and r not in collection:
collection.add(r)
bar.update()
time.sleep(timesleep)
print(bar)
while example 0% 100% [##############################]
Title: while example Started: 03/01/2017 01:10:02 Finished: 03/01/2017 01:10:07 Total time elapsed: 00:00:05
Note: the Percentage indicator is significantly slower due to background computation.
Thus, it is recommended for tasks with less iterations but longer computational time per iteration.
perc = pyprind.ProgPercent(n)
for i in range(n):
time.sleep(timesleep) # your computation here
perc.update()
[100 %] Time elapsed: 00:00:05 | ETA: 00:00:00 Total time elapsed: 00:00:05
Alternatively, you can use the progress bar and percentage indicators as generators.
for i in pyprind.prog_bar(range(n)):
time.sleep(timesleep) # your computation here
0% 100% [##############################] | ETA: 00:00:00 Total time elapsed: 00:00:05
for i in pyprind.prog_percent(range(n)):
time.sleep(timesleep) # your computation here
[100 %] Time elapsed: 00:00:05 | ETA: 00:00:00 Total time elapsed: 00:00:05
Simply print()
the tracking object after the tracking has completed.
bar = pyprind.ProgBar(n)
for i in range(n):
time.sleep(timesleep) # your computation here
bar.update()
print(bar)
0% 100% [##############################] | ETA: 00:00:00 Total time elapsed: 00:00:05
Title: Started: 03/01/2017 01:10:54 Finished: 03/01/2017 01:10:59 Total time elapsed: 00:00:05
bar = pyprind.ProgBar(n, monitor=True, title='Job_1')
for i in range(n):
time.sleep(timesleep) # your computation here
bar.update()
# print report for future reference
print(bar)
Job_1 0% 100% [##############################] | ETA: 00:00:00 Total time elapsed: 00:00:05
Title: Job_1 Started: 09/07/2016 21:33:03 Finished: 09/07/2016 21:33:08 Total time elapsed: 00:00:05 CPU %: 2.10 Memory %: 0.48
monitor
(bool
): default False. Monitors CPU and memory usage if True
(requires the psutil
package).
bar = pyprind.ProgBar(n, monitor=True)
for i in range(n):
time.sleep(timesleep) # your computation here
bar.update()
print(bar)
0% 100% [##############################] | ETA: 00:00:00 Total time elapsed: 00:00:05
Title: Started: 09/07/2016 21:33:08 Finished: 09/07/2016 21:33:13 Total time elapsed: 00:00:05 CPU %: 1.90 Memory %: 0.48
perc = pyprind.ProgPercent(n, monitor=True)
for i in range(n):
time.sleep(timesleep) # your computation here
perc.update()
print(perc)
[100 %] Time elapsed: 00:00:05 | ETA: 00:00:00 Total time elapsed: 00:00:05
Title: Started: 09/07/2016 21:33:13 Finished: 09/07/2016 21:33:19 Total time elapsed: 00:00:05 CPU %: 2.90 Memory %: 0.48
title
(str
): default ''. A title for the progress bar
bar = pyprind.ProgBar(n, title='My 1st Progress Bar')
for i in range(n):
time.sleep(timesleep) # your computation here
bar.update()
My 1st Progress Bar 0% 100% [##############################] | ETA: 00:00:00 Total time elapsed: 00:00:05
perc = pyprind.ProgPercent(n, title='My 1st Percent Tracker')
for i in range(n):
time.sleep(timesleep) # your computation here
perc.update()
My 1st Percent Tracker [100 %] Time elapsed: 00:00:05 | ETA: 00:00:00 Total time elapsed: 00:00:05
width
(int
): default 30. Sets the progress bar width in characters.
bar = pyprind.ProgBar(n, width=10)
for i in range(n):
time.sleep(timesleep) # your computation here
bar.update()
0% 100% [##########] | ETA: 00:00:00 Total time elapsed: 00:00:05
bar = pyprind.ProgBar(n, width=70)
for i in range(n):
time.sleep(timesleep) # your computation here
bar.update()
0% 100% [######################################################################] | ETA: 00:00:00 Total time elapsed: 00:00:05
stream
(int
): default 2. Takes 1 for stdout, 2 for stderr, or given stream object
bar = pyprind.ProgBar(n, stream=1)
for i in range(n):
time.sleep(timesleep) # your computation here
bar.update()
0% 100% [##############################] | ETA: 00:00:00 Total time elapsed: 00:00:05
bar = pyprind.ProgBar(n, stream=2)
for i in range(n):
time.sleep(timesleep) # your computation here
bar.update()
0% 100% [##############################] | ETA: 00:00:00 Total time elapsed: 00:00:05
import sys
bar = pyprind.ProgBar(n, stream=sys.stdout)
for i in range(n):
time.sleep(timesleep) # your computation here
bar.update()
The tracking object can be stopped early via the .stop()
method:
bar = pyprind.ProgBar(n)
for i in range(n):
time.sleep(timesleep) # your computation here
if i == 5:
bar.stop()
break
bar.update()
0% 100% [##############################] | ETA: 00:00:00 Total time elapsed: 00:00:00
Sometimes it is useful to print out the name of currently processed items, e.g., files are being processed. This can be done by providing a custom string for the optional item_id
parameter of the .update()
method.
items = ['file_%s.csv' %i for i in range(1,21)]
bar = pyprind.ProgBar(len(items))
for i in items:
time.sleep(timesleep) # your computation here
bar.update(item_id = i)
0% 100% [####################] | ETA: 00:00:00 | Item ID: file_20.csv Total time elapsed: 00:00:01
bar = pyprind.ProgBar(n, bar_char='█')
for i in range(n):
time.sleep(0.1) # do some computation
bar.update()
0% 100% [██████████████████████████████] | ETA: 00:00:00 Total time elapsed: 00:00:10
Update the progress in after iteration of the loop via the force_flush
parameter.
n = 100
bar = pyprind.ProgBar(n, bar_char='█')
for i in range(n):
time.sleep(0.5) # do some computation
bar.update(force_flush=True)
0% 100% [██████████████████████████████] | ETA: 00:00:00 Total time elapsed: 00:00:50
Update the progress every 4 seconds using the update_interval
parameter.
n = 100
bar = pyprind.ProgBar(n, bar_char='█', update_interval=4)
for i in range(n):
time.sleep(0.2) # do some computation
bar.update()
0% 100% [██████████████████████████████] | ETA: 00:00:00 Total time elapsed: 00:00:20
n = 100
bar = pyprind.ProgPercent(n, update_interval=4)
for i in range(n):
time.sleep(0.2) # do some computation
bar.update()
[100 %] Time elapsed: 00:00:20 | ETA: 00:00:00 Total time elapsed: 00:00:20