Last active
August 22, 2016 09:20
-
-
Save ekaitz-zarraga/1ef0aa2075d62fbf784f466ded0d0854 to your computer and use it in GitHub Desktop.
Simple python progress bar python2 and 3
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 Progress: | |
""" | |
Simple progress bar | |
""" | |
def __init__(self, total = 100, width=50): | |
self.total = total | |
self.width = width | |
def update(self, value): | |
if not sys.stdout.isatty(): | |
# IT IS NOT A TERMINAL! | |
# DON'T WRITE | |
return | |
if value <= self.total: | |
self.value = value | |
self._repaint() | |
def _repaint(self): | |
percent = round( float(self.value) / float(self.total), 2) | |
bar = int( percent * self.width ) | |
percent = int( percent * 100 ) | |
barstring = [ '#' if i <= bar else ' ' for i in range(self.width) ] | |
barstring = ''.join(barstring) | |
sys.stdout.write('\r[' + barstring + ']' + ' ' + str(percent) + '%') | |
sys.stdout.flush() | |
if percent == 100: | |
sys.stdout.write('\n') | |
if __name__ == '__main__': | |
""" | |
Progress bar tester | |
""" | |
bar = Progress() | |
import time | |
for i in range(101): | |
time.sleep(0.05) | |
bar.update(i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment