Skip to content

Instantly share code, notes, and snippets.

@ekaitz-zarraga
Last active August 22, 2016 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ekaitz-zarraga/1ef0aa2075d62fbf784f466ded0d0854 to your computer and use it in GitHub Desktop.
Save ekaitz-zarraga/1ef0aa2075d62fbf784f466ded0d0854 to your computer and use it in GitHub Desktop.
Simple python progress bar python2 and 3
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