diff --git a/tqdm/notebook.py b/tqdm/notebook.py index db25d857..1f488d25 100644 --- a/tqdm/notebook.py +++ b/tqdm/notebook.py @@ -14,6 +14,7 @@ from __future__ import absolute_import, division # import compatibility functions and utilities import re import sys +from weakref import proxy # to inherit from the tqdm class from .std import tqdm as std_tqdm @@ -75,17 +76,19 @@ __all__ = ['tqdm_notebook', 'tnrange', 'tqdm', 'trange'] class TqdmHBox(HBox): """`ipywidgets.HBox` with a pretty representation""" def _repr_json_(self, pretty=None): - if not hasattr(self, "pbar"): + pbar = getattr(self, 'pbar', None) + if pbar is None: return {} - d = self.pbar.format_dict + d = pbar.format_dict if pretty is not None: d["ascii"] = not pretty return d def __repr__(self, pretty=False): - if not hasattr(self, "pbar"): + pbar = getattr(self, 'pbar', None) + if pbar is None: return super(TqdmHBox, self).__repr__() - return self.pbar.format_meter(**self._repr_json_(pretty)) + return pbar.format_meter(**self._repr_json_(pretty)) def _repr_pretty_(self, pp, *_, **__): pp.text(self.__repr__(True)) @@ -237,7 +240,7 @@ class tqdm_notebook(std_tqdm): 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 self.container = self.status_printer(self.fp, total, self.desc, self.ncols) - self.container.pbar = self + self.container.pbar = proxy(self) self.displayed = False if display_here and self.delay <= 0: display(self.container)