hosamshahin
1/21/2017 - 4:58 PM

python: Print iterations progress

# -*- coding: utf-8 -*-
from time import sleep
import sys

# Print iterations progress
def print_progress(iteration, total, prefix='', suffix='', decimals=1, bar_length=100):
    """
    http://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console
    https://gist.github.com/aubricus/f91fb55dc6ba5557fbab06119420dd6a
    Call in a loop to create terminal progress bar
    @params:
        iteration   - Required  : current iteration (Int)
        total       - Required  : total iterations (Int)
        prefix      - Optional  : prefix string (Str)
        suffix      - Optional  : suffix string (Str)
        decimals    - Optional  : positive number of decimals in percent complete (Int)
        bar_length  - Optional  : character length of bar (Int)
    """
    str_format = "{0:." + str(decimals) + "f}"
    percents = str_format.format(100 * (iteration / float(total)))
    filled_length = int(round(bar_length * iteration / float(total)))
    bar = '█' * filled_length + '-' * (bar_length - filled_length)

    sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)),

    if iteration == total:
        sys.stdout.write('\n')
    sys.stdout.flush()

if __name__=="__main__":
    # make a list
    items = list(range(0, 57))
    i = 0
    l = len(items)

    # Initial call to print 0% progress
    print_progress(i, l, prefix = 'Progress:', suffix = 'Complete', bar_length = 50)
    for item in items:
      # Do stuff...
        sleep(0.1)
        # Update Progress Bar
        i += 1
        print_progress(i, l, prefix = 'Progress:', suffix = 'Complete', bar_length = 50)

    # # Sample Output
    # Progress: |█████████████████████████████████████████████-----| 90.0% Complete