Widget based progress bar for Jupyter
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is 3 commits ahead of alexanderkuk:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
LICENSE
README.md
code.py
test.ipynb

README.md

Widget based progress bar for Jupyter (IPython Notebook)

Code

Just copy and paste it into your project:

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
        )

Examples

Progress bar changes its color based on outcome:

Iterators are supported:

More then one progress bar can be in a sigle cell:

They can even be from different threads: