Advertisement
Koepnick

Simple Progress Bar

Dec 12th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. def print_progress(iteration, total, prefix='', suffix='', decimals=1, bar_length=100):
  2.     """
  3.    Call in a loop to create terminal progress bar
  4.    @params:
  5.        iteration   - Required  : current iteration (Int)
  6.        total       - Required  : total iterations (Int)
  7.        prefix      - Optional  : prefix string (Str)
  8.        suffix      - Optional  : suffix string (Str)
  9.        decimals    - Optional  : positive number of decimals in percent complete (Int)
  10.        bar_length  - Optional  : character length of bar (Int)
  11.    """
  12.     str_format = "{0:." + str(decimals) + "f}"
  13.     percents = str_format.format(100 * (iteration / float(total)))
  14.     filled_length = int(round(bar_length * iteration / float(total)))
  15.     bar = '=' * filled_length + '-' * (bar_length - filled_length)
  16.     sys.stdout.write('\r%s |%s| %s%s %s' % (prefix, bar, percents, '%', suffix)),
  17.     if iteration == total:
  18.         sys.stdout.write('\n')
  19.     sys.stdout.flush()
  20.  
  21. ## Instructions
  22. ### Initialize
  23. print_progress(0, 100, prefix = 'Progress:', suffix = 'Complete', bar_length = 50)
  24.  
  25. ### Update in a loop
  26. print_progress(i, 100, prefix = 'Progress:', suffix = 'Complete', bar_length = 50)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement