From 930e3fd54172e85093b737085e3a391880f1ef01 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Fri, 18 Dec 2020 22:59:44 +0000 Subject: [PATCH] fix ASCII notebook export - fixes #1098, fixes #105, fixes #937 --- setup.cfg | 2 +- tqdm/notebook.py | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/setup.cfg b/setup.cfg index 6192d663f..379d8d8aa 100644 --- a/setup.cfg +++ b/setup.cfg @@ -3,7 +3,7 @@ universal = 1 [flake8] ignore = W503,W504,E722 -max_line_length = 80 +max_line_length = 88 exclude = .asv,.eggs,.tox,.ipynb_checkpoints,build,dist,.git,__pycache__ [tool:pytest] diff --git a/tqdm/notebook.py b/tqdm/notebook.py index a129f535c..85f919b56 100644 --- a/tqdm/notebook.py +++ b/tqdm/notebook.py @@ -57,6 +57,7 @@ except ImportError: IPY = 0 IProgress = None + HBox = object try: from IPython.display import display # , clear_output @@ -74,6 +75,18 @@ __all__ = ['tqdm_notebook', 'tnrange', 'tqdm', 'trange'] +class TqdmHBox(HBox): + def __repr__(self): + if not hasattr(self, "pbar"): + return super(TqdmHBox, self).__repr__() + return self.pbar.format_meter(**dict( + self.pbar.format_dict, + bar_format=( + self.pbar.format_dict['bar_format'] or "{l_bar}{bar}{r_bar}" + ).replace("", "{bar}"), + ascii=True)) + + class tqdm_notebook(std_tqdm): """ Experimental IPython/Jupyter Notebook widget using tqdm! @@ -110,7 +123,7 @@ def status_printer(_, total=None, desc=None, ncols=None): rtext = HTML() if desc: ltext.value = desc - container = HBox(children=[ltext, pbar, rtext]) + container = TqdmHBox(children=[ltext, pbar, rtext]) # Prepare layout if ncols is not None: # use default style of ipywidgets # ncols could be 100, "100px", "100%" @@ -228,8 +241,8 @@ def __init__(self, *args, **kwargs): # 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 - 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 if display_here: display(self.container) self.sp = self.display