Last active
September 18, 2016 01:47
-
-
Save MyGodIsHe/975b012c1b023774c41bf25e0ccb3c82 to your computer and use it in GitHub Desktop.
Console Progress Bar on python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
class ProgressBar(object): | |
def __init__(self, total, prefix='', suffix='', decimals=1, bar_length=100): | |
""" | |
Call in a loop to create terminal progress bar | |
:param total: total iterations | |
:param prefix: prefix string | |
:param suffix: suffix string | |
:param decimals: positive number of decimals in percent complete | |
:param bar_length: character length of bar | |
""" | |
self.iteration = 0 | |
self.total = total | |
self.prefix = prefix | |
self.suffix = suffix | |
self.decimals = decimals | |
self.bar_length = bar_length | |
self.show() | |
def __iter__(self): | |
return self | |
def next(self): | |
if self.iteration >= self.total: | |
raise StopIteration | |
self.iteration += 1 | |
self.show() | |
def show(self): | |
format_str = "{0:." + str(self.decimals) + "f}" | |
percents = format_str.format(100 * (self.iteration / float(self.total))) | |
filled_length = int(round(self.bar_length * self.iteration / float(self.total))) | |
bar = '█' * filled_length + '-' * (self.bar_length - filled_length) | |
sys.stdout.write('\r%s |%s| %s%s %s' % (self.prefix, bar, percents, '%', self.suffix)), | |
sys.stdout.flush() | |
if self.iteration == self.total: | |
sys.stdout.write('\n') | |
sys.stdout.flush() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment