Skip to content

Commit 8a66f6e

Browse files
committed
notebook: fix delay, add tests
1 parent a638d6a commit 8a66f6e

2 files changed

Lines changed: 51 additions & 11 deletions

File tree

tests_notebook.ipynb

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"outputs": [],
1717
"source": [
1818
"from functools import partial\n",
19+
"from time import sleep\n",
1920
"\n",
2021
"from tqdm.notebook import tqdm_notebook\n",
2122
"from tqdm.notebook import tnrange\n",
@@ -36,7 +37,7 @@
3637
"text": [
3738
"Help on function display in module tqdm.notebook:\n",
3839
"\n",
39-
"display(self, msg=None, pos=None, close=False, bar_style=None)\n",
40+
"display(self, msg=None, pos=None, close=False, bar_style=None, check_delay=True)\n",
4041
" Use `self.sp` to display `msg` in the specified `pos`.\n",
4142
" \n",
4243
" Consider overloading this function when inheriting to use e.g.:\n",
@@ -450,6 +451,44 @@
450451
" print(t)\n",
451452
" assert t.colour == 'yellow'"
452453
]
454+
},
455+
{
456+
"cell_type": "code",
457+
"execution_count": 18,
458+
"metadata": {},
459+
"outputs": [],
460+
"source": [
461+
"# NBVAL_TEST_NAME: delay no trigger\n",
462+
"with tqdm_notebook(total=1, delay=10) as t:\n",
463+
" t.update()"
464+
]
465+
},
466+
{
467+
"cell_type": "code",
468+
"execution_count": 19,
469+
"metadata": {},
470+
"outputs": [
471+
{
472+
"data": {
473+
"application/vnd.jupyter.widget-view+json": {
474+
"model_id": "fe102eedbb4f437783fbd0cff32f6613",
475+
"version_major": 2,
476+
"version_minor": 0
477+
},
478+
"text/plain": [
479+
"100%|##########| 1/1 [00:00<00:00, 7.68it/s]"
480+
]
481+
},
482+
"metadata": {},
483+
"output_type": "display_data"
484+
}
485+
],
486+
"source": [
487+
"# NBVAL_TEST_NAME: delay trigger\n",
488+
"with tqdm_notebook(total=1, delay=0.1) as t:\n",
489+
" sleep(0.1)\n",
490+
" t.update()"
491+
]
453492
}
454493
],
455494
"metadata": {

tqdm/notebook.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def status_printer(_, total=None, desc=None, ncols=None):
145145

146146
def display(self, msg=None, pos=None,
147147
# additional signals
148-
close=False, bar_style=None):
148+
close=False, bar_style=None, check_delay=True):
149149
# Note: contrary to native tqdm, msg='' does NOT clear bar
150150
# goal is to keep all infos if error happens so user knows
151151
# at which iteration the loop failed.
@@ -190,6 +190,10 @@ def display(self, msg=None, pos=None,
190190
except AttributeError:
191191
self.container.visible = False
192192

193+
if check_delay and self.delay > 0 and not self.displayed:
194+
display(self.container)
195+
self.displayed = True
196+
193197
@property
194198
def colour(self):
195199
if hasattr(self, 'container'):
@@ -243,7 +247,7 @@ def __init__(self, *args, **kwargs):
243247

244248
# Print initial bar state
245249
if not self.disable:
246-
self.display()
250+
self.display(check_delay=False)
247251

248252
def __iter__(self):
249253
try:
@@ -258,11 +262,6 @@ def __iter__(self):
258262
# since this could be a shared bar which the user will `reset()`
259263

260264
def update(self, n=1):
261-
if self.disable:
262-
return
263-
if not self.displayed and self.delay > 0:
264-
display(self.container)
265-
self.displayed = True
266265
try:
267266
return super(tqdm_notebook, self).update(n=n)
268267
# NB: except ... [ as ...] breaks IPython async KeyboardInterrupt
@@ -275,16 +274,18 @@ def update(self, n=1):
275274
# since this could be a shared bar which the user will `reset()`
276275

277276
def close(self):
277+
if self.disable:
278+
return
278279
super(tqdm_notebook, self).close()
279280
# Try to detect if there was an error or KeyboardInterrupt
280281
# in manual mode: if n < total, things probably got wrong
281282
if self.total and self.n < self.total:
282-
self.disp(bar_style='danger')
283+
self.disp(bar_style='danger', check_delay=False)
283284
else:
284285
if self.leave:
285-
self.disp(bar_style='success')
286+
self.disp(bar_style='success', check_delay=False)
286287
else:
287-
self.disp(close=True)
288+
self.disp(close=True, check_delay=False)
288289

289290
def clear(self, *_, **__):
290291
pass

0 commit comments

Comments
 (0)