Skip to content

Instantly share code, notes, and snippets.

@Kerruba
Last active March 30, 2024 09:05
Show Gist options
  • Save Kerruba/b422b4ea1570882ab677d60f2afd6bf0 to your computer and use it in GitHub Desktop.
Save Kerruba/b422b4ea1570882ab677d60f2afd6bf0 to your computer and use it in GitHub Desktop.
Python #Python
# -*- coding: utf-8 -*-
# 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 : 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 - 1:
sys.stdout.write('\n')
sys.stdout.flush()
@vcancy
Copy link

vcancy commented Jan 9, 2019

cool

@CarlosDomingues
Copy link

Very useful! Thanks! Preview for future reference:

ezgif-4-8c1b5a4d8e14

Copy link

ghost commented Jul 27, 2020

Very nice, only change line 22 to:

if iteration == total -1:

so that the prompt jumps to the next line at the end of the count.

@yashtalele72
Copy link

I was searching for this kind of thing from long time...... Its better than TQDM too as it only works in certain conditions i.e cmd prompt

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