Skip to content

Instantly share code, notes, and snippets.

@paulwinex
Last active April 23, 2020 16:49
Show Gist options
  • Save paulwinex/c48b60a5cd05fa9d766138980ffd86e6 to your computer and use it in GitHub Desktop.
Save paulwinex/c48b60a5cd05fa9d766138980ffd86e6 to your computer and use it in GitHub Desktop.
Simple progress bar in Python.
class ProgressBar(object):
def __init__(self, label='Progress', start_message=None, end_message=None,
chunk_full='#', chunk_empty='-', border='[]'):
self.lb = label
self.sm = start_message
self.em = end_message
self.cf = chunk_full
self.ce = chunk_empty
self.br = border
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print()
def progress(self, value, suffix=None):
text = '\r'+'{label} {progress:>3}% {left}{full}{empty}{right}{end}'.format(
label=self.lb,
progress=value,
left=self.br[0],
full=self.cf * value,
empty=self.ce * (100 - value),
right=self.br[-1],
end=suffix if suffix is not None else '').strip()
print(text, end='')
def test():
import time
with ProgressBar('Done:', start_message='Start', end_message='End') as pr:
for i in range(0, 100+1, 4):
time.sleep(0.1)
pr.progress(i)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment