A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: keep colors in console outputs with ansi colors (#7367)
Myles Scolnick87a2f232c38c
diff --git a/frontend/src/components/editor/output/__tests__/ansi-reduce.test.ts b/frontend/src/components/editor/output/__tests__/ansi-reduce.test.tsindex 07c316937a..18565ef142 100644--- a/frontend/src/components/editor/output/__tests__/ansi-reduce.test.ts+++ b/frontend/src/components/editor/output/__tests__/ansi-reduce.test.ts@@ -415,7 +415,7 @@ describe("ansiReduce", () => {test("complex progress simulation", () => {const text =- "\r 0%| | 0/101000 [00:00<?, ?it/s]\rCompiling.. : 0%| | 0/101000 [00:00<?, ?it/s]\n\r 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\n\rCompiling.. : 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\n\n\r 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\u001B[A\n\n\rCompiling.. : 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\rRunning chain 0: 0%| | 0/101000 [00:00<?, ?it/s]\n\rRunning chain 1: 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\n\n\rRunning chain 2: 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 15%|████▎ | 15150/101000 [00:00<00:00, 129479.68it/s]\u001B[A\u001B[A\n\rRunning chain 1: 15%|████▎ | 15150/101000 [00:00<00:00, 124469.08it/s]\u001B[A\rRunning chain 0: 15%|████▎ | 15150/101000 [00:00<00:00, 122865.03it/s]\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 30%|████████▋ | 30300/101000 [00:00<00:00, 130679.02it/s]\u001B[A\u001B[A\n\rRunning chain 1: 30%|████████▋ | 30300/101000 [00:00<00:00, 126103.71it/s]\u001B[A\rRunning chain 0: 30%|████████▋ | 30300/101000 [00:00<00:00, 124748.00it/s]\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 45%|█████████████ | 45450/101000 [00:00<00:00, 127078.72it/s]\u001B[A\u001B[A\n\rRunning chain 1: 45%|█████████████ | 45450/101000 [00:00<00:00, 123708.59it/s]\u001B[A\rRunning chain 0: 45%|█████████████ | 45450/101000 [00:00<00:00, 121550.69it/s]\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 60%|█████████████████▍ | 60600/101000 [00:00<00:00, 126319.28it/s]\u001B[A\u001B[A\n\rRunning chain 1: 60%|█████████████████▍ | 60600/101000 [00:00<00:00, 123669.56it/s]\u001B[A\rRunning chain 0: 60%|█████████████████▍ | 60600/101000 [00:00<00:00, 122438.92it/s]\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 75%|█████████████████████▊ | 75750/101000 [00:00<00:00, 126984.84it/s]\u001B[A\u001B[A\n\rRunning chain 1: 75%|█████████████████████▊ | 75750/101000 [00:00<00:00, 123764.98it/s]\u001B[A\rRunning chain 0: 75%|█████████████████████▊ | 75750/101000 [00:00<00:00, 123796.97it/s]\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 90%|██████████████████████████ | 90900/101000 [00:01<00:00, 128241.91it/s]\u001B[A\u001B[A\n\rRunning chain 1: 90%|██████████████████████████ | 90900/101000 [00:01<00:00, 125149.55it/s]\u001B[A\rRunning chain 0: 90%|██████████████████████████ | 90900/101000 [00:01<00:00, 124582.89it/s]\rRunning chain 3: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 88054.93it/s]\n\rRunning chain 2: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 86530.93it/s]\n\rRunning chain 1: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 85142.19it/s]\n\rRunning chain 0: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 84719.76it/s]\n";+ "\r 0%| | 0/101000 [00:00<?, ?it/s]\rCompiling.. : 0%| | 0/101000 [00:00<?, ?it/s]\n\r 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\n\rCompiling.. : 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\n\n\r 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\u001B[A\n\n\rCompiling.. : 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\rRunning chain 0: 0%| | 0/101000 [00:00<?, ?it/s]\n\rRunning chain 1: 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\n\n\rRunning chain 2: 0%| | 0/101000 [00:00<?, ?it/s]\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 15%|████▎ | 15150/101000 [00:00<00:00, 129479.68it/s]\u001B[A\u001B[A\n\rRunning chain 1: 15%|████▎ | 15150/101000 [00:00<00:00, 124469.08it/s]\u001B[A\rRunning chain 0: 15%|████▎ | 15150/101000 [00:00<00:00, 122865.03it/s]\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 30%|████████▋ | 30300/101000 [00:00<00:00, 130679.02it/s]\u001B[A\u001B[A\n\rRunning chain 1: 30%|████████▋ | 30300/101000 [00:00<00:00, 126103.71it/s]\u001B[A\rRunning chain 0: 30%|████████▋ | 30300/101000 [00:00<00:00, 124748.00it/s]\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 45%|█████████████ | 45450/101000 [00:00<00:00, 127078.72it/s]\u001B[A\u001B[A\n\rRunning chain 1: 45%|█████████████ | 45450/101000 [00:00<00:00, 123708.59it/s]\u001B[A\rRunning chain 0: 45%|█████████████ | 45450/101000 [00:00<00:00, 121550.69it/s]\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 60%|█████████████████▝ | 60600/101000 [00:00<00:00, 126319.28it/s]\u001B[A\u001B[A\n\rRunning chain 1: 60%|█████████████████▝ | 60600/101000 [00:00<00:00, 123669.56it/s]\u001B[A\rRunning chain 0: 60%|█████████████████▝ | 60600/101000 [00:00<00:00, 122438.92it/s]\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 75%|█████████████████████▊ | 75750/101000 [00:00<00:00, 126984.84it/s]\u001B[A\u001B[A\n\rRunning chain 1: 75%|█████████████████████▊ | 75750/101000 [00:00<00:00, 123764.98it/s]\u001B[A\rRunning chain 0: 75%|█████████████████████▊ | 75750/101000 [00:00<00:00, 123796.97it/s]\n\n\n\r ... (more hidden) ...\u001B[A\u001B[A\u001B[A\n\n\rRunning chain 2: 90%|██████████████████████████ | 90900/101000 [00:01<00:00, 128241.91it/s]\u001B[A\u001B[A\n\rRunning chain 1: 90%|██████████████████████████ | 90900/101000 [00:01<00:00, 125149.55it/s]\u001B[A\rRunning chain 0: 90%|██████████████████████████ | 90900/101000 [00:01<00:00, 124582.89it/s]\rRunning chain 3: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 88054.93it/s]\n\rRunning chain 2: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 86530.93it/s]\n\rRunning chain 1: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 85142.19it/s]\n\rRunning chain 0: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 84719.76it/s]\n";expect(ansiReduce(text)).toMatchInlineSnapshot(`"Running chain 3: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 88054.93it/s]Running chain 2: 100%|█████████████████████████████| 101000/101000 [00:01<00:00, 86530.93it/s]@@ -531,6 +531,39 @@ describe("AnsiReducer streaming with append()", () => {});});+describe("AnsiReducer color preservation", () => {+ const CASES = [+ // SGR sequences+ "\u001b[34mBlue text\u001b[m normal text\u001b[31mRed text\u001b[0m",+ // Complex SGR with parameters+ "\u001b[1;31mBold Red\u001b[0m \u001b[48;5;240mGray bg\u001b[0m",+ // Character set selection+ "Text\u001b(BMore\u001b(0Graphics\u001b(B",+ // Complex case+ "\u001b[34m[D 251201 15:32:24 cell_runner:695]\u001b(B\u001b[m Running post_execution hooks in context\n\u001b[34m[D 251201 15:32:24 hooks_post_execution:65]\u001b(B\u001b[m Acquiring graph lock to update cell import workspace\n\u001b[34m[D 251201 15:32:24 hooks_post_execution:67]\u001b(B\u001b[m Acquired graph lock to update import workspace.\n",+ ];++ test.each(CASES)("preserves ANSI color codes", (input) => {+ const reducer = new AnsiReducer();+ const result = reducer.reduce(input);+ expect(result).toBe(input);+ });++ test("preserves color codes with cursor movements", () => {+ const reducer = new AnsiReducer();+ // Test that color codes work alongside cursor movements+ // Note: when cursor moves up, lines below are discarded (tqdm behavior)+ const result = reducer.reduce(+ "Line1\n\u001b[31mRed\u001b[0m\u001b[1A\u001b[32mGreen\u001b[0m",+ );+ // After moving up from row 1 to row 0, row 1 is discarded+ // Green is written at the end of row 0+ expect(result).toMatchInlineSnapshot(+ `"Line1 \u001b[32mGreen\u001b[0m"`,+ );+ });+});+describe("StatefulOutputMessage", () => {test("initializes and processes text", () => {const message = {diff --git a/frontend/src/components/editor/output/ansi-reduce.ts b/frontend/src/components/editor/output/ansi-reduce.tsindex dc18573d99..cd2de5671d 100644--- a/frontend/src/components/editor/output/ansi-reduce.ts+++ b/frontend/src/components/editor/output/ansi-reduce.ts@@ -96,10 +96,17 @@ export class TerminalBuffer {/*** Handle a basic ANSI escape sequence.* Supports cursor movement and line erasing.+ * For other sequences (like color codes), preserve them by writing to the buffer.*/handleEscape(seq: string) {const match = TerminalBuffer.ESCAPE_REGEX.exec(seq);if (!match) {+ // If it doesn't match the cursor movement pattern, it might be:+ // - A color/styling code (SGR sequences ending in 'm')+ // - A character set selection sequence (ESC(B)+ // - Other escape sequences+ // Preserve these by writing them as regular text+ this.writeString(seq);return;}@@ -159,6 +166,11 @@ export class TerminalBuffer {// No default}break;+ default:+ // For other CSI sequences we don't handle (like SGR color codes ending in 'm'),+ // preserve them by writing as regular text+ this.writeString(seq);+ break;}}@@ -172,8 +184,9 @@ export class TerminalBuffer {* Parses ANSI escape sequences into tokens for processing.*/export class AnsiParser {+ // Matches both CSI sequences (ESC[...letter) and other escape sequences like character set selection (ESC(B)// biome-ignore lint/suspicious/noControlCharactersInRegex: Needed for ANSI parsing- private ESC_REGEX = /\u001B\[[0-9;]*[A-Za-z]/gu;+ private ESC_REGEX = /\u001B(?:\[[0-9;]*[A-Za-z]|\([0-9A-Za-z])/gu;parse(input: string): { type: "text" | "escape"; value: string }[] {const tokens: { type: "text" | "escape"; value: string }[] = [];