Skip to content

Instantly share code, notes, and snippets.

@simonthompson99
Last active January 13, 2021 12:44
Show Gist options
  • Save simonthompson99/bb0785529ca9fb4e4f48467c6359a324 to your computer and use it in GitHub Desktop.
Save simonthompson99/bb0785529ca9fb4e4f48467c6359a324 to your computer and use it in GitHub Desktop.
[Iterator Progress Bar] Print a progress bar whilst processing an iterable #python
def progress_bar(iterable, prefix='Progress', suffix='Complete', decimals=1,
length=50, fill='=', printEnd="\r"):
"""
shows a terminal progreess bar as iterations are passed through, use as
for i in progress_bar(range(1000)):
pass
:params: iteration: current iteration
:params: total: total iterations
:params: prefix: prefix string
:params: suffix: suffix string
:params: decimals: positive number of decimals in percent complete
:params: length: character length of bar
:params: fill: bar fill character
:params: print_end: end character (e.g. "\r", "\r\n")
"""
total = len(iterable)
# progress bar printing function
def print_progress_bar(iteration):
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '>' + '-' * (length - filledLength)
print(f'\r{prefix} |{bar}| {percent}% {suffix}', end = printEnd)
# initial call
print_progress_bar(0)
# update progress bar
for i, item in enumerate(iterable):
yield item
print_progress_bar(i + 1)
# print new line on complete
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment