2020-06-10 16:04:54 +00:00
|
|
|
"""
|
|
|
|
IPython/Jupyter Notebook progressbar decorator for iterators.
|
2021-12-01 20:47:00 +00:00
|
|
|
Includes a default `range` iterator printing to `stderr`.
|
2020-06-10 16:04:54 +00:00
|
|
|
|
|
|
|
Usage:
|
2021-12-01 20:47:00 +00:00
|
|
|
>>> from tqdm.notebook import trange, tqdm
|
|
|
|
>>> for i in trange(10):
|
|
|
|
... ...
|
2020-06-10 16:04:54 +00:00
|
|
|
"""
|
|
|
|
# future division is important to divide integers and get as
|
|
|
|
# a result precise floating numbers (instead of truncated int)
|
2021-12-01 20:47:00 +00:00
|
|
|
from __future__ import absolute_import, division
|
|
|
|
|
2020-06-10 16:04:54 +00:00
|
|
|
# import compatibility functions and utilities
|
2021-12-01 20:47:00 +00:00
|
|
|
import re
|
2020-06-10 16:04:54 +00:00
|
|
|
import sys
|
2021-12-01 20:47:00 +00:00
|
|
|
from weakref import proxy
|
|
|
|
|
2020-06-10 16:04:54 +00:00
|
|
|
# to inherit from the tqdm class
|
|
|
|
from .std import tqdm as std_tqdm
|
2021-12-01 20:47:00 +00:00
|
|
|
from .utils import _range
|
2020-06-10 16:04:54 +00:00
|
|
|
|
|
|
|
if True: # pragma: no cover
|
|
|
|
# import IPython/Jupyter base widget and display utilities
|
|
|
|
IPY = 0
|
|
|
|
try: # IPython 4.x
|
|
|
|
import ipywidgets
|
|
|
|
IPY = 4
|
|
|
|
except ImportError: # IPython 3.x / 2.x
|
|
|
|
IPY = 32
|
|
|
|
import warnings
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings(
|
2021-12-01 20:47:00 +00:00
|
|
|
'ignore', message=".*The `IPython.html` package has been deprecated.*")
|
2020-06-10 16:04:54 +00:00
|
|
|
try:
|
2021-12-01 20:47:00 +00:00
|
|
|
import IPython.html.widgets as ipywidgets # NOQA: F401
|
2020-06-10 16:04:54 +00:00
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try: # IPython 4.x / 3.x
|
|
|
|
if IPY == 32:
|
2021-12-01 20:47:00 +00:00
|
|
|
from IPython.html.widgets import HTML
|
2020-06-10 16:04:54 +00:00
|
|
|
from IPython.html.widgets import FloatProgress as IProgress
|
2021-12-01 20:47:00 +00:00
|
|
|
from IPython.html.widgets import HBox
|
2020-06-10 16:04:54 +00:00
|
|
|
IPY = 3
|
|
|
|
else:
|
2021-12-01 20:47:00 +00:00
|
|
|
from ipywidgets import HTML
|
2020-06-10 16:04:54 +00:00
|
|
|
from ipywidgets import FloatProgress as IProgress
|
2021-12-01 20:47:00 +00:00
|
|
|
from ipywidgets import HBox
|
2020-06-10 16:04:54 +00:00
|
|
|
except ImportError:
|
|
|
|
try: # IPython 2.x
|
|
|
|
from IPython.html.widgets import HTML
|
2021-12-01 20:47:00 +00:00
|
|
|
from IPython.html.widgets import ContainerWidget as HBox
|
|
|
|
from IPython.html.widgets import FloatProgressWidget as IProgress
|
2020-06-10 16:04:54 +00:00
|
|
|
IPY = 2
|
|
|
|
except ImportError:
|
|
|
|
IPY = 0
|
2021-12-01 20:47:00 +00:00
|
|
|
IProgress = None
|
|
|
|
HBox = object
|
2020-06-10 16:04:54 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
from IPython.display import display # , clear_output
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
# HTML encoding
|
|
|
|
try: # Py3
|
|
|
|
from html import escape
|
|
|
|
except ImportError: # Py2
|
|
|
|
from cgi import escape
|
|
|
|
|
|
|
|
__author__ = {"github.com/": ["lrq3000", "casperdcl", "alexanderkuk"]}
|
|
|
|
__all__ = ['tqdm_notebook', 'tnrange', 'tqdm', 'trange']
|
|
|
|
|
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
class TqdmHBox(HBox):
|
|
|
|
"""`ipywidgets.HBox` with a pretty representation"""
|
|
|
|
def _repr_json_(self, pretty=None):
|
|
|
|
pbar = getattr(self, 'pbar', None)
|
|
|
|
if pbar is None:
|
|
|
|
return {}
|
|
|
|
d = pbar.format_dict
|
|
|
|
if pretty is not None:
|
|
|
|
d["ascii"] = not pretty
|
|
|
|
return d
|
|
|
|
|
|
|
|
def __repr__(self, pretty=False):
|
|
|
|
pbar = getattr(self, 'pbar', None)
|
|
|
|
if pbar is None:
|
|
|
|
return super(TqdmHBox, self).__repr__()
|
|
|
|
return pbar.format_meter(**self._repr_json_(pretty))
|
|
|
|
|
|
|
|
def _repr_pretty_(self, pp, *_, **__):
|
|
|
|
pp.text(self.__repr__(True))
|
|
|
|
|
|
|
|
|
2020-06-10 16:04:54 +00:00
|
|
|
class tqdm_notebook(std_tqdm):
|
|
|
|
"""
|
|
|
|
Experimental IPython/Jupyter Notebook widget using tqdm!
|
|
|
|
"""
|
|
|
|
@staticmethod
|
|
|
|
def status_printer(_, total=None, desc=None, ncols=None):
|
|
|
|
"""
|
|
|
|
Manage the printing of an IPython/Jupyter Notebook progress bar widget.
|
|
|
|
"""
|
|
|
|
# Fallback to text bar if there's no total
|
|
|
|
# DEPRECATED: replaced with an 'info' style bar
|
|
|
|
# if not total:
|
|
|
|
# return super(tqdm_notebook, tqdm_notebook).status_printer(file)
|
|
|
|
|
|
|
|
# fp = file
|
|
|
|
|
|
|
|
# Prepare IPython progress bar
|
2021-12-01 20:47:00 +00:00
|
|
|
if IProgress is None: # #187 #451 #558 #872
|
2020-06-10 16:04:54 +00:00
|
|
|
raise ImportError(
|
2021-12-01 20:47:00 +00:00
|
|
|
"IProgress not found. Please update jupyter and ipywidgets."
|
2020-06-10 16:04:54 +00:00
|
|
|
" See https://ipywidgets.readthedocs.io/en/stable"
|
|
|
|
"/user_install.html")
|
2021-12-01 20:47:00 +00:00
|
|
|
if total:
|
|
|
|
pbar = IProgress(min=0, max=total)
|
|
|
|
else: # No total? Show info style bar with no progress tqdm status
|
|
|
|
pbar = IProgress(min=0, max=1)
|
|
|
|
pbar.value = 1
|
|
|
|
pbar.bar_style = 'info'
|
|
|
|
if ncols is None:
|
|
|
|
pbar.layout.width = "20px"
|
|
|
|
|
|
|
|
ltext = HTML()
|
|
|
|
rtext = HTML()
|
2020-06-10 16:04:54 +00:00
|
|
|
if desc:
|
2021-12-01 20:47:00 +00:00
|
|
|
ltext.value = desc
|
|
|
|
container = TqdmHBox(children=[ltext, pbar, rtext])
|
2020-06-10 16:04:54 +00:00
|
|
|
# Prepare layout
|
|
|
|
if ncols is not None: # use default style of ipywidgets
|
|
|
|
# ncols could be 100, "100px", "100%"
|
|
|
|
ncols = str(ncols) # ipywidgets only accepts string
|
|
|
|
try:
|
|
|
|
if int(ncols) > 0: # isnumeric and positive
|
|
|
|
ncols += 'px'
|
|
|
|
except ValueError:
|
|
|
|
pass
|
|
|
|
pbar.layout.flex = '2'
|
|
|
|
container.layout.width = ncols
|
|
|
|
container.layout.display = 'inline-flex'
|
|
|
|
container.layout.flex_flow = 'row wrap'
|
|
|
|
|
|
|
|
return container
|
|
|
|
|
|
|
|
def display(self, msg=None, pos=None,
|
|
|
|
# additional signals
|
2021-12-01 20:47:00 +00:00
|
|
|
close=False, bar_style=None, check_delay=True):
|
2020-06-10 16:04:54 +00:00
|
|
|
# Note: contrary to native tqdm, msg='' does NOT clear bar
|
|
|
|
# goal is to keep all infos if error happens so user knows
|
|
|
|
# at which iteration the loop failed.
|
|
|
|
|
|
|
|
# Clear previous output (really necessary?)
|
|
|
|
# clear_output(wait=1)
|
|
|
|
|
|
|
|
if not msg and not close:
|
2021-12-01 20:47:00 +00:00
|
|
|
d = self.format_dict
|
|
|
|
# remove {bar}
|
|
|
|
d['bar_format'] = (d['bar_format'] or "{l_bar}<bar/>{r_bar}").replace(
|
|
|
|
"{bar}", "<bar/>")
|
|
|
|
msg = self.format_meter(**d)
|
2020-06-10 16:04:54 +00:00
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
ltext, pbar, rtext = self.container.children
|
2020-06-10 16:04:54 +00:00
|
|
|
pbar.value = self.n
|
|
|
|
|
|
|
|
if msg:
|
|
|
|
# html escape special characters (like '&')
|
|
|
|
if '<bar/>' in msg:
|
2021-12-01 20:47:00 +00:00
|
|
|
left, right = map(escape, re.split(r'\|?<bar/>\|?', msg, 1))
|
2020-06-10 16:04:54 +00:00
|
|
|
else:
|
|
|
|
left, right = '', escape(msg)
|
|
|
|
|
|
|
|
# Update description
|
2021-12-01 20:47:00 +00:00
|
|
|
ltext.value = left
|
2020-06-10 16:04:54 +00:00
|
|
|
# never clear the bar (signal: msg='')
|
|
|
|
if right:
|
2021-12-01 20:47:00 +00:00
|
|
|
rtext.value = right
|
2020-06-10 16:04:54 +00:00
|
|
|
|
|
|
|
# Change bar style
|
|
|
|
if bar_style:
|
|
|
|
# Hack-ish way to avoid the danger bar_style being overridden by
|
|
|
|
# success because the bar gets closed after the error...
|
2021-12-01 20:47:00 +00:00
|
|
|
if pbar.bar_style != 'danger' or bar_style != 'success':
|
2020-06-10 16:04:54 +00:00
|
|
|
pbar.bar_style = bar_style
|
|
|
|
|
|
|
|
# Special signal to close the bar
|
|
|
|
if close and pbar.bar_style != 'danger': # hide only if no error
|
|
|
|
try:
|
|
|
|
self.container.close()
|
|
|
|
except AttributeError:
|
|
|
|
self.container.visible = False
|
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
if check_delay and self.delay > 0 and not self.displayed:
|
|
|
|
display(self.container)
|
|
|
|
self.displayed = True
|
|
|
|
|
|
|
|
@property
|
|
|
|
def colour(self):
|
|
|
|
if hasattr(self, 'container'):
|
|
|
|
return self.container.children[-2].style.bar_color
|
|
|
|
|
|
|
|
@colour.setter
|
|
|
|
def colour(self, bar_color):
|
|
|
|
if hasattr(self, 'container'):
|
|
|
|
self.container.children[-2].style.bar_color = bar_color
|
|
|
|
|
2020-06-10 16:04:54 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2021-12-01 20:47:00 +00:00
|
|
|
"""
|
|
|
|
Supports the usual `tqdm.tqdm` parameters as well as those listed below.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
display : Whether to call `display(self.container)` immediately
|
|
|
|
[default: True].
|
|
|
|
"""
|
|
|
|
kwargs = kwargs.copy()
|
2020-06-10 16:04:54 +00:00
|
|
|
# Setup default output
|
|
|
|
file_kwarg = kwargs.get('file', sys.stderr)
|
|
|
|
if file_kwarg is sys.stderr or file_kwarg is None:
|
|
|
|
kwargs['file'] = sys.stdout # avoid the red block in IPython
|
|
|
|
|
|
|
|
# Initialize parent class + avoid printing by using gui=True
|
|
|
|
kwargs['gui'] = True
|
|
|
|
# convert disable = None to False
|
|
|
|
kwargs['disable'] = bool(kwargs.get('disable', False))
|
2021-12-01 20:47:00 +00:00
|
|
|
colour = kwargs.pop('colour', None)
|
|
|
|
display_here = kwargs.pop('display', True)
|
2020-06-10 16:04:54 +00:00
|
|
|
super(tqdm_notebook, self).__init__(*args, **kwargs)
|
|
|
|
if self.disable or not kwargs['gui']:
|
2021-12-01 20:47:00 +00:00
|
|
|
self.disp = lambda *_, **__: None
|
2020-06-10 16:04:54 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Get bar width
|
|
|
|
self.ncols = '100%' if self.dynamic_ncols else kwargs.get("ncols", None)
|
|
|
|
|
|
|
|
# Replace with IPython progress bar display (with correct total)
|
|
|
|
unit_scale = 1 if self.unit_scale is True else self.unit_scale or 1
|
|
|
|
total = self.total * unit_scale if self.total else self.total
|
2021-12-01 20:47:00 +00:00
|
|
|
self.container = self.status_printer(self.fp, total, self.desc, self.ncols)
|
|
|
|
self.container.pbar = proxy(self)
|
|
|
|
self.displayed = False
|
|
|
|
if display_here and self.delay <= 0:
|
|
|
|
display(self.container)
|
|
|
|
self.displayed = True
|
|
|
|
self.disp = self.display
|
|
|
|
self.colour = colour
|
2020-06-10 16:04:54 +00:00
|
|
|
|
|
|
|
# Print initial bar state
|
|
|
|
if not self.disable:
|
2021-12-01 20:47:00 +00:00
|
|
|
self.display(check_delay=False)
|
2020-06-10 16:04:54 +00:00
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
def __iter__(self):
|
2020-06-10 16:04:54 +00:00
|
|
|
try:
|
2021-12-01 20:47:00 +00:00
|
|
|
for obj in super(tqdm_notebook, self).__iter__():
|
2020-06-10 16:04:54 +00:00
|
|
|
# return super(tqdm...) will not catch exception
|
|
|
|
yield obj
|
|
|
|
# NB: except ... [ as ...] breaks IPython async KeyboardInterrupt
|
|
|
|
except: # NOQA
|
2021-12-01 20:47:00 +00:00
|
|
|
self.disp(bar_style='danger')
|
2020-06-10 16:04:54 +00:00
|
|
|
raise
|
|
|
|
# NB: don't `finally: close()`
|
|
|
|
# since this could be a shared bar which the user will `reset()`
|
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
def update(self, n=1):
|
2020-06-10 16:04:54 +00:00
|
|
|
try:
|
2021-12-01 20:47:00 +00:00
|
|
|
return super(tqdm_notebook, self).update(n=n)
|
2020-06-10 16:04:54 +00:00
|
|
|
# NB: except ... [ as ...] breaks IPython async KeyboardInterrupt
|
|
|
|
except: # NOQA
|
|
|
|
# cannot catch KeyboardInterrupt when using manual tqdm
|
|
|
|
# as the interrupt will most likely happen on another statement
|
2021-12-01 20:47:00 +00:00
|
|
|
self.disp(bar_style='danger')
|
2020-06-10 16:04:54 +00:00
|
|
|
raise
|
|
|
|
# NB: don't `finally: close()`
|
|
|
|
# since this could be a shared bar which the user will `reset()`
|
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
def close(self):
|
|
|
|
if self.disable:
|
|
|
|
return
|
|
|
|
super(tqdm_notebook, self).close()
|
2020-06-10 16:04:54 +00:00
|
|
|
# Try to detect if there was an error or KeyboardInterrupt
|
|
|
|
# in manual mode: if n < total, things probably got wrong
|
|
|
|
if self.total and self.n < self.total:
|
2021-12-01 20:47:00 +00:00
|
|
|
self.disp(bar_style='danger', check_delay=False)
|
2020-06-10 16:04:54 +00:00
|
|
|
else:
|
|
|
|
if self.leave:
|
2021-12-01 20:47:00 +00:00
|
|
|
self.disp(bar_style='success', check_delay=False)
|
2020-06-10 16:04:54 +00:00
|
|
|
else:
|
2021-12-01 20:47:00 +00:00
|
|
|
self.disp(close=True, check_delay=False)
|
2020-06-10 16:04:54 +00:00
|
|
|
|
2021-12-01 20:47:00 +00:00
|
|
|
def clear(self, *_, **__):
|
|
|
|
pass
|
2020-06-10 16:04:54 +00:00
|
|
|
|
|
|
|
def reset(self, total=None):
|
|
|
|
"""
|
|
|
|
Resets to 0 iterations for repeated use.
|
|
|
|
|
|
|
|
Consider combining with `leave=True`.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
total : int or float, optional. Total to use for the new bar.
|
|
|
|
"""
|
2021-12-01 20:47:00 +00:00
|
|
|
if self.disable:
|
|
|
|
return super(tqdm_notebook, self).reset(total=total)
|
|
|
|
_, pbar, _ = self.container.children
|
|
|
|
pbar.bar_style = ''
|
2020-06-10 16:04:54 +00:00
|
|
|
if total is not None:
|
|
|
|
pbar.max = total
|
2021-12-01 20:47:00 +00:00
|
|
|
if not self.total and self.ncols is None: # no longer unknown total
|
|
|
|
pbar.layout.width = None # reset width
|
2020-06-10 16:04:54 +00:00
|
|
|
return super(tqdm_notebook, self).reset(total=total)
|
|
|
|
|
|
|
|
|
|
|
|
def tnrange(*args, **kwargs):
|
|
|
|
"""
|
|
|
|
A shortcut for `tqdm.notebook.tqdm(xrange(*args), **kwargs)`.
|
|
|
|
On Python3+, `range` is used instead of `xrange`.
|
|
|
|
"""
|
|
|
|
return tqdm_notebook(_range(*args), **kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
# Aliases
|
|
|
|
tqdm = tqdm_notebook
|
|
|
|
trange = tnrange
|