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: Add support for parsing Date in vega loader (#441)

Myles Scolnick14fd3125ce23
diff --git a/frontend/src/plugins/impl/vega/__tests__/vega.test.ts b/frontend/src/plugins/impl/vega/__tests__/vega.test.ts
new file mode 100644
index 0000000000..61ba11cc9b
--- /dev/null
+++ b/frontend/src/plugins/impl/vega/__tests__/vega.test.ts
@@ -0,0 +1,32 @@
+/* Copyright 2023 Marimo. All rights reserved. */
+import { describe, expect, it, vi } from "vitest";
+import { vegaLoadData, vegaLoader } from "../loader";
+
+describe("vega loader", () => {
+ it("should parse csv data", async () => {
+ const csvData = `
+active,username,id
+2023-08-14T19:28:47Z,akshayka,1994308
+2023-08-14T21:30:17Z,mscolnick,5108954
+`.trim();
+
+ vi.spyOn(vegaLoader, "load").mockReturnValue(Promise.resolve(csvData));
+
+ const data = await vegaLoadData(csvData, { type: "csv", parse: "auto" });
+
+ expect(data).toMatchInlineSnapshot(`
+ [
+ {
+ "active": "2023-08-14T19:28:47.000Z",
+ "id": 1994308,
+ "username": "akshayka",
+ },
+ {
+ "active": "2023-08-14T21:30:17.000Z",
+ "id": 5108954,
+ "username": "mscolnick",
+ },
+ ]
+ `);
+ });
+});
diff --git a/frontend/src/plugins/impl/vega/loader.ts b/frontend/src/plugins/impl/vega/loader.ts
index ee4e93d957..c3832e7f1a 100644
--- a/frontend/src/plugins/impl/vega/loader.ts
+++ b/frontend/src/plugins/impl/vega/loader.ts
@@ -1,8 +1,11 @@
/* Copyright 2023 Marimo. All rights reserved. */
// @ts-expect-error - no types
-import { loader as createLoader, read } from "vega-loader";
+import { loader as createLoader, read, typeParsers } from "vega-loader";
import { DataFormat } from "./types";
+// Augment the typeParsers to support Date
+typeParsers.date = (value: string) => new Date(value).toISOString();
+
export const vegaLoader = createLoader();
export function vegaLoadData(