|
| 1 | +""" |
| 2 | +Jupyter notebook progress support. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +from time import perf_counter |
| 8 | + |
| 9 | +import ipywidgets as widgets |
| 10 | +from humanize import metric |
| 11 | +from IPython.display import display |
| 12 | + |
| 13 | +from ._base import Progress |
| 14 | +from ._formats import field_format |
| 15 | + |
| 16 | +__all__ = ["JupyterProgress"] |
| 17 | + |
| 18 | + |
| 19 | +class JupyterProgress(Progress): |
| 20 | + """ |
| 21 | + Progress logging to Jupyter notebook widgets. |
| 22 | + """ |
| 23 | + |
| 24 | + widget: widgets.IntProgress |
| 25 | + text: widgets.Label |
| 26 | + box: widgets.HBox |
| 27 | + total: int | None |
| 28 | + current: int |
| 29 | + _last_update: float = 0 |
| 30 | + _field_format: str | None = None |
| 31 | + |
| 32 | + def __init__( |
| 33 | + self, |
| 34 | + label: str | None, |
| 35 | + total: int | None, |
| 36 | + fields: dict[str, str | None], |
| 37 | + ): |
| 38 | + self.current = 0 |
| 39 | + self.total = total |
| 40 | + if total: |
| 41 | + self.widget = widgets.IntProgress(value=0, min=0, max=total, step=1) |
| 42 | + else: |
| 43 | + self.widget = widgets.IntProgress(value=1, min=0, max=1, step=1) |
| 44 | + self.widget.bar_style = "info" |
| 45 | + |
| 46 | + pieces = [] |
| 47 | + if label: |
| 48 | + pieces.append(widgets.Label(value=label)) |
| 49 | + pieces.append(self.widget) |
| 50 | + |
| 51 | + self.text = widgets.Label() |
| 52 | + if total: |
| 53 | + self.text.value = "0 / {}".format(metric(total)) |
| 54 | + pieces.append(self.text) |
| 55 | + |
| 56 | + self.box = widgets.HBox(pieces) |
| 57 | + display(self.box) |
| 58 | + |
| 59 | + if fields: |
| 60 | + self._field_format = ", ".join( |
| 61 | + [f"{name}: {field_format(name, fs)}" for (name, fs) in fields.items()] |
| 62 | + ) |
| 63 | + |
| 64 | + def update(self, advance: int = 1, **kwargs: float | int | str): |
| 65 | + """ |
| 66 | + Update the progress bar. |
| 67 | + """ |
| 68 | + self.current += advance |
| 69 | + now = perf_counter() |
| 70 | + if now - self._last_update >= 0.1 or (self.total and self.current >= self.total): |
| 71 | + self.widget.value = self.current |
| 72 | + if self.total: |
| 73 | + if self.total >= 1000: |
| 74 | + txt = "{} / {}".format(metric(self.current), metric(self.total)) |
| 75 | + else: |
| 76 | + txt = "{} / {}".format(self.current, self.total) |
| 77 | + else: |
| 78 | + txt = "{} / ?".format(metric(self.current)) |
| 79 | + if self._field_format: |
| 80 | + txt += " ({})".format(self._field_format.format(**kwargs)) |
| 81 | + self.text.value = txt |
| 82 | + |
| 83 | + self._last_update = now |
| 84 | + # if self._field_format: |
| 85 | + # self.tqdm.set_postfix_str(self._field_format.format(kwargs)) |
| 86 | + |
| 87 | + def finish(self): |
| 88 | + """ |
| 89 | + Finish and clean up this progress bar. If the progresss bar is used as |
| 90 | + a context manager, this is automatically called on context exit. |
| 91 | + """ |
| 92 | + self.box.close() |
| 93 | + |
| 94 | + def __enter__(self): |
| 95 | + return self |
| 96 | + |
| 97 | + def __exit__(self, *args): |
| 98 | + self.finish() |
0 commit comments