Python Programming/Progress Bars

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Progress bars in Python are abundant. There are several well-known packages for progress bars out there.

External Progress Bars[edit]

Some of the top bars are from the progress and the tqdm library.

Progress[edit]

progress
Easy to use progress bars
PyPi Link https://pypi.python.org/pypi/progress
Pip command pip install progress
Import command import progress.bar or import progress.spinner

The progress module provides 7 different progress bars to use. They are:

  • Bar
  • ChargingBar
  • FillingSquaresBar
  • FillingCirclesBar
  • IncrementalBar
  • PixelBar
  • ShadyBar

The module also provides 6 spinners. These do not have a finish. They are:

  • Spinner
  • PieSpinner
  • MoonSpinner
  • LineSpinner
  • PixelSpinner

In order to access a bar/spinner, you run progress.a.b, where a is the type of indicator you want (bar or spinner) and b is the name of the bar/spinner. Furthermore, you have to import the module as progress.a or else it cannot see the bar/spinner.

Example code[edit]

import progress.bar
import time

b = progress.bar.Bar('Waiting', max = 100)

for i in range(100):
    b.next()
    time.sleep(0.1)
    
b.finish()

We can also make it an iterator so we can put it into a for loop.

from __future__ import print_function
import progress.bar
import time

b = progress.bar.Bar('Printing', max = 26)
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']

for letter in b.iter(letters):
    print(letter)
    time.sleep(0.1)
    
b.finish()

Tqdm[edit]

tqdm
Fast, Extensible Progress Meter
PyPi Link https://pypi.python.org/pypi/tqdm
Pip command pip install tqdm

The tqdm module relies off of one main class, the tqdm class. It has a GUI version, and a jupyter notebook-compatible version, and one for pandas. It will run as an iterator, a callable bar, and an action in a with statement.

Basic Code[edit]

from tqdm import tqdm

adder = 1
sum = 0

for i in tqdm(range(100)):
    sum += adder