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.

fix: mo.image_compare scales images to given width/height (#10500)

Wally5b53f7e492bb
diff --git a/frontend/src/plugins/impl/image-comparison/ImageComparisonComponent.tsx b/frontend/src/plugins/impl/image-comparison/ImageComparisonComponent.tsx
index 547bc7c008..e34015cddf 100644
--- a/frontend/src/plugins/impl/image-comparison/ImageComparisonComponent.tsx
+++ b/frontend/src/plugins/impl/image-comparison/ImageComparisonComponent.tsx
@@ -49,6 +49,18 @@ const ImageComparisonComponent: React.FC<ImageComparisonData> = ({
minHeight: "2rem",
};
+ // The slider's width="100%" images resolve against its shadow-DOM wrappers (circular),
+ // so size the images explicitly and letterbox with object-fit to keep them aligned.
+ // Treat empty strings as "not provided" (normalize_dimension passes them through).
+ const imageStyle: React.CSSProperties | undefined =
+ width || height
+ ? {
+ width: width || undefined,
+ height: height || undefined,
+ objectFit: "contain",
+ }
+ : undefined;
+
// If an image fails to load, the slider collapses to nothing; surface a
// visible error instead of silently rendering an empty output.
if (failedSrcs.size > 0) {
@@ -67,12 +79,18 @@ const ImageComparisonComponent: React.FC<ImageComparisonData> = ({
return (
<div style={containerStyle}>
- <ImgComparisonSlider value={value} direction={direction}>
+ <ImgComparisonSlider
+ value={value}
+ direction={direction}
+ // inline-block leaves a baseline gap below the slider; align to top.
+ style={{ verticalAlign: "top" }}
+ >
<img
slot="first"
src={beforeSrc}
alt="Before"
width="100%"
+ style={imageStyle}
onError={() => handleError(beforeSrc)}
/>
<img
@@ -80,6 +98,7 @@ const ImageComparisonComponent: React.FC<ImageComparisonData> = ({
src={afterSrc}
alt="After"
width="100%"
+ style={imageStyle}
onError={() => handleError(afterSrc)}
/>
</ImgComparisonSlider>
diff --git a/frontend/src/plugins/impl/image-comparison/__tests__/ImageComparisonComponent.test.tsx b/frontend/src/plugins/impl/image-comparison/__tests__/ImageComparisonComponent.test.tsx
index 198fde8e73..acedab40fe 100644
--- a/frontend/src/plugins/impl/image-comparison/__tests__/ImageComparisonComponent.test.tsx
+++ b/frontend/src/plugins/impl/image-comparison/__tests__/ImageComparisonComponent.test.tsx
@@ -68,4 +68,62 @@ describe("ImageComparisonComponent", () => {
expect(error?.textContent).toContain("…");
expect(error?.textContent).not.toContain("A".repeat(200));
});
+
+ it("sizes both images explicitly when dimensions are given", () => {
+ const { getAllByAltText } = render(
+ <ImageComparisonComponent {...baseProps} width="300px" height="300px" />,
+ );
+
+ for (const img of getAllByAltText(/Before|After/)) {
+ expect((img as HTMLElement).style.width).toBe("300px");
+ expect((img as HTMLElement).style.height).toBe("300px");
+ // Letterbox identically so the slider's clip stays aligned.
+ expect((img as HTMLElement).style.objectFit).toBe("contain");
+ }
+ });
+
+ it("applies a single dimension to both images when only one is given", () => {
+ const { getAllByAltText } = render(
+ <ImageComparisonComponent {...baseProps} width="300px" />,
+ );
+
+ for (const img of getAllByAltText(/Before|After/)) {
+ expect((img as HTMLElement).style.width).toBe("300px");
+ expect((img as HTMLElement).style.objectFit).toBe("contain");
+ }
+ });
+
+ it("applies height-only sizing to both images", () => {
+ const { getAllByAltText } = render(
+ <ImageComparisonComponent {...baseProps} height="300px" />,
+ );
+
+ for (const img of getAllByAltText(/Before|After/)) {
+ expect((img as HTMLElement).style.height).toBe("300px");
+ expect((img as HTMLElement).style.objectFit).toBe("contain");
+ }
+ });
+
+ it("treats empty-string dimensions as not provided", () => {
+ const { getAllByAltText } = render(
+ <ImageComparisonComponent {...baseProps} width="" height="" />,
+ );
+
+ for (const img of getAllByAltText(/Before|After/)) {
+ expect((img as HTMLElement).style.width).toBe("");
+ expect((img as HTMLElement).style.height).toBe("");
+ }
+ });
+
+ it("leaves images unstyled when no dimensions are given", () => {
+ const { getAllByAltText } = render(
+ <ImageComparisonComponent {...baseProps} />,
+ );
+
+ for (const img of getAllByAltText(/Before|After/)) {
+ // No explicit sizing: the slider keeps its intrinsic-size behavior.
+ expect((img as HTMLElement).style.width).toBe("");
+ expect((img as HTMLElement).style.height).toBe("");
+ }
+ });
});