Skip to content

Instantly share code, notes, and snippets.

@CodeArtha
Created February 9, 2024 11:10
Show Gist options
  • Select an option

  • Save CodeArtha/a232a0701e47eb103f274a86437b9cff to your computer and use it in GitHub Desktop.

Select an option

Save CodeArtha/a232a0701e47eb103f274a86437b9cff to your computer and use it in GitHub Desktop.
python print progressbar
# This function prints a progress bar in the terminal without deleting previous terminal output and without creating a newline
# each time the progress bar is updated. This version works with a fixed width of the progress bar. There is another version
# in [this gist](https://gist.github.com/greenstick/b23e475d2bfdc3a82e34eaa1f6781ee4) that will adapt to the width of the terminal
# though this comes with a large performance penalty. Its ok for one-offs, by the author recommends to use a proper progress
# bar library instead for a more robust solution. [This other gist](https://gist.github.com/shakeyourbunny/303b000168edc2705262ce40381034a3)
# makes the resizeable progress bar work on windows. The comments on both those gist are worth a read.
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '▒', printEnd = "\r"):
"""
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)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
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)
# Print New Line on Complete
if iteration == total:
print()
# Sample useage
import time
## A List of Items
items = list(range(0, 57))
l = len(items)
## Initial call to print 0% progress
printProgressBar(0, l, prefix = 'Progress:', suffix = 'Complete', length = 50)
for i, item in enumerate(items):
# Do stuff...
time.sleep(0.1)
# Update Progress Bar
printProgressBar(i + 1, l, prefix = 'Progress:', suffix = 'Complete', length = 50)
# Single call version where you pass it the iterable and the function itself does the work
def progressBar(iterable, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', printEnd = "\r"):
"""
Call in a loop to create terminal progress bar
@params:
iterable - Required : iterable object (Iterable)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
total = len(iterable)
# Progress Bar Printing Function
def printProgressBar (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
printProgressBar(0)
# Update Progress Bar
for i, item in enumerate(iterable):
yield item
printProgressBar(i + 1)
# Print New Line on Complete
print()
# Sample useage
import time
## A List of Items
items = list(range(0, 57))
## A Nicer, Single-Call Usage
for item in progressBar(items, prefix = 'Progress:', suffix = 'Complete', length = 50):
# Do stuff...
time.sleep(0.1)
@CodeArtha

Copy link
Copy Markdown
Author

tqdm: add a progress meter to your loops in a second:

import time
from tqdm import tqdm
for i in tqdm(range(100)):
    time.sleep(1)

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