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] 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 a526c046..fb8227d7 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 0baf6ea7..db25d857 100644 --- a/tqdm/notebook.py +++ b/tqdm/notebook.py @@ -145,7 +145,7 @@ class tqdm_notebook(std_tqdm): 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 @@ class tqdm_notebook(std_tqdm): 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 @@ class tqdm_notebook(std_tqdm): # Print initial bar state if not self.disable: - self.display() + self.display(check_delay=False) def __iter__(self): try: @@ -258,11 +262,6 @@ class tqdm_notebook(std_tqdm): # 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 @@ class tqdm_notebook(std_tqdm): # 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