|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT license. |
| 3 | + |
| 4 | +import json |
| 5 | +import re |
| 6 | +import sys |
| 7 | + |
| 8 | +# tqdm text-mode progress bar patterns: |
| 9 | +# - "%|" separates percentage from the bar |
| 10 | +# - Block characters (━, █, ▏-▉) are used for the bar itself |
| 11 | +# - "\r" carriage returns are used for in-place updates |
| 12 | +_TQDM_PATTERNS = [ |
| 13 | + re.compile(r"%\|"), # " 0%|" or " 50%|..." |
| 14 | + re.compile(r"[━█▏▎▍▌▋▊▉]"), # progress bar block characters |
| 15 | +] |
| 16 | + |
| 17 | + |
| 18 | +def _is_tqdm_line(line: str) -> bool: |
| 19 | + """ |
| 20 | + Check if a line is part of a tqdm progress bar output. |
| 21 | +
|
| 22 | + Args: |
| 23 | + line (str): A single line of text from stderr output. |
| 24 | +
|
| 25 | + Returns: |
| 26 | + bool: True if the line matches tqdm progress bar patterns. |
| 27 | + """ |
| 28 | + stripped = line.strip() |
| 29 | + if not stripped or stripped == "\r": |
| 30 | + # Bare carriage returns or blank lines between tqdm updates |
| 31 | + return False |
| 32 | + return any(pattern.search(line) for pattern in _TQDM_PATTERNS) |
| 33 | + |
| 34 | + |
| 35 | +def strip_notebook_progress_bars(file_path: str) -> bool: |
| 36 | + """ |
| 37 | + Remove tqdm progress bar outputs from notebook cell stderr streams. |
| 38 | +
|
| 39 | + Strips stderr stream outputs that contain tqdm progress bar patterns. |
| 40 | + If all lines in a stderr output are tqdm lines, the entire output is removed. |
| 41 | + If only some lines are tqdm, those lines are stripped and the output is kept. |
| 42 | +
|
| 43 | + Args: |
| 44 | + file_path (str): Path to the .ipynb file. |
| 45 | +
|
| 46 | + Returns: |
| 47 | + bool: True if the file was modified. |
| 48 | + """ |
| 49 | + if not file_path.endswith(".ipynb"): |
| 50 | + return False |
| 51 | + |
| 52 | + with open(file_path, encoding="utf-8") as f: |
| 53 | + content = json.load(f) |
| 54 | + |
| 55 | + modified = False |
| 56 | + |
| 57 | + for cell in content.get("cells", []): |
| 58 | + outputs = cell.get("outputs", []) |
| 59 | + new_outputs = [] |
| 60 | + |
| 61 | + for output in outputs: |
| 62 | + if output.get("output_type") == "stream" and output.get("name") == "stderr": |
| 63 | + text_lines = output.get("text", []) |
| 64 | + non_tqdm_lines = [line for line in text_lines if not _is_tqdm_line(line)] |
| 65 | + |
| 66 | + if len(non_tqdm_lines) < len(text_lines): |
| 67 | + modified = True |
| 68 | + # Keep output only if there are meaningful non-tqdm lines |
| 69 | + remaining = [line for line in non_tqdm_lines if line.strip()] |
| 70 | + if remaining: |
| 71 | + output["text"] = non_tqdm_lines |
| 72 | + new_outputs.append(output) |
| 73 | + # else: drop the entire output (all tqdm or only whitespace left) |
| 74 | + else: |
| 75 | + new_outputs.append(output) |
| 76 | + else: |
| 77 | + new_outputs.append(output) |
| 78 | + |
| 79 | + if len(new_outputs) != len(outputs): |
| 80 | + cell["outputs"] = new_outputs |
| 81 | + |
| 82 | + if not modified: |
| 83 | + return False |
| 84 | + |
| 85 | + with open(file_path, "w", encoding="utf-8") as f: |
| 86 | + json.dump(content, f, indent=1, ensure_ascii=False) |
| 87 | + f.write("\n") |
| 88 | + |
| 89 | + return True |
| 90 | + |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + modified_files = [file_path for file_path in sys.argv[1:] if strip_notebook_progress_bars(file_path)] |
| 94 | + if modified_files: |
| 95 | + print("Stripped tqdm progress bars from:", modified_files) |
| 96 | + sys.exit(1) |
0 commit comments