In [1]:
%load_ext watermark
%watermark -v -d -u


Last updated: 07/19/2015 

CPython 3.4.3
IPython 3.2.0

PyPrind demo


I would be happy to hear your comments and suggestions.
Please feel free to drop me a note via twitter, email, or google+.


Sections




In [2]:
import pyprind



Basic Progress Bar


In [3]:
n = 150000

bar = pyprind.ProgBar(n)
for i in range(n):
    # do some computation
    bar.update()


0%                          100%
[##############################] | ETA[sec]: 0.000 
Total time elapsed: 1.645 sec



Basic Percentage Indicator

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.


In [4]:
n = 1500

perc = pyprind.ProgPercent(n)
for i in range(n):
    # do some computation
    perc.update()


[100 %] elapsed[sec]: 1.410 | ETA[sec]: 0.000 
Total time elapsed: 1.410 sec



Progress Bar and Percentage Indicator generators

Alternatively, you can use the progress bar and percentage indicators as generators.


In [5]:
for i in pyprind.prog_bar(range(n)):
    # do something
    pass


0%                          100%
[##############################] | ETA[sec]: 0.000 
Total time elapsed: 0.115 sec

In [7]:
for i in pyprind.prog_percent(range(n)):
    # do something
    pass


[100 %] elapsed[sec]: 1.418 | ETA[sec]: 0.000 
Total time elapsed: 1.418 sec



Progress Bar/Percentage Indicator - Reporting tracking information

Simply print() the tracking object after the tracking has completed.


In [4]:
n = 150000

bar = pyprind.ProgBar(n)
for i in range(n):
    # do some computation
    bar.update()
print(bar)


0%                          100%
[##############################] | ETA[sec]: 0.000 
Total time elapsed: 1.614 sec
Title: 
  Started: 01/21/2015 23:21:28
  Finished: 01/21/2015 23:21:29
  Total time elapsed: 1.614 sec

In [5]:
n = 150000

bar = pyprind.ProgBar(n, monitor=True, title='Job_1')
for i in range(n):
    # do some computation
    bar.update()
    
# print report for future reference
print(bar)


Job_1
0%                          100%
[##############################] | ETA[sec]: 0.000 
Total time elapsed: 2.016 sec
Title: Job_1
  Started: 01/21/2015 23:21:30
  Finished: 01/21/2015 23:21:32
  Total time elapsed: 2.016 sec
  CPU %: 56.100000
  Memory %: 0.333166



Progress Bar/Percentage Indicator - Reporting CPU and memory usage

monitor (bool): default False. Monitors CPU and memory usage if True (requires the psutil package).


In [6]:
n = 150000

bar = pyprind.ProgBar(n, monitor=True)
for i in range(n):
    # do some computation
    bar.update()
print(bar)


0%                          100%
[##############################] | ETA[sec]: 0.000 
Total time elapsed: 1.688 sec
Title: 
  Started: 01/21/2015 23:21:34
  Finished: 01/21/2015 23:21:36
  Total time elapsed: 1.688 sec
  CPU %: 57.800000
  Memory %: 0.333261


In [7]:
n = 1500

perc = pyprind.ProgPercent(n, monitor=True)
for i in range(n):
    # do some computation
    perc.update()
print(perc)


[100 %] elapsed[sec]: 1.255 | ETA[sec]: 0.000 
Total time elapsed: 1.255 sec
Title: 
  Started: 01/21/2015 23:21:37
  Finished: 01/21/2015 23:21:38
  Total time elapsed: 1.255 sec
  CPU %: 48.600000
  Memory %: 0.333738



Progress Bar/Percentage Indicator - Setting a title

title (str): default ''. A title for the progress bar


In [8]:
n = 150000

bar = pyprind.ProgBar(n, title='My 1st Progress Bar')
for i in range(n):
    # do some computation
    bar.update()


My 1st Progress Bar
0%                          100%
[##############################] | ETA[sec]: 0.000 
Total time elapsed: 1.929 sec

In [9]:
n = 1500

perc = pyprind.ProgPercent(n, title='My 1st Percent Tracker')
for i in range(n):
    # do some computation
    perc.update()


My 1st Percent Tracker
[100 %] elapsed[sec]: 1.211 | ETA[sec]: 0.000 
Total time elapsed: 1.211 sec



Progress Bar - Changing the default width

width (int): default 30. Sets the progress bar width in characters.


In [10]:
n = 150000

bar = pyprind.ProgBar(n, width=10)
for i in range(n):
    # do some computation
    bar.update()

bar = pyprind.ProgBar(n, width=70)
for i in range(n):
    # do some computation
    bar.update()


0%      100%
[##########] | ETA[sec]: 0.000 
Total time elapsed: 1.189 sec
0%                                                                  100%
[######################################################################] | ETA[sec]: 0.000 
Total time elapsed: 2.085 sec



Progress Bar/Percentage Indicator - Changing the output stream

stream (int): default 2. Takes 1 for stdout, 2 for stderr, or given stream object


In [11]:
n = 150000

bar = pyprind.ProgBar(n, stream=1)
for i in range(n):
    # do some computation
    bar.update()


0%                          100%
[##############################] | ETA[sec]: 0.000 
Total time elapsed: 1.789 sec

In [12]:
bar = pyprind.ProgBar(n, stream=2)
for i in range(n):
    # do some computation
    bar.update()


0%                          100%
[##############################] | ETA[sec]: 0.000 
Total time elapsed: 1.604 sec

In [13]:
import sys

bar = pyprind.ProgBar(n, stream=sys.stdout)
for i in range(n):
    # do some computation
    bar.update()


0%                          100%
[##############################] | ETA[sec]: 0.000 
Total time elapsed: 1.729 sec



Stopping the Progress Bar/Percentage Indicator early

The tracking object can be stopped early via the .stop() method:


In [14]:
n = 150000

bar = pyprind.ProgBar(n)
for i in range(n):
    # do some computation
    if i == 10000:
        bar.stop()
    bar.update()


0%                          100%
[##############################] | ETA[sec]: 0.000 
Total time elapsed: 0.184 sec



Progress Bar/Percentage Indicator - Printing currently processed items

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.


In [15]:
import time

items = ['file_%s.csv' %i for i in range(1,21)]

bar = pyprind.ProgBar(len(items))
for i in items:
    time.sleep(0.5) # do some computation
    bar.update(item_id = i)


0%                100%
[####################] | ETA[sec]: 0.000 | Item ID: file_20.csv
Total time elapsed: 10.104 sec



Choosing your own progress bar style


In [6]:
import sys
sys.path = ['/Users/sebastian/github/pyprind/'] + sys.path
import pyprind
import time

In [9]:
n = 100
bar = pyprind.ProgBar(n, bar_char='█')
for i in range(n):
    time.sleep(0.1) # do some computation
    bar.update()


0%                          100%
[██████████████████████████████] | ETA[sec]: 0.000 
Total time elapsed: 10.346 sec

In [ ]: