Refactor tqdm_notebook with new subclassing architecture

Signed-off-by: Stephen L. <lrq3000@gmail.com>
This commit is contained in:
Stephen L 2016-01-23 00:42:30 +01:00
parent b06b168dd3
commit e6e4cebbc3
2 changed files with 67 additions and 60 deletions

View File

@ -346,6 +346,16 @@ Returns
Experimental GUI version of trange!
"""
class tqdm_notebook(tqdm):
"""
Experimental IPython/Jupyter Notebook widget using tqdm!
"""
def tnrange(*args, **kwargs):
"""
Experimental IPython/Jupyter Notebook widget using tqdm!
"""
Examples and Advanced Usage
---------------------------

View File

@ -42,70 +42,70 @@ except ImportError: # pragma: no cover
from cgi import escape # python 2.x
# to inherit from the tqdm class
from ._tqdm import tqdm, format_meter, StatusPrinter
from ._tqdm import tqdm
__author__ = {"github.com/": ["lrq3000", "casperdcl"]}
__all__ = ['tqdm_notebook', 'tnrange']
def NotebookPrinter(file, total=None, desc=None): # pragma: no cover
"""
Manage the printing of an IPython/Jupyter Notebook progress bar widget.
"""
# Fallback to text bar if there's no total
if not total:
return StatusPrinter(file)
fp = file
if not getattr(fp, 'flush', False): # pragma: no cover
fp.flush = lambda: None
# Prepare IPython progress bar
pbar = IntProgress(min=0, max=total)
if desc:
pbar.description = desc
# Prepare status text
ptext = HTML()
# Only way to place text to the right of the bar is to use a container
container = HBox(children=[pbar, ptext])
display(container)
def print_status(s='', close=False):
# Clear previous output (really necessary?)
# clear_output(wait=1)
# Get current iteration value from format_meter string
n = None
if s:
npos = s.find(r'/|/') # because we use bar_format=r'{n}|...'
# Check that n can be found in s (else n > total)
if npos >= 0:
n = int(s[:npos]) # get n from string
s = s[npos+3:] # remove from string
# Update bar with current n value
if n is not None:
pbar.value = n
# Print stats
s = s.replace('||', '') # remove inesthetical pipes
s = escape(s) # html escape special characters (like '?')
ptext.value = s
# Special signal to close the bar
if close:
container.visible = False
return print_status
class tqdm_notebook(tqdm): # pragma: no cover
"""
Experimental IPython/Jupyter Notebook widget using tqdm!
"""
def __init__(self, *args, **kwargs):
@staticmethod
def status_printer(file, total=None, desc=None):
"""
Manage the printing of an IPython/Jupyter Notebook progress bar widget.
"""
# Fallback to text bar if there's no total
if not total:
return super(tqdm_notebook, tqdm_notebook).status_printer(file)
fp = file
if not getattr(fp, 'flush', False): # pragma: no cover
fp.flush = lambda: None
# Prepare IPython progress bar
pbar = IntProgress(min=0, max=total)
if desc:
pbar.description = desc
# Prepare status text
ptext = HTML()
# Only way to place text to the right of the bar is to use a container
container = HBox(children=[pbar, ptext])
display(container)
def print_status(s='', close=False):
# Clear previous output (really necessary?)
# clear_output(wait=1)
# Get current iteration value from format_meter string
n = None
if s:
npos = s.find(r'/|/') # because we use bar_format=r'{n}|...'
# Check that n can be found in s (else n > total)
if npos >= 0:
n = int(s[:npos]) # get n from string
s = s[npos+3:] # remove from string
# Update bar with current n value
if n is not None:
pbar.value = n
# Print stats
s = s.replace('||', '') # remove inesthetical pipes
s = escape(s) # html escape special characters (like '?')
ptext.value = s
# Special signal to close the bar
if close:
container.visible = False
return print_status
def __init__(self, *args, **kwargs):
# Setup default output
if not kwargs.get('file', None) or kwargs['file'] == sys.stderr:
kwargs['file'] = sys.stdout # avoid the red block in IPython
@ -119,25 +119,22 @@ class tqdm_notebook(tqdm): # pragma: no cover
# Delete the text progress bar display
self.sp('')
# Replace with IPython progress bar display
self.sp_nb = NotebookPrinter(self.fp, self.total, self.desc)
self.sp = self.sprinter
self.sp = self.status_printer(self.fp, self.total, self.desc)
self.desc = None # trick to place description before the bar
# Print initial bar state
if not self.disable:
self.sp(format_meter(self.n, self.total, 0,
# TODO: replace by self.refresh()
self.sp(self.format_meter(self.n, self.total, 0,
(self.dynamic_ncols(self.file) if self.dynamic_ncols
else self.ncols),
self.desc, self.ascii, self.unit, self.unit_scale, None,
self.bar_format))
def sprinter(self, s=''):
self.sp_nb(s)
def close(self, *args, **kwargs):
super(tqdm_notebook, self).close(*args, **kwargs)
if not self.leave:
self.sp_nb(s='', close=True)
self.sp(s='', close=True)
def tnrange(*args, **kwargs): # pragma: no cover