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: static pages for tables (#4525)

Myles Scolnick7a4654fcad11
diff --git a/frontend/src/core/static/__tests__/files.test.ts b/frontend/src/core/static/__tests__/files.test.ts
index eeb2567431..e50c096cc9 100644
--- a/frontend/src/core/static/__tests__/files.test.ts
+++ b/frontend/src/core/static/__tests__/files.test.ts
@@ -84,7 +84,7 @@ describe("patchFetch", () => {
});
});
-describe("patchVegaLoader", () => {
+describe("patchVegaLoader - loader.http", () => {
const pathsToTest = [
"virtual-file.json",
"/virtual-file.json",
@@ -128,3 +128,55 @@ describe("patchVegaLoader", () => {
expect(content).toBe('{"key": "value"}');
});
});
+
+describe("patchVegaLoader - loader.load", () => {
+ const pathsToTest = [
+ "virtual-file.json",
+ "/virtual-file.json",
+ "./virtual-file.json",
+ "http://foo.com/virtual-file.json",
+ ];
+
+ it.each(pathsToTest)(
+ "should return file content for virtual files for %s",
+ async (s) => {
+ const virtualFiles = {
+ "/virtual-file.json":
+ "data:application/json;base64,eyJrZXkiOiAidmFsdWUifQ==" as DataURLString,
+ };
+
+ const loader = createLoader();
+ const unpatch = patchVegaLoader(loader, virtualFiles);
+ const content = await loader.load(s);
+ unpatch();
+ expect(content).toBe('{"key": "value"}');
+ },
+ );
+
+ it("should fallback to original load method for non-virtual files", async () => {
+ const loader = createLoader();
+
+ const unpatch = patchVegaLoader(loader, {});
+ const content = await loader.load(remoteURL);
+ unpatch(); // Restore the original load function
+
+ expect(content).toBe("Remote content");
+ });
+
+ it("should work with data URIs", async () => {
+ const loader = createLoader();
+ const unpatch = patchVegaLoader(loader, {});
+ const content = await loader.load(
+ "data:application/json;base64,eyJrZXkiOiAidmFsdWUifQ==",
+ );
+ unpatch();
+ expect(content).toBe('{"key": "value"}');
+ });
+
+ it("should handle missing virtual files gracefully in loader.load", async () => {
+ const loader = createLoader();
+ const unpatch = patchVegaLoader(loader, {});
+ await expect(loader.load("/non-existent-file.json")).rejects.toThrow();
+ unpatch();
+ });
+});
diff --git a/frontend/src/core/static/files.ts b/frontend/src/core/static/files.ts
index b9dc9030e4..80dc309f8f 100644
--- a/frontend/src/core/static/files.ts
+++ b/frontend/src/core/static/files.ts
@@ -3,6 +3,7 @@
import type { StaticVirtualFiles } from "./types";
import { getStaticVirtualFiles } from "./static-state";
import { Logger } from "@/utils/Logger";
+import type { Loader } from "@/plugins/impl/vega/vega-loader";
/**
* Patch fetch to resolve virtual files
@@ -70,18 +71,29 @@ function getContentType(fileName: string): string {
}
export function patchVegaLoader(
- loader: {
- http: (url: string) => Promise<string>;
- },
+ loader: Loader,
files: StaticVirtualFiles = getStaticVirtualFiles(),
) {
const originalHttp = loader.http.bind(loader);
+ const originalLoad = loader.load.bind(loader);
- loader.http = async (url: string) => {
+ function maybeGetVirtualFile(url: string): string | undefined {
const pathname = new URL(url, document.baseURI).pathname;
- if (files[url] || files[pathname]) {
- const base64 = files[url] || files[pathname];
- return await window.fetch(base64).then((r) => r.text());
+ // Few variations to grab the URL.
+ // This can happen if a static file was open at file:// or https://
+ return (
+ files[url] ||
+ files[withoutLeadingDot(url)] ||
+ files[pathname] ||
+ files[withoutLeadingDot(pathname)]
+ );
+ }
+
+ loader.http = async (url: string) => {
+ const vfile = maybeGetVirtualFile(url);
+ if (vfile) {
+ // If the file is a virtual file, fetch it
+ return await window.fetch(vfile).then((r) => r.text());
}
try {
@@ -96,7 +108,31 @@ export function patchVegaLoader(
}
};
+ loader.load = async (url: string) => {
+ const vfile = maybeGetVirtualFile(url);
+ if (vfile) {
+ // If the file is a virtual file, fetch it
+ return await window.fetch(vfile).then((r) => r.text());
+ }
+
+ try {
+ return await originalLoad(url);
+ } catch (error) {
+ // If it's a data URL, just return the data
+ if (url.startsWith("data:")) {
+ return await window.fetch(url).then((r) => r.text());
+ }
+ // Re-throw the error
+ throw error;
+ }
+ };
+
return () => {
loader.http = originalHttp;
+ loader.load = originalLoad;
};
}
+
+function withoutLeadingDot(path: string): string {
+ return path.startsWith(".") ? path.slice(1) : path;
+}
diff --git a/frontend/src/plugins/impl/DataTablePlugin.tsx b/frontend/src/plugins/impl/DataTablePlugin.tsx
index eec2061069..e1c118d74a 100644
--- a/frontend/src/plugins/impl/DataTablePlugin.tsx
+++ b/frontend/src/plugins/impl/DataTablePlugin.tsx
@@ -10,7 +10,6 @@ import { Labeled } from "./common/labeled";
import { Alert, AlertTitle } from "@/components/ui/alert";
import { rpc } from "../core/rpc";
import { createPlugin } from "../core/builder";
-import { vegaLoadData } from "./vega/loader";
import { Banner } from "./common/error-banner";
import { ColumnChartSpecModel } from "@/components/data-table/chart-spec-model";
import { ColumnChartContext } from "@/components/data-table/column-summary";
@@ -32,10 +31,6 @@ import type {
import useEvent from "react-use-event-hook";
import { Functions } from "@/utils/functions";
import { ConditionSchema, type ConditionType } from "./data-frames/schema";
-import {
- type ColumnFilterValue,
- filterToFilterCondition,
-} from "@/components/data-table/filters";
import React from "react";
import { TooltipProvider } from "@radix-ui/react-tooltip";
import { Arrays } from "@/utils/arrays";
@@ -51,6 +46,12 @@ import { Button } from "@/components/ui/button";
import { Table2Icon } from "lucide-react";
import { TablePanel } from "@/components/data-table/chart-transforms/chart-transforms";
import { getFeatureFlag } from "@/core/config/feature-flag";
+import {
+ filterToFilterCondition,
+ type ColumnFilterValue,
+} from "@/components/data-table/filters";
+import { vegaLoadData } from "./vega/loader";
+import { isStaticNotebook } from "@/core/static/static-state";
type CsvURL = string;
type TableData<T> = T[] | CsvURL;
@@ -392,7 +393,10 @@ export const LoadingDataTableComponent = memo(
// We still want to run the search,
// so the backend knows the current state for selection
// see https://github.com/marimo-team/marimo/issues/2756
- void searchResultsPromise;
+ // But we should catch errors; this may happen for static exports.
+ void searchResultsPromise.catch((error) => {
+ Logger.error(error);
+ });
} else {
const searchResults = await searchResultsPromise;
tableData = searchResults.data;
@@ -443,9 +447,9 @@ export const LoadingDataTableComponent = memo(
// Column summaries
const { data: columnSummaries, error: columnSummariesError } = useAsyncData<
ColumnSummaries<T>
- >(() => {
+ >(async () => {
if (props.totalRows === 0 || !props.showColumnSummaries) {
- return Promise.resolve({ data: null, summaries: [] });
+ return { data: null, summaries: [] };
}
return props.get_column_summaries({});
}, [
@@ -480,7 +484,7 @@ export const LoadingDataTableComponent = memo(
let errorComponent: React.ReactNode = null;
if (error) {
Logger.error(error);
- errorComponent = (
+ errorComponent = !isStaticNotebook() && (
<Alert variant="destructive" className="mb-2">
<AlertTitle>Error</AlertTitle>
<div className="text-md">