Skip to content
This repository has been archived by the owner on Jun 14, 2020. It is now read-only.

Minor improvements #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
)
```

Expand All @@ -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)

57 changes: 57 additions & 0 deletions code.py
Original file line number Diff line number Diff line change
@@ -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
)