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: enhance cell log handling to include tracebacks (#7248)

Myles Scolnickb4b53e71ea3d
diff --git a/frontend/src/core/cells/__tests__/logs.test.ts b/frontend/src/core/cells/__tests__/logs.test.ts
new file mode 100644
index 0000000000..9a86c592ed
--- /dev/null
+++ b/frontend/src/core/cells/__tests__/logs.test.ts
@@ -0,0 +1,371 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
+import type { CellMessage } from "../../kernel/messages";
+import { formatLogTimestamp, getCellLogsForMessage } from "../logs";
+
+describe("getCellLogsForMessage", () => {
+ beforeEach(() => {
+ // Mock console.log to avoid cluttering test output
+ vi.spyOn(console, "log").mockImplementation(() => {
+ // no-op
+ });
+ });
+
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
+ test("handles text/plain MIME type on stdout", () => {
+ const cellMessage: CellMessage = {
+ cell_id: "cell-1",
+ console: [
+ {
+ mimetype: "text/plain",
+ channel: "stdout",
+ data: "Hello, World!",
+ timestamp: 1234567890,
+ },
+ ],
+ output: null,
+ status: "idle",
+ stale_inputs: null,
+ timestamp: 0,
+ };
+
+ const logs = getCellLogsForMessage(cellMessage);
+
+ expect(logs).toHaveLength(1);
+ expect(logs[0]).toEqual({
+ timestamp: 1234567890,
+ level: "stdout",
+ message: "Hello, World!",
+ cellId: "cell-1",
+ });
+ });
+
+ test("handles text/plain MIME type on stderr", () => {
+ const cellMessage: CellMessage = {
+ cell_id: "cell-2",
+ console: [
+ {
+ mimetype: "text/plain",
+ channel: "stderr",
+ data: "Error occurred",
+ timestamp: 1234567890,
+ },
+ ],
+ output: null,
+ status: "idle",
+ stale_inputs: null,
+ timestamp: 0,
+ };
+
+ const logs = getCellLogsForMessage(cellMessage);
+
+ expect(logs).toHaveLength(1);
+ expect(logs[0]).toEqual({
+ timestamp: 1234567890,
+ level: "stderr",
+ message: "Error occurred",
+ cellId: "cell-2",
+ });
+ });
+
+ test("handles text/html MIME type and strips HTML tags", () => {
+ const cellMessage: CellMessage = {
+ cell_id: "cell-3",
+ console: [
+ {
+ mimetype: "text/html",
+ channel: "stdout",
+ data: '<span style="color: red;">Error: Something went wrong</span>',
+ timestamp: 1234567890,
+ },
+ ],
+ output: null,
+ status: "idle",
+ stale_inputs: null,
+ timestamp: 0,
+ };
+
+ const logs = getCellLogsForMessage(cellMessage);
+
+ expect(logs).toHaveLength(1);
+ expect(logs[0]).toEqual({
+ timestamp: 1234567890,
+ level: "stdout",
+ message: "Error: Something went wrong",
+ cellId: "cell-3",
+ });
+ });
+
+ test("handles text/html MIME type on stderr", () => {
+ const cellMessage: CellMessage = {
+ cell_id: "cell-4",
+ console: [
+ {
+ mimetype: "text/html",
+ channel: "stderr",
+ data: "<div><strong>Critical Error:</strong> System failure</div>",
+ timestamp: 1234567890,
+ },
+ ],
+ output: null,
+ status: "idle",
+ stale_inputs: null,
+ timestamp: 0,
+ };
+
+ const logs = getCellLogsForMessage(cellMessage);
+
+ expect(logs).toHaveLength(1);
+ expect(logs[0]).toEqual({
+ timestamp: 1234567890,
+ level: "stderr",
+ message: "Critical Error: System failure",
+ cellId: "cell-4",
+ });
+ });
+
+ test("handles application/vnd.marimo+traceback MIME type and strips HTML", () => {
+ const cellMessage: CellMessage = {
+ cell_id: "cell-5",
+ console: [
+ {
+ mimetype: "application/vnd.marimo+traceback",
+ channel: "marimo-error",
+ data: '<div class="traceback"><span style="color: red;">Traceback (most recent call last):</span><pre> File "test.py", line 1</pre></div>',
+ timestamp: 1234567890,
+ },
+ ],
+ output: null,
+ status: "idle",
+ stale_inputs: null,
+ timestamp: 0,
+ };
+
+ const logs = getCellLogsForMessage(cellMessage);
+
+ expect(logs).toHaveLength(1);
+ expect(logs[0].level).toBe("stderr"); // marimo-error should be treated as stderr
+ expect(logs[0].message).toContain("Traceback (most recent call last):");
+ expect(logs[0].message).toContain('File "test.py", line 1');
+ expect(logs[0].cellId).toBe("cell-5");
+ });
+
+ test("handles multiple console outputs with different MIME types", () => {
+ const cellMessage: CellMessage = {
+ cell_id: "cell-7",
+ console: [
+ {
+ mimetype: "text/plain",
+ channel: "stdout",
+ data: "Plain text output",
+ timestamp: 1234567890,
+ },
+ {
+ mimetype: "text/html",
+ channel: "stdout",
+ data: "<span>HTML output</span>",
+ timestamp: 1234567891,
+ },
+ {
+ mimetype: "application/vnd.marimo+traceback",
+ channel: "stderr",
+ data: "<div>Traceback error</div>",
+ timestamp: 1234567892,
+ },
+ ],
+ output: null,
+ status: "idle",
+ stale_inputs: null,
+ timestamp: 0,
+ };
+
+ const logs = getCellLogsForMessage(cellMessage);
+
+ expect(logs).toHaveLength(3);
+ expect(logs[0].message).toBe("Plain text output");
+ expect(logs[1].message).toBe("HTML output");
+ expect(logs[2].message).toBe("Traceback error");
+ });
+
+ test("uses Date.now() when timestamp is missing", () => {
+ const now = Date.now();
+ vi.spyOn(Date, "now").mockReturnValue(now);
+
+ const cellMessage: CellMessage = {
+ cell_id: "cell-8",
+ console: [
+ {
+ mimetype: "text/plain",
+ channel: "stdout",
+ data: "No timestamp",
+ // timestamp is undefined
+ },
+ ],
+ output: null,
+ status: "idle",
+ stale_inputs: null,
+ timestamp: 0,
+ };
+
+ const logs = getCellLogsForMessage(cellMessage);
+
+ expect(logs).toHaveLength(1);
+ expect(logs[0].timestamp).toBe(now);
+ });
+
+ test("ignores unsupported MIME types", () => {
+ const cellMessage: CellMessage = {
+ cell_id: "cell-9",
+ console: [
+ {
+ mimetype: "application/json",
+ channel: "stdout",
+ data: '{"key": "value"}',
+ timestamp: 1234567890,
+ },
+ ],
+ output: null,
+ status: "idle",
+ stale_inputs: null,
+ timestamp: 0,
+ };
+
+ const logs = getCellLogsForMessage(cellMessage);
+
+ expect(logs).toHaveLength(0);
+ });
+
+ test("ignores non-logging channels", () => {
+ const cellMessage: CellMessage = {
+ cell_id: "cell-10",
+ console: [
+ {
+ mimetype: "text/plain",
+ channel: "pdb" as unknown as "stdout", // Non-logging channel
+ data: "Should be ignored",
+ timestamp: 1234567890,
+ },
+ ],
+ output: null,
+ status: "idle",
+ stale_inputs: null,
+ timestamp: 0,
+ };
+
+ const logs = getCellLogsForMessage(cellMessage);
+
+ expect(logs).toHaveLength(0);
+ });
+
+ test("returns empty array when console is null", () => {
+ const cellMessage: CellMessage = {
+ cell_id: "cell-11",
+ console: null as unknown as CellMessage["console"],
+ output: null,
+ status: "idle",
+ stale_inputs: null,
+ timestamp: 0,
+ };
+
+ const logs = getCellLogsForMessage(cellMessage);
+
+ expect(logs).toHaveLength(0);
+ });
+
+ test("handles complex HTML with nested elements in text/html", () => {
+ const cellMessage: CellMessage = {
+ cell_id: "cell-12",
+ console: [
+ {
+ mimetype: "text/html",
+ channel: "stdout",
+ data: "<div><span>Nested</span> <strong>HTML</strong> <em>content</em></div>",
+ timestamp: 1234567890,
+ },
+ ],
+ output: null,
+ status: "idle",
+ stale_inputs: null,
+ timestamp: 0,
+ };
+
+ const logs = getCellLogsForMessage(cellMessage);
+
+ expect(logs).toHaveLength(1);
+ expect(logs[0].message).toBe("Nested HTML content");
+ });
+
+ test("handles marimo-error channel as stderr level", () => {
+ const cellMessage: CellMessage = {
+ cell_id: "cell-13",
+ console: [
+ {
+ mimetype: "text/plain",
+ channel: "marimo-error",
+ data: "Internal error",
+ timestamp: 1234567890,
+ },
+ ],
+ output: null,
+ status: "idle",
+ stale_inputs: null,
+ timestamp: 0,
+ };
+
+ const logs = getCellLogsForMessage(cellMessage);
+
+ expect(logs).toHaveLength(1);
+ expect(logs[0].level).toBe("stderr");
+ });
+});
+
+describe("formatLogTimestamp", () => {
+ test("formats unix timestamp correctly", () => {
+ // January 1, 2024, 12:00:00 PM UTC
+ const timestamp = 1704110400;
+ const result = formatLogTimestamp(timestamp);
+
+ // The result depends on the timezone, so we just check it's not empty
+ // and contains expected time format elements
+ expect(result).toBeTruthy();
+ expect(result).toMatch(/\d+:\d+:\d+/); // Should contain time format
+ });
+
+ test("formats timestamp with AM/PM notation", () => {
+ const timestamp = 1704110400; // Noon
+ const result = formatLogTimestamp(timestamp);
+
+ // Should contain AM or PM
+ expect(result).toMatch(/AM|PM/);
+ });
+
+ test("returns 'Invalid Date' for invalid timestamp", () => {
+ const invalidTimestamp = Number.NaN;
+ const result = formatLogTimestamp(invalidTimestamp);
+
+ expect(result).toBe("Invalid Date");
+ });
+
+ test("handles edge case: zero timestamp", () => {
+ const timestamp = 0; // Unix epoch
+ const result = formatLogTimestamp(timestamp);
+
+ // Should format successfully (Jan 1, 1970)
+ expect(result).toBeTruthy();
+ expect(result).toMatch(/\d+:\d+:\d+/);
+ });
+
+ test("handles recent timestamp", () => {
+ // Use a recent timestamp (seconds since epoch)
+ const timestamp = Math.floor(Date.now() / 1000);
+ const result = formatLogTimestamp(timestamp);
+
+ expect(result).toBeTruthy();
+ expect(result).toMatch(/\d+:\d+:\d+/);
+ expect(result).toMatch(/AM|PM/);
+ });
+});
diff --git a/frontend/src/core/cells/logs.ts b/frontend/src/core/cells/logs.ts
index 0ed9a2b21b..8bfac32c0c 100644
--- a/frontend/src/core/cells/logs.ts
+++ b/frontend/src/core/cells/logs.ts
@@ -2,7 +2,8 @@
import { fromUnixTime } from "date-fns";
import { toast } from "@/components/ui/use-toast";
-import { invariant } from "@/utils/invariant";
+import { parseHtmlContent } from "@/utils/dom";
+import { Strings } from "@/utils/strings";
import type { CellMessage, OutputMessage } from "../kernel/messages";
import { isErrorMime } from "../mime";
import type { CellId } from "./ids";
@@ -21,21 +22,31 @@ export function getCellLogsForMessage(cell: CellMessage): CellLog[] {
const consoleOutputs: OutputMessage[] = [cell.console].filter(Boolean).flat();
for (const output of consoleOutputs) {
- if (output.mimetype === "text/plain") {
- invariant(typeof output.data === "string", "expected string");
+ // Handle text/plain, text/html, and traceback MIME types
+ const isTextPlain = output.mimetype === "text/plain";
+ const isHtml =
+ output.mimetype === "text/html" ||
+ output.mimetype === "application/vnd.marimo+traceback";
+
+ if (isTextPlain || isHtml) {
const isError =
output.channel === "stderr" || output.channel === "marimo-error";
switch (output.channel) {
case "stdout":
case "stderr":
- case "marimo-error":
+ case "marimo-error": {
+ // Convert data to string and process based on MIME type
+ const rawData = Strings.asString(output.data);
+ const message = isHtml ? parseHtmlContent(rawData) : rawData;
+
logs.push({
timestamp: output.timestamp || Date.now(),
level: isError ? "stderr" : "stdout",
- message: output.data,
+ message: message,
cellId: cell.cell_id as CellId,
});
break;
+ }
default:
break;
}