Skip to content

Instantly share code, notes, and snippets.

@dkirkby
Created August 6, 2019 22:20
Show Gist options
  • Save dkirkby/0c88cd2a356ddd21f10bc40866f05055 to your computer and use it in GitHub Desktop.
Save dkirkby/0c88cd2a356ddd21f10bc40866f05055 to your computer and use it in GitHub Desktop.
import time, sys
import IPython.display
class ProgressBar(object):
"""Replace existing contents of the current output cell with a progress bar.
"""
def __init__(self, maxval=1., label='Progress', width=40):
self.maxval = maxval
self.label = label
self.width = width
self.start_at = time.time()
self.update(0.)
def update(self, value):
elapsed = int(round(time.time() - self.start_at))
mins = elapsed // 60
secs = elapsed % 60
timing = '{mins}m{secs:02d}s'.format(mins=mins, secs=secs)
frac = value / self.maxval
ndone = int(round(frac * self.width))
bar = '#' * ndone + '-' * (self.width - ndone)
text = '{label}: [{bar}]{pct:5.1f}% {timing}'.format(
label=self.label, bar=bar, pct=100 * frac, timing=timing)
IPython.display.clear_output(wait=True)
print(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment