Skip to content

Instantly share code, notes, and snippets.

@parsa
Last active July 29, 2017 20:32
Show Gist options
  • Select an option

  • Save parsa/e2a013255a9a3924382ee7f0c033a73c to your computer and use it in GitHub Desktop.

Select an option

Save parsa/e2a013255a9a3924382ee7f0c033a73c to your computer and use it in GitHub Desktop.
pyprogress
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
def printProgressBar(
iteration,
total,
prefix = '',
suffix = '',
decimals = 1,
length = 100,
fill = '█'
):
percent = round(100.* float(iteration) / float(total), decimals)
filledLength = int(float(length) * float(iteration) / float(total))
bar = fill * filledLength + '-' * (length - filledLength)
sys.stdout.write('\r{prefix} |{bar}| {percent}% {suffix}'.format(prefix=prefix, bar=bar, percent=percent, suffix=suffix))
if iteration == total:
sys.stdout.write('\n')
sys.stdout.flush()
#
# Sample Usage
#
from time import sleep
MAX=57
# make a list
items = list(range(MAX))
i = 0
l = len(items)
# Initial call to print 0% progress
printProgressBar(i, l, prefix = 'Progress:', suffix = 'Complete', length = 50)
for item in items:
# Do stuff...
sleep(0.05)
# Update Progress Bar
i += 1
printProgressBar(
i, l, prefix = 'Progress:', suffix = 'Complete', length = 50
)
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
# Print iterations progress
def printProgressBar(
iteration,
total,
prefix = '',
suffix = '',
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)
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)
"""
percent = ("{0:." + str(decimals) + "f}"
).format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
sys.stdout.write('\r%s |%s| %s%% %s\r' % (prefix, bar, percent, suffix))
# Print New Line on Complete
if iteration == total:
sys.stdout.write('\n')
#
# Sample Usage
#
from time import sleep
# make a list
items = list(range(0, 57))
i = 0
l = len(items)
# Initial call to print 0% progress
printProgressBar(i, l, prefix = 'Progress:', suffix = 'Complete', length = 50)
for item in items:
# Do stuff...
sleep(0.05)
# Update Progress Bar
i += 1
printProgressBar(
i, l, prefix = 'Progress:', suffix = 'Complete', length = 50
)
## Sample Output
#Progress: |█████████████████████████████████████████████-----| 90.0% Complete
#import sys
#
#
#def progress(count, total, suffix=''):
# bar_len = 60
# filled_len = int(round(bar_len * count / float(total)))
#
# percents = round(100.0 * count / float(total), 1)
# bar = '█' * filled_len + '-' * (bar_len - filled_len)
#
# sys.stdout.write('\r[%s] %s%s ...%s' % (bar, percents, '%', suffix))
#
# #if count == total:
# # sys.stdout.write('\n')
#
# sys.stdout.flush()
#
#for i in range(57):
# progress(i, 57)
# sleep(.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment