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.

improve: Enable ANSI support in stderr (#2110)

Wasim Sandhufe609e4c8841
diff --git a/frontend/src/components/editor/output/ConsoleOutput.tsx b/frontend/src/components/editor/output/ConsoleOutput.tsx
index 629e710eb0..95bca24a86 100644
--- a/frontend/src/components/editor/output/ConsoleOutput.tsx
+++ b/frontend/src/components/editor/output/ConsoleOutput.tsx
@@ -147,7 +147,7 @@ export const ConsoleOutput = (props: Props): React.ReactNode => {
<NameCellContentEditable
value={cellName}
cellId={cellId}
- className="bg-[var(--slate-4)] border-[var(--slate-4)] hover:bg-[var(--slate-5)] dark:bg-[var(--sky-5)] dark:border-[var(--sky-5)] dark:bg-[var(--sky-6)] dark:text-[var(--sky-12)] text-[var(--slate-12)] rounded-tl rounded-br-lg absolute right-0 bottom-0 text-xs px-1.5 py-0.5 font-mono"
+ className="bg-[var(--slate-4)] border-[var(--slate-4)] hover:bg-[var(--slate-5)] dark:border-[var(--sky-5)] dark:bg-[var(--sky-6)] dark:text-[var(--sky-12)] text-[var(--slate-12)] rounded-tl rounded-br-lg absolute right-0 bottom-0 text-xs px-1.5 py-0.5 font-mono"
/>
</div>
);
diff --git a/frontend/src/components/editor/output/TextOutput.tsx b/frontend/src/components/editor/output/TextOutput.tsx
index e043236842..b71339e710 100644
--- a/frontend/src/components/editor/output/TextOutput.tsx
+++ b/frontend/src/components/editor/output/TextOutput.tsx
@@ -12,7 +12,7 @@ interface Props {
}
export const TextOutput = ({ text, channel }: Props): JSX.Element => {
- const shouldRenderAnsi = channel === "stdout";
+ const shouldRenderAnsi = channel === "stdout" || channel === "stderr";
const renderAnsiText = (text: string) => {
return (
diff --git a/marimo/_smoke_tests/ansi.py b/marimo/_smoke_tests/ansi.py
index 470d4e9fae..0a64b9a937 100644
--- a/marimo/_smoke_tests/ansi.py
+++ b/marimo/_smoke_tests/ansi.py
@@ -1,7 +1,8 @@
# Copyright 2024 Marimo. All rights reserved.
+
import marimo
-__generated_with = "0.1.88"
+__generated_with = "0.8.3"
app = marimo.App()
@@ -51,5 +52,44 @@ def __():
return
+def __():
+ import logging
+
+ # ANSI escape codes for colors
+ class AnsiColorFormatter(logging.Formatter):
+ COLOR_CODES = {
+ 'DEBUG': '\033[94m', # Blue
+ 'INFO': '\033[92m', # Green
+ 'WARNING': '\033[93m', # Yellow
+ 'ERROR': '\033[91m', # Red
+ 'CRITICAL': '\033[95m', # Magenta
+ }
+ RESET_CODE = '\033[0m'
+
+ def format(self, record):
+ color = self.COLOR_CODES.get(record.levelname, self.RESET_CODE)
+ return f"{color}{super().format(record)}{self.RESET_CODE}"
+
+ # Configure the logging
+ logger = logging.getLogger()
+ logger.setLevel(logging.DEBUG)
+
+ # Add a new handler
+ handler = logging.StreamHandler()
+ formatter = AnsiColorFormatter('%(levelname)s: %(message)s')
+ handler.setFormatter(formatter)
+ logger.addHandler(handler)
+
+ # Example log messages
+ logging.error(f"\033[1;32mDirectory created at /path/to/dir\033[0m")
+ logger.debug("This is a debug message")
+ logger.info("This is an info message")
+ logger.warning("This is a warning message")
+ logger.error("This is an error message")
+ logger.critical("This is a critical message")
+ return AnsiColorFormatter, formatter, handler, logger, logging
+
+
if __name__ == "__main__":
app.run()