Skip to content

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.

feat: support ansi in console output (#713)

Myles Scolnick50c0800b14cd
diff --git a/frontend/package.json b/frontend/package.json
index b08943b9da..4610bbada9 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -66,6 +66,7 @@
"@types/react-grid-layout": "^1.3.5",
"@uiw/codemirror-extensions-langs": "^4.21.21",
"@uiw/react-codemirror": "^4.21.21",
+ "ansi_up": "^6.0.2",
"blob-polyfill": "^7.0.20220408",
"class-variance-authority": "^0.7.0",
"clsx": "^2.1.0",
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml
index 53766ce084..791a4e6169 100644
--- a/frontend/pnpm-lock.yaml
+++ b/frontend/pnpm-lock.yaml
@@ -167,6 +167,9 @@ dependencies:
'@uiw/react-codemirror':
specifier: ^4.21.21
+ ansi_up:
+ specifier: ^6.0.2
+ version: 6.0.2
blob-polyfill:
specifier: ^7.0.20220408
version: 7.0.20220408
@@ -7367,6 +7370,10 @@ packages:
resolution: {integrity: sha512-bN798gFfQX+viw3R7yrGWRqnrN2oRkEkUjjl4JNn4E8GxxbjtG3FbrEIIY3l8/hrwUwIeCZvi4QuOTP4MErVug==}
engines: {node: '>=12'}
+ resolution: {integrity: sha512-3G3vKvl1ilEp7J1u6BmULpMA0xVoW/f4Ekqhl8RTrJrhEBkonKn5k3bUc5Xt+qDayA6iDX0jyUh3AbZjB/l0tw==}
+ dev: false
+
resolution: {integrity: sha512-7UvmKalWRt1wgjL1RrGxoSJW/0QZFIegpeGvZG9kjp8vrRu55XTHbwnqq2GpXm9uLbcuhxm3IqX9OB4MZR1b2A==}
diff --git a/frontend/src/components/editor/output/ConsoleOutput.tsx b/frontend/src/components/editor/output/ConsoleOutput.tsx
index a0eba44ccf..34f841376c 100644
--- a/frontend/src/components/editor/output/ConsoleOutput.tsx
+++ b/frontend/src/components/editor/output/ConsoleOutput.tsx
@@ -7,6 +7,9 @@ import { DEFAULT_CELL_NAME } from "@/core/cells/names";
import { NameCellContentEditable } from "../actions/name-cell-input";
import { CellId } from "@/core/cells/ids";
import { Input } from "@/components/ui/input";
+import { AnsiUp } from "ansi_up";
+
+const ansiUp = new AnsiUp();
interface Props {
cellId: CellId;
@@ -37,6 +40,12 @@ export const ConsoleOutput = (props: Props): React.ReactNode => {
return null;
}
+ const renderText = (text: string) => {
+ return (
+ <span dangerouslySetInnerHTML={{ __html: ansiUp.ansi_to_html(text) }} />
+ );
+ };
+
return (
<div
title={stale ? "This console output is stale" : undefined}
@@ -56,7 +65,7 @@ export const ConsoleOutput = (props: Props): React.ReactNode => {
if (output.response == null) {
return (
<div key={idx} className="flex gap-2 items-center">
- <span>{output.data}</span>
+ {renderText(output.data)}
<Input
type="text"
autoComplete="off"
@@ -74,7 +83,7 @@ export const ConsoleOutput = (props: Props): React.ReactNode => {
}
return (
<div key={idx} className="flex gap-2 items-center">
- <span>{output.data}</span>
+ {renderText(output.data)}
<span className="text-[var(--sky-11)]">{output.response}</span>
</div>
);
diff --git a/frontend/src/components/editor/output/TextOutput.tsx b/frontend/src/components/editor/output/TextOutput.tsx
index bd52b7cb39..53fe31b900 100644
--- a/frontend/src/components/editor/output/TextOutput.tsx
+++ b/frontend/src/components/editor/output/TextOutput.tsx
@@ -2,6 +2,9 @@
import { OutputChannel } from "@/core/kernel/messages";
import { cn } from "@/utils/cn";
+import { AnsiUp } from "ansi_up";
+
+const ansiUp = new AnsiUp();
interface Props {
text: string;
@@ -9,6 +12,14 @@ interface Props {
}
export const TextOutput = ({ text, channel }: Props): JSX.Element => {
+ const shouldRenderAnsi = channel === "stdout";
+
+ const renderAnsiText = (text: string) => {
+ return (
+ <span dangerouslySetInnerHTML={{ __html: ansiUp.ansi_to_html(text) }} />
+ );
+ };
+
return (
<span
className={cn(
@@ -17,7 +28,7 @@ export const TextOutput = ({ text, channel }: Props): JSX.Element => {
channel,
)}
>
- {text}
+ {shouldRenderAnsi ? renderAnsiText(text) : text}
</span>
);
};
diff --git a/marimo/_smoke_tests/ansi.py b/marimo/_smoke_tests/ansi.py
new file mode 100644
index 0000000000..62a2432e06
--- /dev/null
+++ b/marimo/_smoke_tests/ansi.py
@@ -0,0 +1,54 @@
+import marimo
+
+__generated_with = "0.1.88"
+app = marimo.App()
+
+
+def __():
+ import marimo as mo
+ return mo,
+
+
+def __():
+ print(
+ "".join(
+ "\x1b[{}m{}\n\x1b[0m".format(31 + i, "Hello, World!") for i in range(8)
+ )
+ )
+ return
+
+
+def __():
+ txt = "\n\n\x1B[1;33;40m 33;40 \x1B[1;33;41m 33;41 \x1B[1;33;42m 33;42 \x1B[1;33;43m 33;43 \x1B[1;33;44m 33;44 \x1B[1;33;45m 33;45 \x1B[1;33;46m 33;46 \x1B[1m\x1B[0\n\n\x1B[1;33;42m >> Tests OK\n\n"
+
+ print(txt)
+ return txt,
+
+
+def __():
+ import sys
+
+ print("Hello world", file=sys.stderr)
+ return sys,
+
+
+def __():
+ # No ANSI conversion when not stdout or stderr
+ "".join("\x1b[{}m{}\n\x1b[0m".format(31 + i, "Hello, World!") for i in range(8))
+ return
+
+
+def __():
+ # Colors input()
+ input("\x1b[34mPress Enter to continue\x1b[0m")
+ return
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/marimo/_smoke_tests/third_party/rich.py b/marimo/_smoke_tests/third_party/rich.py
new file mode 100644
index 0000000000..7555ff0563
--- /dev/null
+++ b/marimo/_smoke_tests/third_party/rich.py
@@ -0,0 +1,28 @@
+import marimo
+
+__generated_with = "0.1.88"
+app = marimo.App()
+
+
+def __():
+ import marimo as mo
+ return mo,
+
+
+def __():
+ # pip install rich
+ from rich import print as pprint
+ return pprint,
+
+
+def __(pprint):
+ pprint("[bold red]Error:[/bold red] Something went wrong!")
+ pprint("[green]Success:[/green] Operation completed.")
+ return
+
+
+if __name__ == "__main__":
+ app.run()