diff --git a/README.md b/README.md index a9e9030..c720a2a 100644 --- a/README.md +++ b/README.md @@ -3,8 +3,8 @@ ### Code Just copy and paste it into your project: ```python -def log_progress(sequence, every=None, size=None, name='Items'): - from ipywidgets import IntProgress, HTML, VBox +def log_progress(sequence, every=None, size=None, name=''): + from ipywidgets import IntProgress, HTML, HBox from IPython.display import display is_iterator = False @@ -28,21 +28,21 @@ def log_progress(sequence, every=None, size=None, name='Items'): else: progress = IntProgress(min=0, max=size, value=0) label = HTML() - box = VBox(children=[label, progress]) + box = HBox(children=[label, progress]) display(box) index = 0 try: - for index, record in enumerate(sequence, 1): - if index == 1 or index % every == 0: + for index, record in enumerate(sequence, 0): + if index % every == 0: if is_iterator: - label.value = '{name}: {index} / ?'.format( + label.value = '{name} {index} / ?'.format( name=name, index=index ) else: progress.value = index - label.value = u'{name}: {index} / {size}'.format( + label.value = u'{name} {index} / {size}'.format( name=name, index=index, size=size @@ -53,10 +53,12 @@ def log_progress(sequence, every=None, size=None, name='Items'): raise else: progress.bar_style = 'success' + index += 1 progress.value = index - label.value = "{name}: {index}".format( + label.value = "{name} {index} / {size}".format( name=name, - index=str(index or '?') + index=str(index or '?'), + size=size ) ``` @@ -80,4 +82,3 @@ More then one progress bar can be in a sigle cell: They can even be from different threads: ![](https://habrastorage.org/files/e64/69a/fe5/e6469afe59ed485c84565a672d24cd50.gif) - diff --git a/code.py b/code.py new file mode 100644 index 0000000..869978a --- /dev/null +++ b/code.py @@ -0,0 +1,57 @@ +def log_progress(sequence, every=None, size=None, name=''): + from ipywidgets import IntProgress, HTML, HBox + from IPython.display import display + + is_iterator = False + if size is None: + try: + size = len(sequence) + except TypeError: + is_iterator = True + if size is not None: + if every is None: + if size <= 200: + every = 1 + else: + every = int(size / 200) # every 0.5% + else: + assert every is not None, 'sequence is iterator, set every' + + if is_iterator: + progress = IntProgress(min=0, max=1, value=1) + progress.bar_style = 'info' + else: + progress = IntProgress(min=0, max=size, value=0) + label = HTML() + box = HBox(children=[label, progress]) + display(box) + + index = 0 + try: + for index, record in enumerate(sequence, 0): + if index % every == 0: + if is_iterator: + label.value = '{name} {index} / ?'.format( + name=name, + index=index + ) + else: + progress.value = index + label.value = u'{name} {index} / {size}'.format( + name=name, + index=index, + size=size + ) + yield record + except: + progress.bar_style = 'danger' + raise + else: + progress.bar_style = 'success' + index += 1 + progress.value = index + label.value = "{name} {index} / {size}".format( + name=name, + index=str(index or '?'), + size=size + )