fix notebook memory leak

- fixes #1216
This commit is contained in:
Casper da Costa-Luis 2021-08-14 22:52:32 +01:00
parent 1ac732c10d
commit 8c35d4228c
No known key found for this signature in database
GPG Key ID: F5126E5FBD2512AD
1 changed files with 8 additions and 5 deletions

View File

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