[Python Patterns] Adding a nice progress bar to your command line scripts

Every now and then I write a dumb little script that takes some time to complete. The progress bar module adds a little flair to the output of the script.

Every now and then I write a dumb little script that takes some time to complete.  The progress bar module adds a little flair to the output of the script.

Remember to install the module from PyPi before you fire up this code.

pip install progress

Insert joke about wishing I could make the world progress by using Pip.

Code:

from progress.bar import FillingSquaresBar

import time

bar = FillingSquaresBar('Processing', max=20)

for i in range(20):
    # Do something useful!!
    time.sleep(1)
    bar.next()

bar.finish()

Output:

>>> from progress.bar import FillingSquaresBar
>>> 
>>> import time
>>> 
>>> bar = FillingSquaresBar('Processing', max=20)
Processing>>> 
>>> for i in range(20):
...     # Do something useful!!
...     time.sleep(1)
...     bar.next()
... 
bar.finish()

Processing ▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣▣ 100%

There are a few different types of progress bars available to you.  Check out the docs for more.  I love the random one.

Progress Docs on PyPi:
https://pypi.org/project/progress/


My blog posts tagged with "Python Patterns" are designed to be a quick look reference for some Python code snippets I use a lot.  They are written to be a quick starting point for future projects so I do not need to type as much.