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: preload vega data to avoid flicker in vegaembed (#254)

Myles Scolnicka69efe77b78c
diff --git a/frontend/src/plugins/impl/DataTablePlugin.tsx b/frontend/src/plugins/impl/DataTablePlugin.tsx
index 082cab9edc..edb34089c9 100644
--- a/frontend/src/plugins/impl/DataTablePlugin.tsx
+++ b/frontend/src/plugins/impl/DataTablePlugin.tsx
@@ -1,9 +1,6 @@
/* Copyright 2023 Marimo. All rights reserved. */
import { useMemo } from "react";
import { z } from "zod";
-// @ts-expect-error - no types
-import { loader as createLoader, read } from "vega-loader";
-
import { DataTable } from "../../components/data-table/data-table";
import {
generateColumns,
@@ -14,6 +11,7 @@ import { useAsyncData } from "@/hooks/useAsyncData";
import { Alert, AlertTitle } from "@/components/ui/alert";
import { rpc } from "../core/rpc";
import { createPlugin } from "../core/builder";
+import { vegaLoadData } from "./vega/loader";
/**
* Arguments for a data table
@@ -38,8 +36,6 @@ type Functions = {
type S = Array<string | number>;
-const loader = createLoader();
-
export const DataTablePlugin = createPlugin<S>("marimo-table")
.withData(
z.object({
@@ -90,10 +86,7 @@ const LoadingDataTableComponent = (
props: DataTableProps & { data: string }
) => {
const { data, loading, error } = useAsyncData<unknown[]>(() => {
- return loader.load(props.data).then((csvData: string) => {
- // csv -> json
- return read(csvData, { type: "csv", parse: "auto" });
- });
+ return vegaLoadData(props.data, { type: "csv" });
}, [props.data]);
if (loading && !data) {
diff --git a/frontend/src/plugins/impl/vega/VegaPlugin.tsx b/frontend/src/plugins/impl/vega/VegaPlugin.tsx
index 47e17a76ef..5d947d71f1 100644
--- a/frontend/src/plugins/impl/vega/VegaPlugin.tsx
+++ b/frontend/src/plugins/impl/vega/VegaPlugin.tsx
@@ -13,6 +13,9 @@ import { useDebugMounting, usePropsDidChange } from "@/hooks/debug";
import { debounce } from "lodash-es";
import useEvent from "react-use-event-hook";
import { Logger } from "@/utils/Logger";
+
+import { vegaLoadData } from "./loader";
+import { useAsyncData } from "@/hooks/useAsyncData";
import { fixRelativeUrl } from "./fix-relative-url";
import "./vega.css";
@@ -73,6 +76,61 @@ export const VegaComponent = ({
chartSelection,
fieldSelection,
spec,
+}: VegaComponentProps<T>) => {
+ const { data: resolvedSpec } = useAsyncData(async () => {
+ // We try to resolve the data before passing it to Vega
+ // otherwise it will try to load it internally and flicker
+ // Instead we can handle the loading state ourselves,
+ // and show the previous chart until the new one is ready
+
+ if (!spec || !spec.data) {
+ return spec;
+ }
+
+ if (!("url" in spec.data)) {
+ return spec;
+ }
+
+ // Skip non-relative URLs
+ if (!spec.data.url.startsWith("/")) {
+ return spec;
+ }
+ const url = spec.data.url;
+
+ const data = await vegaLoadData(url, spec.data.format);
+ return {
+ ...spec,
+ data: {
+ name: url,
+ },
+ datasets: {
+ ...spec.datasets,
+ [url]: data,
+ },
+ } as VegaLiteSpec;
+ }, [spec]);
+
+ if (!resolvedSpec) {
+ return null;
+ }
+
+ return (
+ <LoadedVegaComponent
+ value={value}
+ setValue={setValue}
+ chartSelection={chartSelection}
+ fieldSelection={fieldSelection}
+ spec={resolvedSpec}
+ />
+ );
+};
+
+const LoadedVegaComponent = ({
+ value,
+ setValue,
+ chartSelection,
+ fieldSelection,
+ spec,
}: VegaComponentProps<T>): JSX.Element => {
const vegaView = useRef<View>();
const [error, setError] = useState<Error>();
diff --git a/frontend/src/plugins/impl/vega/loader.ts b/frontend/src/plugins/impl/vega/loader.ts
new file mode 100644
index 0000000000..69f06d4ef1
--- /dev/null
+++ b/frontend/src/plugins/impl/vega/loader.ts
@@ -0,0 +1,13 @@
+/* Copyright 2023 Marimo. All rights reserved. */
+// @ts-expect-error - no types
+import { loader as createLoader, read } from "vega-loader";
+import { DataFormat } from "./types";
+
+export const vegaLoader = createLoader();
+
+export function vegaLoadData(url: string, format: DataFormat | undefined) {
+ return vegaLoader.load(url).then((csvData: string) => {
+ // csv -> json
+ return read(csvData, format);
+ });
+}
diff --git a/frontend/src/plugins/impl/vega/types.ts b/frontend/src/plugins/impl/vega/types.ts
index fe3064c9f0..5ed2487dcd 100644
--- a/frontend/src/plugins/impl/vega/types.ts
+++ b/frontend/src/plugins/impl/vega/types.ts
@@ -16,6 +16,7 @@ export type {
SelectionParameter,
SelectionType,
} from "vega-lite/build/src/selection";
+export type { DataFormat } from "vega-lite/build/src/data";
export type VegaLiteUnitSpec = TopLevelUnitSpec<Field>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts
index ac20837b84..ad9e1e4d8b 100644
--- a/frontend/vite.config.ts
+++ b/frontend/vite.config.ts
@@ -54,6 +54,10 @@ export default defineConfig({
target: `http://localhost:${SERVER_PORT}`,
changeOrigin: true,
},
+ "/@file": {
+ target: `http://localhost:${SERVER_PORT}`,
+ changeOrigin: true,
+ },
"/iosocket": {
target: `ws://localhost:${SERVER_PORT}`,
ws: true,