|
1 | 1 | import sys |
| 2 | +import time |
| 3 | +from html import escape |
| 4 | +from typing import Any, Callable, TextIO, cast |
| 5 | + |
| 6 | + |
| 7 | +def _in_notebook() -> bool: |
| 8 | + try: |
| 9 | + from IPython import get_ipython |
| 10 | + except ImportError: |
| 11 | + return False |
| 12 | + |
| 13 | + shell_getter = cast(Callable[[], Any | None], get_ipython) |
| 14 | + shell = shell_getter() |
| 15 | + return bool(shell and shell.__class__.__name__ == "ZMQInteractiveShell") |
| 16 | + |
| 17 | + |
| 18 | +def _create_notebook_display_handle(message: str) -> Any | None: |
| 19 | + try: |
| 20 | + from IPython.display import display |
| 21 | + except ImportError: |
| 22 | + return None |
| 23 | + |
| 24 | + _display: Callable[..., Any] = display |
| 25 | + return _display(_render_notebook_message(message), display_id=True) |
| 26 | + |
| 27 | + |
| 28 | +def _render_notebook_message(message: str) -> Any: |
| 29 | + try: |
| 30 | + from IPython.display import HTML |
| 31 | + except ImportError: |
| 32 | + return message |
| 33 | + |
| 34 | + html_cls = cast(Callable[[str], Any], HTML) |
| 35 | + return html_cls( |
| 36 | + "<pre style='margin: 0; white-space: pre-wrap; font-family: monospace;'>" |
| 37 | + f"{escape(message)}" |
| 38 | + "</pre>" |
| 39 | + ) |
2 | 40 |
|
3 | 41 |
|
4 | 42 | class ProgressDisplay: |
5 | | - """Manages terminal progress display during job polling.""" |
6 | | - |
7 | | - # Class constants |
8 | | - COLORS = { |
9 | | - "completed": "\033[92m", |
10 | | - "processing": "\033[93m", |
11 | | - "pending": "\033[94m", |
12 | | - "failed": "\033[91m", |
13 | | - "reset": "\033[0m", |
14 | | - } |
15 | | - SYMBOLS = {"completed": "✓", "processing": "⟳", "pending": "○", "failed": "✗"} |
| 43 | + """Manages terminal and notebook progress display during job polling.""" |
| 44 | + |
| 45 | + COLORS = {"failed": "\033[91m", "reset": "\033[0m"} |
16 | 46 | SPINNER_CHARS = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"] |
17 | 47 |
|
18 | | - def __init__(self) -> None: |
| 48 | + def __init__(self, stream: TextIO | None = None) -> None: |
| 49 | + self.stream = stream or sys.stdout |
| 50 | + self._interactive = bool( |
| 51 | + hasattr(self.stream, "isatty") and self.stream.isatty() |
| 52 | + ) |
| 53 | + self._use_notebook_display = not self._interactive and _in_notebook() |
| 54 | + self._display_handle: Any | None = None |
| 55 | + self._finalized = False |
| 56 | + self._last_plain_status: str | None = None |
| 57 | + self._start_time = time.monotonic() |
19 | 58 | self.spinner_frame = 0 |
20 | | - self.last_status: dict[str, str] = {} |
21 | 59 |
|
22 | | - def update(self, cluster_status: dict[str, str]) -> None: |
23 | | - """Update progress display with current cluster status.""" |
24 | | - if not cluster_status: |
| 60 | + def update(self, job_status: str) -> None: |
| 61 | + """Update progress display with the overall job status.""" |
| 62 | + if self._finalized: |
25 | 63 | return |
26 | 64 |
|
27 | | - # Always render to keep spinner animating |
28 | | - self._render(cluster_status, is_final=False) |
29 | | - |
30 | | - # Track last status for potential future use |
31 | | - if cluster_status != self.last_status: |
32 | | - self.last_status = cluster_status.copy() |
| 65 | + if self._interactive: |
| 66 | + message = self._build_running_line(job_status) |
| 67 | + print(f"\r{message}\033[K", end="", file=self.stream, flush=True) |
| 68 | + elif self._use_notebook_display: |
| 69 | + self._update_notebook_display(self._build_running_line(job_status)) |
| 70 | + else: |
| 71 | + message = self._build_plain_line(job_status) |
| 72 | + if message != self._last_plain_status: |
| 73 | + print(message, file=self.stream, flush=True) |
| 74 | + self._last_plain_status = message |
33 | 75 |
|
34 | | - # Always increment spinner to show activity |
35 | 76 | self.spinner_frame += 1 |
36 | 77 |
|
37 | | - def finalize(self, cluster_status: dict[str, str]) -> None: |
| 78 | + def finalize( |
| 79 | + self, |
| 80 | + final_status: str | None = None, |
| 81 | + cluster_status: dict[str, str] | None = None, |
| 82 | + ) -> None: |
38 | 83 | """Show final status and cleanup.""" |
39 | | - if cluster_status: |
40 | | - self._render(cluster_status, is_final=True) |
41 | | - print() # Ensure newline |
42 | | - |
43 | | - def _render(self, cluster_status: dict[str, str], is_final: bool) -> None: |
44 | | - """Render status to terminal.""" |
45 | | - status_counts = self._count_statuses(cluster_status) |
46 | | - progress_bar = self._build_progress_bar(cluster_status) |
47 | | - status_line = self._build_status_line( |
48 | | - progress_bar, status_counts, is_final=is_final |
49 | | - ) |
| 84 | + if self._finalized: |
| 85 | + return |
| 86 | + self._finalized = True |
50 | 87 |
|
51 | | - # Print status line |
52 | | - if is_final: |
53 | | - print(f"\r{status_line}{self.COLORS['reset']}") |
54 | | - sys.stdout.flush() |
55 | | - self._show_failed_clusters(cluster_status, status_counts["failed"]) |
56 | | - else: |
57 | | - print(f"\r{status_line}{self.COLORS['reset']}", end="", flush=True) |
58 | | - |
59 | | - def _count_statuses(self, cluster_status: dict[str, str]) -> dict[str, int]: |
60 | | - """Count occurrences of each status.""" |
61 | | - counts = {"completed": 0, "failed": 0} |
62 | | - for status in cluster_status.values(): |
63 | | - counts[status] = counts.get(status, 0) + 1 |
64 | | - return counts |
65 | | - |
66 | | - def _build_progress_bar(self, cluster_status: dict[str, str]) -> str: |
67 | | - """Build colored progress bar from cluster statuses.""" |
68 | | - progress_units = [] |
69 | | - for cluster_id in self._sorted_cluster_ids(cluster_status): |
70 | | - status = cluster_status[cluster_id] |
71 | | - color = self.COLORS.get(status, self.COLORS["reset"]) |
72 | | - symbol = self.SYMBOLS.get(status, "?") |
73 | | - progress_units.append(f"{color}{symbol}{self.COLORS['reset']}") |
74 | | - return "".join(progress_units) |
75 | | - |
76 | | - def _build_status_line( |
77 | | - self, progress_bar: str, counts: dict[str, int], is_final: bool |
78 | | - ) -> str: |
79 | | - """Build status line with progress bar and counts.""" |
80 | | - total = sum(counts.values()) |
81 | | - completed = counts["completed"] |
82 | | - |
83 | | - if is_final: |
84 | | - status_line = f"[DONE] [{progress_bar}] {completed}/{total}" |
85 | | - if counts["failed"] > 0: |
86 | | - status_line += f" ({counts['failed']} failed)" |
87 | | - elif completed == total: |
88 | | - status_line += " completed" |
| 88 | + if final_status is None: |
| 89 | + if self._interactive: |
| 90 | + print(file=self.stream, flush=True) |
| 91 | + return |
| 92 | + |
| 93 | + message = self._build_final_line(final_status) |
| 94 | + if self._interactive: |
| 95 | + print(f"\r{message}\033[K", file=self.stream, flush=True) |
| 96 | + elif self._use_notebook_display: |
| 97 | + self._update_notebook_display(message) |
89 | 98 | else: |
90 | | - spinner = self.SPINNER_CHARS[self.spinner_frame % len(self.SPINNER_CHARS)] |
91 | | - status_line = f"{spinner} [{progress_bar}] {completed}/{total} completed" |
| 99 | + print(message, file=self.stream, flush=True) |
| 100 | + |
| 101 | + if final_status == "failed" and cluster_status: |
| 102 | + self._show_failed_clusters(cluster_status) |
| 103 | + |
| 104 | + def _update_notebook_display(self, message: str) -> None: |
| 105 | + """Update a single notebook output cell instead of printing many lines.""" |
| 106 | + if self._display_handle is None: |
| 107 | + self._display_handle = _create_notebook_display_handle(message) |
| 108 | + if self._display_handle is None: |
| 109 | + self._use_notebook_display = False |
| 110 | + print(message, file=self.stream, flush=True) |
| 111 | + return |
92 | 112 |
|
93 | | - return status_line |
| 113 | + self._display_handle.update(_render_notebook_message(message)) |
94 | 114 |
|
95 | | - def _show_failed_clusters( |
96 | | - self, cluster_status: dict[str, str], failed_count: int |
97 | | - ) -> None: |
98 | | - """Show details of failed clusters.""" |
99 | | - if failed_count == 0: |
100 | | - return |
| 115 | + def _build_running_line(self, job_status: str) -> str: |
| 116 | + spinner = self.SPINNER_CHARS[self.spinner_frame % len(self.SPINNER_CHARS)] |
| 117 | + elapsed = self._format_elapsed() |
| 118 | + return f"{spinner} {self._status_message(job_status)} {elapsed} elapsed" |
| 119 | + |
| 120 | + def _build_plain_line(self, job_status: str) -> str: |
| 121 | + return self._status_message(job_status) |
| 122 | + |
| 123 | + @staticmethod |
| 124 | + def _build_final_line(final_status: str) -> str: |
| 125 | + if final_status == "completed": |
| 126 | + return "[DONE] CyteType job completed." |
| 127 | + if final_status == "failed": |
| 128 | + return "[FAILED] CyteType job failed." |
| 129 | + if final_status == "timed_out": |
| 130 | + return "[TIMEOUT] CyteType job timed out." |
| 131 | + return "[STOPPED] CyteType job stopped." |
101 | 132 |
|
| 133 | + @staticmethod |
| 134 | + def _status_message(job_status: str) -> str: |
| 135 | + if job_status == "pending": |
| 136 | + return "CyteType job queued..." |
| 137 | + if job_status == "processing": |
| 138 | + return "CyteType job running..." |
| 139 | + if job_status == "not_found": |
| 140 | + return "Waiting for CyteType job to start..." |
| 141 | + return "Waiting for CyteType results..." |
| 142 | + |
| 143 | + def _format_elapsed(self) -> str: |
| 144 | + elapsed = int(time.monotonic() - self._start_time) |
| 145 | + minutes, seconds = divmod(elapsed, 60) |
| 146 | + return f"{minutes:02d}:{seconds:02d}" |
| 147 | + |
| 148 | + def _show_failed_clusters(self, cluster_status: dict[str, str]) -> None: |
| 149 | + """Show details of failed clusters.""" |
102 | 150 | failed_details = [] |
103 | 151 | for cluster_id in self._sorted_cluster_ids(cluster_status): |
104 | | - if cluster_status[cluster_id] == "failed": |
105 | | - color = self.COLORS["failed"] |
106 | | - symbol = self.SYMBOLS["failed"] |
| 152 | + if cluster_status[cluster_id] != "failed": |
| 153 | + continue |
| 154 | + |
| 155 | + if self._interactive: |
107 | 156 | failed_details.append( |
108 | | - f"{color}{symbol} Cluster {cluster_id}{self.COLORS['reset']}" |
| 157 | + f"{self.COLORS['failed']}✗ Cluster {cluster_id}{self.COLORS['reset']}" |
109 | 158 | ) |
| 159 | + else: |
| 160 | + failed_details.append(f"✗ Cluster {cluster_id}") |
110 | 161 |
|
111 | | - # Group into lines of 4 |
112 | 162 | for i in range(0, len(failed_details), 4): |
113 | | - print(f" {' | '.join(failed_details[i : i + 4])}") |
| 163 | + print(f" {' | '.join(failed_details[i : i + 4])}", file=self.stream) |
114 | 164 |
|
115 | 165 | @staticmethod |
116 | 166 | def _sorted_cluster_ids(cluster_status: dict[str, str]) -> list[str]: |
|
0 commit comments