Skip to content

Commit

Permalink
fix notebook memory leak
Browse files Browse the repository at this point in the history
- fixes #1216
  • Loading branch information
casperdcl committed Aug 14, 2021
1 parent 1ac732c commit 8c35d42
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions tqdm/notebook.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# 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
Expand Down Expand Up @@ -75,17 +76,19 @@
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))
Expand Down Expand Up @@ -237,7 +240,7 @@ def __init__(self, *args, **kwargs):
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)
Expand Down

0 comments on commit 8c35d42

Please sign in to comment.