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: loading data in vega charts using 'repeat' (#4396)

Myles Scolnick5d2eab34e498
diff --git a/frontend/src/plugins/impl/vega/__tests__/resolve-data.test.ts b/frontend/src/plugins/impl/vega/__tests__/resolve-data.test.ts
index ad15302357..82a15bc43e 100644
--- a/frontend/src/plugins/impl/vega/__tests__/resolve-data.test.ts
+++ b/frontend/src/plugins/impl/vega/__tests__/resolve-data.test.ts
@@ -129,4 +129,109 @@ describe("resolveVegaSpecData", () => {
await expect(resolveVegaSpecData(spec)).resolves.toEqual(expected);
expect(vegaLoader.load).toHaveBeenCalledTimes(4);
});
+
+ it("correctly resolves nested URL data in spec property", async () => {
+ const spec = asSpec({
+ mark: "point",
+ spec: { data: { url: "http://example.com/data", format: "json" } },
+ });
+ const resolvedData = { some: "data" };
+ vi.spyOn(vegaLoader, "load").mockResolvedValueOnce(resolvedData);
+
+ const expected = {
+ mark: "point",
+ spec: { data: { name: "/data" } },
+ datasets: {
+ "/data": resolvedData,
+ },
+ };
+
+ await expect(resolveVegaSpecData(spec)).resolves.toEqual(expected);
+ expect(vegaLoader.load).toHaveBeenCalledWith("http://example.com/data");
+ });
+
+ it("preserves existing datasets in the spec", async () => {
+ const spec = asSpec({
+ data: { url: "http://example.com/data", format: "json" },
+ datasets: { existing: "dataset" },
+ });
+ const resolvedData = { some: "data" };
+ vi.spyOn(vegaLoader, "load").mockResolvedValueOnce(resolvedData);
+
+ const expected = {
+ ...spec,
+ data: { name: "/data" },
+ datasets: {
+ existing: "dataset",
+ "/data": resolvedData,
+ },
+ };
+
+ await expect(resolveVegaSpecData(spec)).resolves.toEqual(expected);
+ });
+
+ it("handles multiple data URLs with the same pathname", async () => {
+ const spec = asSpec({
+ layer: [
+ { data: { url: "http://example.com/data", format: "json" } },
+ { data: { url: "http://example.com/data", format: "json" } },
+ ],
+ });
+ const resolvedData = { some: "data" };
+ vi.spyOn(vegaLoader, "load").mockResolvedValue(resolvedData);
+
+ const expected = {
+ layer: [{ data: { name: "/data" } }, { data: { name: "/data" } }],
+ datasets: {
+ "/data": resolvedData,
+ },
+ };
+
+ await expect(resolveVegaSpecData(spec)).resolves.toEqual(expected);
+ // Loaded twice, but batching should lower-level
+ expect(vegaLoader.load).toHaveBeenCalledTimes(2);
+ });
+
+ it("handles errors from vegaLoadData", async () => {
+ const spec = asSpec({
+ data: { url: "http://example.com/data", format: "json" },
+ });
+ vi.spyOn(vegaLoader, "load").mockRejectedValue(
+ new Error("Failed to load data"),
+ );
+
+ await expect(resolveVegaSpecData(spec)).rejects.toThrow(
+ "Failed to load data",
+ );
+ });
+
+ it("handles different data formats", async () => {
+ const spec = asSpec({
+ data: { url: "http://example.com/data", format: "csv" },
+ });
+ const resolvedData = { some: "data" };
+ vi.spyOn(vegaLoader, "load").mockResolvedValueOnce(resolvedData);
+
+ const expected = {
+ ...spec,
+ data: { name: "/data" },
+ datasets: {
+ "/data": resolvedData,
+ },
+ };
+
+ await expect(resolveVegaSpecData(spec)).resolves.toEqual(expected);
+ expect(vegaLoader.load).toHaveBeenCalledWith("http://example.com/data");
+ });
+
+ it("returns the input spec if no datasets are resolved", async () => {
+ const spec = asSpec({
+ mark: "point",
+ data: { name: "dataset" },
+ });
+
+ const result = await resolveVegaSpecData(spec);
+ expect(result).toEqual(spec);
+ expect(result).not.toHaveProperty("datasets");
+ });
});
diff --git a/frontend/src/plugins/impl/vega/resolve-data.ts b/frontend/src/plugins/impl/vega/resolve-data.ts
index 1e6b060917..633534e9d3 100644
--- a/frontend/src/plugins/impl/vega/resolve-data.ts
+++ b/frontend/src/plugins/impl/vega/resolve-data.ts
@@ -57,6 +57,13 @@ export async function resolveVegaSpecData(
};
}
+ if ("spec" in spec) {
+ spec = {
+ ...spec,
+ spec: await traverse(spec.spec),
+ };
+ }
+
if (!spec.data) {
return spec;
}
diff --git a/marimo/_smoke_tests/altair/repeat.py b/marimo/_smoke_tests/altair/repeat.py
new file mode 100644
index 0000000000..202503ea98
--- /dev/null
+++ b/marimo/_smoke_tests/altair/repeat.py
@@ -0,0 +1,50 @@
+import marimo
+
+__generated_with = "0.12.4"
+app = marimo.App()
+
+
+def _():
+ ## BUG: interactive, repeated chart does not work with polars data frame, non interactive one works fine.
+ return
+
+
+def _():
+ import marimo as mo
+ import polars as pl
+ import altair as alt
+
+ from datetime import date
+
+ sample_data = pl.DataFrame(
+ {
+ "day": [date(2025, 1, 1), date(2025, 1, 2)],
+ "value1": [10, 9],
+ "value2": [100, 34],
+ }
+ )
+ return alt, date, mo, pl, sample_data
+
+
+def _(alt, mo, sample_data):
+ chart = (
+ alt.Chart(sample_data)
+ .mark_line()
+ .encode(
+ x=alt.X("day:T"),
+ y=alt.Y(alt.repeat("column"), type="quantitative"),
+ )
+ .repeat(column=["value1", "value2"])
+ )
+
+ # expected output is to have two rows with same charts
+
+ mo.vstack([chart, mo.ui.altair_chart(chart)])
+ return (chart,)
+
+
+if __name__ == "__main__":
+ app.run()