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.

improvement: show images when possible in the table

Myles Scolnick1a462a4a20ba
diff --git a/frontend/package.json b/frontend/package.json
index 8ea0611b92..14d2474188 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -186,6 +186,7 @@
"@storybook/react": "^8.4.7",
"@storybook/react-vite": "^8.4.7",
"@swc-jotai/react-refresh": "^0.1.1",
+ "@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.0.0",
"@testing-library/react-hooks": "^8.0.1",
"@types/katex": "^0.16.7",
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml
index 57f014e0e4..a2b837310b 100644
--- a/frontend/pnpm-lock.yaml
+++ b/frontend/pnpm-lock.yaml
@@ -454,6 +454,9 @@ importers:
'@swc-jotai/react-refresh':
specifier: ^0.1.1
version: 0.1.1
+ '@testing-library/jest-dom':
+ specifier: ^6.6.3
+ version: 6.6.3
'@testing-library/react':
specifier: ^16.0.0
@@ -3207,6 +3210,10 @@ packages:
resolution: {integrity: sha512-xGGHpBXYSHUUr6XsKBfs85TWlYKpTc37cSBBVrXcib2MkHLboWlkClhWF37JKlDb9KEq3dHs+f2xR7XJEWGBxA==}
engines: {node: '>=14', npm: '>=6', yarn: '>=1'}
+ '@testing-library/[email protected]':
+ resolution: {integrity: sha512-IteBhl4XqYNkM54f4ejhLRJiZNqcSCoXUOG2CPK7qbD322KjQozM4kHQOfkG2oln9b9HTYqs+Sae8vBATubxxA==}
+ engines: {node: '>=14', npm: '>=6', yarn: '>=1'}
+
'@testing-library/[email protected]':
resolution: {integrity: sha512-Aqhl2IVmLt8IovEVarNDFuJDVWVvhnr9/GCU6UUnrYXwgDFF9h2L2o2P9KBni1AST5sT6riAyoukFLyjQUgD/g==}
engines: {node: '>=12'}
@@ -12358,6 +12365,16 @@ snapshots:
lodash: 4.17.21
redent: 3.0.0
+ '@testing-library/[email protected]':
+ dependencies:
+ '@adobe/css-tools': 4.4.0
+ aria-query: 5.3.2
+ chalk: 3.0.0
+ css.escape: 1.5.1
+ dom-accessibility-api: 0.6.3
+ lodash: 4.17.21
+ redent: 3.0.0
+
dependencies:
'@babel/runtime': 7.25.6
diff --git a/frontend/src/__tests__/setup.ts b/frontend/src/__tests__/setup.ts
index 83ffae4acd..a577d684de 100644
--- a/frontend/src/__tests__/setup.ts
+++ b/frontend/src/__tests__/setup.ts
@@ -1,6 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { afterEach } from "vitest";
import { cleanup } from "@testing-library/react";
+import "@testing-library/jest-dom/vitest";
import "blob-polyfill";
// Cleanup after each test case (e.g., clearing jsdom)
diff --git a/frontend/src/components/data-table/__tests__/url-detector.test.tsx b/frontend/src/components/data-table/__tests__/url-detector.test.tsx
new file mode 100644
index 0000000000..4dc652c9ad
--- /dev/null
+++ b/frontend/src/components/data-table/__tests__/url-detector.test.tsx
@@ -0,0 +1,82 @@
+import { render, screen, fireEvent } from "@testing-library/react";
+import { describe, it, expect, vi } from "vitest";
+import { UrlDetector } from "../url-detector";
+
+describe("UrlDetector", () => {
+ it("renders plain text without URLs", () => {
+ render(<UrlDetector text="Hello world" />);
+ expect(screen.getByText("Hello world")).toBeInTheDocument();
+ });
+
+ it("renders regular URLs as clickable links", () => {
+ render(<UrlDetector text="Check https://marimo.io for more" />);
+ const link = screen.getByRole("link");
+ expect(link).toHaveAttribute("href", "https://marimo.io");
+ expect(link).toHaveAttribute("target", "_blank");
+ expect(link).toHaveAttribute("rel", "noopener noreferrer");
+ });
+
+ it("renders multiple URLs in text", () => {
+ render(
+ <UrlDetector text="Visit https://marimo.io and https://github.com/marimo-team" />,
+ );
+ const links = screen.getAllByRole("link");
+ expect(links).toHaveLength(2);
+ expect(links[0]).toHaveAttribute("href", "https://marimo.io");
+ expect(links[1]).toHaveAttribute("href", "https://github.com/marimo-team");
+ });
+
+ it("renders image URLs as images", () => {
+ render(<UrlDetector text="Image: https://example.com/image.png" />);
+ const img = screen.getByRole("img");
+ expect(img).toHaveAttribute("src", "https://example.com/image.png");
+ expect(img).toHaveAttribute("alt", "URL preview");
+ });
+
+ it("renders known image domains as images", () => {
+ render(
+ <UrlDetector text="Avatar: https://avatars.githubusercontent.com/u/123" />,
+ );
+ const img = screen.getByRole("img");
+ expect(img).toHaveAttribute(
+ "src",
+ "https://avatars.githubusercontent.com/u/123",
+ );
+ });
+
+ it("falls back to link when image fails to load", () => {
+ render(<UrlDetector text="Broken image: https://example.com/broken.png" />);
+ const img = screen.getByRole("img");
+
+ // Simulate image load error
+ fireEvent.error(img);
+
+ // Should now be a link
+ const link = screen.getByRole("link");
+ expect(link).toHaveAttribute("href", "https://example.com/broken.png");
+ expect(img).not.toBeInTheDocument();
+ });
+
+ it("handles various image extensions", () => {
+ const extensions = ["png", "jpg", "jpeg", "gif", "webp", "svg", "ico"];
+ extensions.forEach((ext) => {
+ const { container } = render(
+ <UrlDetector text={`Image: https://example.com/image.${ext}`} />,
+ );
+ const img = container.querySelector("img");
+ expect(img).toHaveAttribute("src", `https://example.com/image.${ext}`);
+ });
+ });
+
+ it.skip("prevents event propagation on link clicks", () => {
+ const mockStopPropagation = vi.fn();
+ render(<UrlDetector text="Check https://marimo.io" />);
+ const link = screen.getByRole("link");
+
+ fireEvent.click(link, {
+ stopPropagation: mockStopPropagation,
+ });
+
+ expect(mockStopPropagation).toHaveBeenCalled();
+ });
+});
diff --git a/frontend/src/components/data-table/url-detector.tsx b/frontend/src/components/data-table/url-detector.tsx
index 187d9cb1a6..4e3b8b088a 100644
--- a/frontend/src/components/data-table/url-detector.tsx
+++ b/frontend/src/components/data-table/url-detector.tsx
@@ -1,25 +1,45 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { Events } from "@/utils/events";
+import { useState } from "react";
const urlRegex = /(https?:\/\/\S+)/g;
+const imageRegex = /\.(png|jpe?g|gif|webp|svg|ico)(\?.*)?$/i;
+const knownImageDomains = ["avatars.githubusercontent.com"];
+
+const ImageWithFallback = ({ url }: { url: string }) => {
+ const [error, setError] = useState(false);
+
+ if (error) {
+ return <URLAnchor url={url} />;
+ }
+
+ return (
+ <div className="flex max-h-[20px] overflow-hidden">
+ <img
+ src={url}
+ alt="URL preview"
+ className="object-contain max-h-full max-w-full rounded"
+ onError={() => setError(true)}
+ />
+ </div>
+ );
+};
+
export const UrlDetector = ({ text }: { text: string }) => {
const createMarkup = (text: string) => {
const parts = text.split(urlRegex);
return parts.map((part, index) => {
if (urlRegex.test(part)) {
- return (
- <a
- key={index}
- href={part}
- target="_blank"
- rel="noopener noreferrer"
- onClick={Events.stopPropagation()}
- className="text-link hover:underline"
- >
- {part}
- </a>
- );
+ const isImage =
+ imageRegex.test(part) ||
+ knownImageDomains.some((domain) => part.includes(domain));
+
+ if (isImage) {
+ return <ImageWithFallback key={index} url={part} />;
+ }
+
+ return <URLAnchor key={index} url={part} />;
}
return part;
});
@@ -27,3 +47,17 @@ export const UrlDetector = ({ text }: { text: string }) => {
return <>{createMarkup(text)}</>;
};
+
+const URLAnchor = ({ url }: { url: string }) => {
+ return (
+ <a
+ href={url}
+ target="_blank"
+ rel="noopener noreferrer"
+ onClick={Events.stopPropagation()}
+ className="text-link hover:underline"
+ >
+ {url}
+ </a>
+ );
+};
diff --git a/frontend/tsconfig.json b/frontend/tsconfig.json
index a00af71de8..437e22972a 100644
--- a/frontend/tsconfig.json
+++ b/frontend/tsconfig.json
@@ -36,7 +36,8 @@
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
"types": [
- "vite/client"
+ "vite/client",
+ "@testing-library/jest-dom"
] /* Specify type package names to be included without being referenced in a source file. */,
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */