#!/usr/bin/env python # coding: utf-8 # [Sebastian Raschka](http://sebastianraschka.com) # In[1]: get_ipython().run_line_magic('load_ext', 'watermark') get_ipython().run_line_magic('watermark', '-v -d -u -p pyprind') # # PyPrind demo #
# I would be happy to hear your comments and suggestions. # Please feel free to drop me a note via # [twitter](https://twitter.com/rasbt), [email](mailto:bluewoodtree@gmail.com), or [google+](https://plus.google.com/118404394130788869227). #
# # ## Sections # - [Basic Progress Bar](#Basic-Progress-Bar) # - [Basic Percentage Indicator](#Basic-Percentage-Indicator) # - [Progress Bar and Percentage Indicator generators](#Progress-Bar-and-Percentage-Indicator-generators) # - [Progress Bar/Percentage Indicator - Reporting tracking information](#Progress-Bar/Percentage-Indicator---Reporting-CPU-and-memory-usage) # - [Progress Bar/Percentage Indicator - Reporting CPU and memory usage](#Progress-Bar/Percentage-Indicator---Reporting-CPU-and-memory-usage) # - [Progress Bar/Percentage Indicator - Setting a title](#Progress-Bar/Percentage-Indicator---Setting-a-title) # - [Progress Bar - Changing the default width](#Progress-Bar---Changing-the-default-width) # - [Progress Bar/Percentage Indicator - Changing the output stream](#Progress-Bar/Percentage-Indicator---Setting-a-title) # - [Stopping the Progress Bar/Percentage Indicator early](#Stopping-the-Progress-Bar/Percentage-Indicator-early) # - [Choosing your own progress bar style](#Choosing-your-own-progress-bar-style) # - [Controlling the update frequency](#Controlling-the-update-frequency) #
#
# In[2]: import pyprind # #
#
# ## Basic Progress Bar # [[back to section overview](#sections)] # **Using a for-loop** # In[3]: import time n = 100 timesleep = 0.05 bar = pyprind.ProgBar(n) for i in range(n): time.sleep(timesleep) # your computation here bar.update() # **In a while-loop** # In[4]: 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) # #
#
# ## Basic Percentage Indicator # [[back to section overview](#sections)] # *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[5]: perc = pyprind.ProgPercent(n) for i in range(n): time.sleep(timesleep) # your computation here perc.update() # #
#
# ## Progress Bar and Percentage Indicator generators # [[back to section overview](#sections)] # Alternatively, you can use the progress bar and percentage indicators as generators. # In[6]: for i in pyprind.prog_bar(range(n)): time.sleep(timesleep) # your computation here # In[7]: for i in pyprind.prog_percent(range(n)): time.sleep(timesleep) # your computation here # #
#
# ## Progress Bar/Percentage Indicator - Reporting tracking information # [[back to section overview](#sections)] # Simply `print()` the tracking object after the tracking has completed. # In[8]: bar = pyprind.ProgBar(n) for i in range(n): time.sleep(timesleep) # your computation here bar.update() print(bar) # In[9]: 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) # #
#
# ## Progress Bar/Percentage Indicator - Reporting CPU and memory usage # [[back to section overview](#sections)] # `monitor` (`bool`): default False. Monitors CPU and memory usage if `True` # (requires the [`psutil`](https://pypi.python.org/pypi/psutil) package). # In[10]: bar = pyprind.ProgBar(n, monitor=True) for i in range(n): time.sleep(timesleep) # your computation here bar.update() print(bar) # # In[11]: perc = pyprind.ProgPercent(n, monitor=True) for i in range(n): time.sleep(timesleep) # your computation here perc.update() print(perc) # #
#
# ## Progress Bar/Percentage Indicator - Setting a title # [[back to section overview](#sections)] # `title` (`str`): default ''. A title for the progress bar # In[10]: bar = pyprind.ProgBar(n, title='My 1st Progress Bar') for i in range(n): time.sleep(timesleep) # your computation here bar.update() # In[11]: perc = pyprind.ProgPercent(n, title='My 1st Percent Tracker') for i in range(n): time.sleep(timesleep) # your computation here perc.update() # #
#
# ## Progress Bar - Changing the default width # [[back to section overview](#sections)] # `width` (`int`): default 30. Sets the progress bar width in characters. # In[12]: bar = pyprind.ProgBar(n, width=10) for i in range(n): time.sleep(timesleep) # your computation here bar.update() # In[13]: bar = pyprind.ProgBar(n, width=70) for i in range(n): time.sleep(timesleep) # your computation here bar.update() # #
#
# ## Progress Bar/Percentage Indicator - Changing the output stream # [[back to section overview](#sections)] # `stream` (`int`): default 2. Takes 1 for stdout, 2 for stderr, or given stream object # In[14]: bar = pyprind.ProgBar(n, stream=1) for i in range(n): time.sleep(timesleep) # your computation here bar.update() # In[15]: bar = pyprind.ProgBar(n, stream=2) for i in range(n): time.sleep(timesleep) # your computation here bar.update() # In[16]: import sys bar = pyprind.ProgBar(n, stream=sys.stdout) for i in range(n): time.sleep(timesleep) # your computation here bar.update() #
#
# ## Stopping the Progress Bar/Percentage Indicator early # [[back to section overview](#sections)] # The tracking object can be stopped early via the `.stop()` method: # In[19]: bar = pyprind.ProgBar(n) for i in range(n): time.sleep(timesleep) # your computation here if i == 5: bar.stop() break bar.update() #
#
# ## Progress Bar/Percentage Indicator - Printing currently processed items # [[back to section overview](#sections)] # 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[20]: 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) #
#
# ## Choosing your own progress bar style # [[back to section overview](#sections)] # In[21]: bar = pyprind.ProgBar(n, bar_char='█') for i in range(n): time.sleep(0.1) # do some computation bar.update() #
#
# ## Controlling the update frequency # [[back to section overview](#sections)] # Update the progress in after iteration of the loop via the `force_flush` parameter. # In[22]: 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) #
# Update the progress every 4 seconds using the `update_interval` parameter. # In[23]: 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() # In[24]: n = 100 bar = pyprind.ProgPercent(n, update_interval=4) for i in range(n): time.sleep(0.2) # do some computation bar.update()