Skip to content

Instantly share code, notes, and snippets.

@embayer
Last active February 4, 2017 16:57
Show Gist options
  • Save embayer/6a21a9ec59b8c57ea872180e47255608 to your computer and use it in GitHub Desktop.
Save embayer/6a21a9ec59b8c57ea872180e47255608 to your computer and use it in GitHub Desktop.
render a progressbar on the commandline
def progressbar(seconds, prefix='', suffix=''):
''' render a progressbar on the commandline
'''
def print_progressbar(iteration, total, decimals=1, length=100, fill= '█'):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filled_length = int(length * iteration // total)
bar = fill * filled_length + '-' * (length - filled_length)
print('\r{} |{}| {}%% {}'.format(prefix, bar, percent, suffix), end='\r')
# Print New Line on Complete
if iteration == total:
print()
# make a list
items = list(range(0, seconds))
i = 0
l = len(items)
# Initial call to print 0% progress
print_progressbar(i, l, length=50)
for item in items:
# do stuff...
sleep(1)
# update progressbar
i += 1
seconds_left = l - i
mins, secs = divmod(seconds_left, 60)
suffix = '{:02d}:{:02d}'.format(mins, secs)
print_progressbar(i, l, length=50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment