|
| 1 | +""" |
| 2 | +Notebook-aware progress display for ModelForge CLI. |
| 3 | +
|
| 4 | +Automatically uses ipywidgets when running inside a Jupyter/Colab/VSCode |
| 5 | +notebook environment, and falls back to tqdm for plain terminal use. |
| 6 | +""" |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | + |
| 10 | +def _is_notebook() -> bool: |
| 11 | + """Return True if running inside a Jupyter-compatible notebook kernel.""" |
| 12 | + try: |
| 13 | + from IPython import get_ipython |
| 14 | + shell = get_ipython() |
| 15 | + if shell is None: |
| 16 | + return False |
| 17 | + return shell.__class__.__name__ == "ZMQInteractiveShell" |
| 18 | + except ImportError: |
| 19 | + return False |
| 20 | + |
| 21 | + |
| 22 | +class ProgressDisplay: |
| 23 | + """ |
| 24 | + Unified progress display that works in notebooks and terminals. |
| 25 | +
|
| 26 | + Usage:: |
| 27 | +
|
| 28 | + display = ProgressDisplay() |
| 29 | + display.start("Loading model...") |
| 30 | + display.update(50, "Halfway there") |
| 31 | + display.update(100, "Done!") |
| 32 | + display.close() |
| 33 | + """ |
| 34 | + |
| 35 | + def __init__(self): |
| 36 | + self._notebook = _is_notebook() |
| 37 | + self._bar = None |
| 38 | + self._label = None |
| 39 | + self._tqdm_bar = None |
| 40 | + self._last_value = 0 |
| 41 | + |
| 42 | + def start(self, message: str = "Starting...") -> None: |
| 43 | + """Initialise and display the progress bar.""" |
| 44 | + self._last_value = 0 |
| 45 | + if self._notebook: |
| 46 | + self._start_widget(message) |
| 47 | + else: |
| 48 | + self._start_tqdm(message) |
| 49 | + |
| 50 | + def _start_widget(self, message: str) -> None: |
| 51 | + try: |
| 52 | + import ipywidgets as widgets |
| 53 | + from IPython.display import display |
| 54 | + |
| 55 | + self._bar = widgets.IntProgress( |
| 56 | + value=0, |
| 57 | + min=0, |
| 58 | + max=100, |
| 59 | + description="", |
| 60 | + bar_style="info", |
| 61 | + layout=widgets.Layout(width="100%"), |
| 62 | + ) |
| 63 | + self._label = widgets.Label(value=message) |
| 64 | + box = widgets.VBox([self._label, self._bar]) |
| 65 | + display(box) |
| 66 | + except ImportError: |
| 67 | + # ipywidgets not available — fall through to tqdm |
| 68 | + self._notebook = False |
| 69 | + self._start_tqdm(message) |
| 70 | + |
| 71 | + def _start_tqdm(self, message: str) -> None: |
| 72 | + from tqdm import tqdm |
| 73 | + |
| 74 | + self._tqdm_bar = tqdm( |
| 75 | + total=100, |
| 76 | + desc=message, |
| 77 | + bar_format="{desc}: {percentage:3.0f}%|{bar}| [{elapsed}]", |
| 78 | + dynamic_ncols=True, |
| 79 | + ) |
| 80 | + |
| 81 | + def update(self, value: int, message: str = "") -> None: |
| 82 | + """ |
| 83 | + Update progress. |
| 84 | +
|
| 85 | + Args: |
| 86 | + value: Progress value 0-100. |
| 87 | + message: Status message to display alongside the bar. |
| 88 | + """ |
| 89 | + value = max(0, min(100, value)) |
| 90 | + if self._notebook: |
| 91 | + if self._bar is not None: |
| 92 | + self._bar.value = value |
| 93 | + if value >= 100: |
| 94 | + self._bar.bar_style = "success" |
| 95 | + if self._label is not None and message: |
| 96 | + self._label.value = message |
| 97 | + else: |
| 98 | + if self._tqdm_bar is not None: |
| 99 | + delta = value - self._last_value |
| 100 | + if delta > 0: |
| 101 | + self._tqdm_bar.update(delta) |
| 102 | + if message: |
| 103 | + self._tqdm_bar.set_description(message) |
| 104 | + self._last_value = value |
| 105 | + |
| 106 | + def close(self) -> None: |
| 107 | + """Close/finalize the progress display.""" |
| 108 | + if self._notebook: |
| 109 | + if self._bar is not None: |
| 110 | + self._bar.bar_style = "success" |
| 111 | + else: |
| 112 | + if self._tqdm_bar is not None: |
| 113 | + self._tqdm_bar.close() |
| 114 | + self._tqdm_bar = None |
| 115 | + |
| 116 | + def error(self) -> None: |
| 117 | + """Mark the progress bar as errored.""" |
| 118 | + if self._notebook: |
| 119 | + if self._bar is not None: |
| 120 | + self._bar.bar_style = "danger" |
| 121 | + if self._label is not None: |
| 122 | + self._label.value = "Training failed." |
| 123 | + else: |
| 124 | + if self._tqdm_bar is not None: |
| 125 | + self._tqdm_bar.set_description("Training failed") |
| 126 | + self._tqdm_bar.close() |
| 127 | + self._tqdm_bar = None |
0 commit comments