From a22aebcd1d4b0295e8e1f4e795861a5f108f2f74 Mon Sep 17 00:00:00 2001 From: Peter Hansen Date: Mon, 8 Mar 2021 11:57:09 -0600 Subject: [PATCH 1/3] fix delay for notebooks --- tqdm/notebook.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tqdm/notebook.py b/tqdm/notebook.py index 2d73ea825..5b2debaf5 100644 --- a/tqdm/notebook.py +++ b/tqdm/notebook.py @@ -234,8 +234,10 @@ def __init__(self, *args, **kwargs): 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 - if display_here: + self.displayed = False + if display_here and self.delay <= 0: display(self.container) + self.displayed = True self.disp = self.display self.colour = colour @@ -256,6 +258,9 @@ def __iter__(self): # since this could be a shared bar which the user will `reset()` def update(self, n=1): + if not self.displayed and self.delay > 0: + display(self.container) + self.displayed = True try: return super(tqdm_notebook, self).update(n=n) # NB: except ... [ as ...] breaks IPython async KeyboardInterrupt From a638d6a157543c437145dd1506a25fc18c2f1543 Mon Sep 17 00:00:00 2001 From: Peter Hansen Date: Wed, 17 Mar 2021 15:37:30 -0500 Subject: [PATCH 2/3] notebook update checks if disabled --- tqdm/notebook.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tqdm/notebook.py b/tqdm/notebook.py index 5b2debaf5..0baf6ea7c 100644 --- a/tqdm/notebook.py +++ b/tqdm/notebook.py @@ -258,6 +258,8 @@ def __iter__(self): # since this could be a shared bar which the user will `reset()` def update(self, n=1): + if self.disable: + return if not self.displayed and self.delay > 0: display(self.container) self.displayed = True From 8a66f6e35d753f58dbed4425ab2fb91641bef715 Mon Sep 17 00:00:00 2001 From: Casper da Costa-Luis Date: Mon, 5 Apr 2021 16:52:03 +0100 Subject: [PATCH 3/3] notebook: fix `delay`, add tests --- tests_notebook.ipynb | 41 ++++++++++++++++++++++++++++++++++++++++- tqdm/notebook.py | 21 +++++++++++---------- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/tests_notebook.ipynb b/tests_notebook.ipynb index a526c0466..fb8227d7d 100644 --- a/tests_notebook.ipynb +++ b/tests_notebook.ipynb @@ -16,6 +16,7 @@ "outputs": [], "source": [ "from functools import partial\n", + "from time import sleep\n", "\n", "from tqdm.notebook import tqdm_notebook\n", "from tqdm.notebook import tnrange\n", @@ -36,7 +37,7 @@ "text": [ "Help on function display in module tqdm.notebook:\n", "\n", - "display(self, msg=None, pos=None, close=False, bar_style=None)\n", + "display(self, msg=None, pos=None, close=False, bar_style=None, check_delay=True)\n", " Use `self.sp` to display `msg` in the specified `pos`.\n", " \n", " Consider overloading this function when inheriting to use e.g.:\n", @@ -450,6 +451,44 @@ " print(t)\n", " assert t.colour == 'yellow'" ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [], + "source": [ + "# NBVAL_TEST_NAME: delay no trigger\n", + "with tqdm_notebook(total=1, delay=10) as t:\n", + " t.update()" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "fe102eedbb4f437783fbd0cff32f6613", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "100%|##########| 1/1 [00:00<00:00, 7.68it/s]" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# NBVAL_TEST_NAME: delay trigger\n", + "with tqdm_notebook(total=1, delay=0.1) as t:\n", + " sleep(0.1)\n", + " t.update()" + ] } ], "metadata": { diff --git a/tqdm/notebook.py b/tqdm/notebook.py index 0baf6ea7c..db25d8570 100644 --- a/tqdm/notebook.py +++ b/tqdm/notebook.py @@ -145,7 +145,7 @@ def status_printer(_, total=None, desc=None, ncols=None): def display(self, msg=None, pos=None, # additional signals - close=False, bar_style=None): + close=False, bar_style=None, check_delay=True): # 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. @@ -190,6 +190,10 @@ def display(self, msg=None, pos=None, except AttributeError: self.container.visible = False + 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'): @@ -243,7 +247,7 @@ def __init__(self, *args, **kwargs): # Print initial bar state if not self.disable: - self.display() + self.display(check_delay=False) def __iter__(self): try: @@ -258,11 +262,6 @@ def __iter__(self): # since this could be a shared bar which the user will `reset()` def update(self, n=1): - if self.disable: - return - if not self.displayed and self.delay > 0: - display(self.container) - self.displayed = True try: return super(tqdm_notebook, self).update(n=n) # NB: except ... [ as ...] breaks IPython async KeyboardInterrupt @@ -275,16 +274,18 @@ def update(self, n=1): # since this could be a shared bar which the user will `reset()` def close(self): + if self.disable: + return super(tqdm_notebook, self).close() # 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: - self.disp(bar_style='danger') + self.disp(bar_style='danger', check_delay=False) else: if self.leave: - self.disp(bar_style='success') + self.disp(bar_style='success', check_delay=False) else: - self.disp(close=True) + self.disp(close=True, check_delay=False) def clear(self, *_, **__): pass