Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notebook: support delay #1142

Merged
merged 3 commits into from Apr 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 40 additions & 1 deletion tests_notebook.ipynb
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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": {
Expand Down
20 changes: 14 additions & 6 deletions tqdm/notebook.py
Expand Up @@ -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.
Expand Down Expand Up @@ -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'):
Expand Down Expand Up @@ -234,14 +238,16 @@ 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

# Print initial bar state
if not self.disable:
self.display()
self.display(check_delay=False)

def __iter__(self):
try:
Expand All @@ -268,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
Expand Down