Skip to content

Instantly share code, notes, and snippets.

@lilgallon
Forked from aubricus/License
Last active August 7, 2019 19:27
Show Gist options
  • Save lilgallon/0f6a96373d3d65b70cacef2216488ac0 to your computer and use it in GitHub Desktop.
Save lilgallon/0f6a96373d3d65b70cacef2216488ac0 to your computer and use it in GitHub Desktop.
Python Progress Bar
import sys
# Print iterations progress
def print_progress(iteration, total,
prefix='', suffix='', decimals=1, bar_length=100):
"""
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: >0 number of decimals in percent complete (Int)
bar_length - Optional: character length of bar (Int)
"""
percents = f'{100 * (iteration / float(total)):.2f}'
filled_length = int(round(bar_length * iteration / float(total)))
bar = f'{"█" * filled_length}{"-" * (bar_length - filled_length)}'
sys.stdout.write(f'\r{prefix} |{bar}| {percents}% {suffix}'),
if iteration == total:
sys.stdout.write('\n')
sys.stdout.flush()
@lilgallon
Copy link
Author

Changes from fork:

  • Updated code for python 3
  • Fixed PEP8 issues
  • Added import sys at the top

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment