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 altair not setting to full-width when width: container (#8969)

Shahmir Varqha98abdc3495d9
diff --git a/frontend/src/plugins/impl/vega/__tests__/utils.test.ts b/frontend/src/plugins/impl/vega/__tests__/utils.test.ts
index 37104d804b..7764fdaa79 100644
--- a/frontend/src/plugins/impl/vega/__tests__/utils.test.ts
+++ b/frontend/src/plugins/impl/vega/__tests__/utils.test.ts
@@ -33,4 +33,72 @@ describe("getContainerWidth", () => {
it("should return undefined when width is explicitly undefined", () => {
expect(getContainerWidth({ width: undefined })).toBeUndefined();
});
+
+ it("should find width in nested facet spec", () => {
+ expect(
+ getContainerWidth({
+ $schema: "https://vega.github.io/schema/vega-lite/v6.json",
+ facet: { column: { field: "Origin", type: "nominal" } },
+ spec: {
+ mark: "point",
+ encoding: {},
+ width: "container",
+ },
+ }),
+ ).toBe("container");
+ });
+
+ it("should find width in nested repeat spec", () => {
+ expect(
+ getContainerWidth({
+ $schema: "https://vega.github.io/schema/vega-lite/v6.json",
+ repeat: { row: ["A", "B"] },
+ spec: {
+ mark: "point",
+ encoding: {},
+ width: "container",
+ },
+ }),
+ ).toBe("container");
+ });
+
+ it("should return undefined for nested spec without width", () => {
+ expect(
+ getContainerWidth({
+ facet: { column: { field: "Origin" } },
+ spec: { mark: "point", encoding: {} },
+ }),
+ ).toBeUndefined();
+ });
+
+ it("should return undefined for hconcat (width on sub-specs)", () => {
+ expect(
+ getContainerWidth({
+ hconcat: [{ width: "container" }, { width: "container" }],
+ }),
+ ).toBeUndefined();
+ });
+
+ it("should return undefined for vconcat (width on sub-specs)", () => {
+ expect(
+ getContainerWidth({
+ vconcat: [{ width: "container" }, { width: "container" }],
+ }),
+ ).toBeUndefined();
+ });
+
+ it("should return undefined for compiled Vega spec (width as signal)", () => {
+ expect(
+ getContainerWidth({
+ $schema: "https://vega.github.io/schema/vega/v6.json",
+ autosize: { contains: "padding", type: "fit-x" },
+ signals: [
+ {
+ name: "width",
+ init: "isFinite(containerSize()[0]) ? containerSize()[0] : 300",
+ },
+ ],
+ }),
+ ).toBeUndefined();
+ });
});
diff --git a/frontend/src/plugins/impl/vega/utils.ts b/frontend/src/plugins/impl/vega/utils.ts
index 093c328618..ecc927d42d 100644
--- a/frontend/src/plugins/impl/vega/utils.ts
+++ b/frontend/src/plugins/impl/vega/utils.ts
@@ -6,13 +6,22 @@ import type { DataType, FieldTypes, VegaDataType } from "./vega-loader";
export type ContainerWidth = number | "container";
/**
- * Get the container width from a VegaLite spec.
- * @param spec - The VegaLite spec.
- * @returns The container width.
+ * Get the container width from a Vega-Lite spec.
+ *
+ * For unit specs, `width` is at the top level. For facet/repeat specs,
+ * `width` is nested inside `spec`. This does not handle hconcat/vconcat and Vega spec
+ * where the width may be a signal. These cases are covered by
+ * the CSS fallback `.vega-embed:has(> .chart-wrapper.fit-x)`.
*/
export function getContainerWidth(spec: unknown): ContainerWidth | undefined {
- if (typeof spec === "object" && spec !== null && "width" in spec) {
- return spec.width as ContainerWidth | undefined;
+ if (typeof spec === "object" && spec !== null) {
+ if ("width" in spec) {
+ return spec.width as ContainerWidth | undefined;
+ }
+ // Faceted/repeated spec
+ if ("spec" in spec) {
+ return getContainerWidth(spec.spec);
+ }
}
return undefined;
}
diff --git a/frontend/src/plugins/impl/vega/vega.css b/frontend/src/plugins/impl/vega/vega.css
index 16c129f3a1..8d069a6776 100644
--- a/frontend/src/plugins/impl/vega/vega.css
+++ b/frontend/src/plugins/impl/vega/vega.css
@@ -4,7 +4,8 @@
max-width: 100%;
}
-.vega-embed[data-container-width="container"] {
+.vega-embed[data-container-width="container"],
+.vega-embed:has(> .chart-wrapper.fit-x) {
width: 100%;
}
diff --git a/marimo/_smoke_tests/altair_examples/container_charts.py b/marimo/_smoke_tests/altair_examples/container_charts.py
index 213cb81674..29e1cc2b56 100644
--- a/marimo/_smoke_tests/altair_examples/container_charts.py
+++ b/marimo/_smoke_tests/altair_examples/container_charts.py
@@ -1,6 +1,6 @@
import marimo
-__generated_with = "0.21.0"
+__generated_with = "0.21.1"
app = marimo.App(width="medium")
with app.setup:
@@ -12,8 +12,34 @@ with app.setup:
@app.cell
def _():
cars = data.cars()
+ base = (
+ alt.Chart(cars)
+ .mark_point()
+ .encode(
+ x="Horsepower",
+ y="Miles_per_Gallon",
+ color="Origin",
+ )
+ )
+ # 1. Unit chart — width at top level, getContainerWidth works
+ base.properties(width="container")
+ return base, cars
+
+
+def _(base):
+ # facet — width nested under spec
+ base.properties(width="container").facet(column="Origin:N")
+ return
+
+
+def _(cars):
+ # with vegafusion enabled. This will use vega instead of vega-lite
+ # The chart should be fully expanded
+ alt.data_transformers.enable("vegafusion")
- chart = (
+ _base = (
alt.Chart(cars)
.mark_point()
.encode(
@@ -21,16 +47,23 @@ def _():
y="Miles_per_Gallon",
color="Origin",
)
- .interactive()
+ .properties(width="container")
)
- chart.properties(width="container")
- return (chart,)
+ _base
+ return
+
+
+def _(base):
+ # This should not stretch the entire width
+ base.properties(autosize=alt.AutoSizeParams(type="fit-x"))
+ return
@app.cell
-def _(chart):
+def _(base):
# Broken
- chart.properties(height="container")
+ base.properties(height="container")
return