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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: column summaries in wasm (#2802)
Myles Scolnick6622b6a1925d
diff --git a/frontend/src/components/data-table/__test__/__snapshots__/chart-spec-model.test.ts.snap b/frontend/src/components/data-table/__test__/__snapshots__/chart-spec-model.test.ts.snapnew file mode 100644index 0000000000..31cbb2bf39--- /dev/null+++ b/frontend/src/components/data-table/__test__/__snapshots__/chart-spec-model.test.ts.snap@@ -0,0 +1,307 @@+// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html++exports[`ColumnChartSpecModel > snapshot > array 1`] = `+{+ "background": "transparent",+ "config": {+ "axis": {+ "domain": false,+ },+ "view": {+ "stroke": "transparent",+ },+ },+ "data": {+ "values": [+ "a",+ "b",+ "c",+ ],+ },+ "encoding": {+ "color": {+ "condition": {+ "test": "datum["bin_maxbins_10_a_range"] === "null"",+ "value": "#cc4e00",+ },+ "value": "#027864",+ },+ "tooltip": [+ {+ "bin": true,+ "field": "a",+ "format": ".2f",+ "title": "a",+ "type": "nominal",+ },+ {+ "aggregate": "count",+ "format": ",d",+ "title": "Count",+ "type": "quantitative",+ },+ ],+ "x": {+ "axis": null,+ "bin": true,+ "field": "a",+ "scale": {+ "align": 0,+ "paddingInner": 0,+ "paddingOuter": {+ "expr": "length(data('source_0')) == 2 ? 1 : length(data('source_0')) == 3 ? 0.5 : length(data('source_0')) == 4 ? 0 : 0",+ },+ },+ "type": "nominal",+ },+ "y": {+ "aggregate": "count",+ "axis": null,+ "scale": {+ "type": "linear",+ },+ "type": "quantitative",+ },+ },+ "height": 100,+ "mark": {+ "align": "right",+ "color": "#027864",+ "size": {+ "expr": "min(28, 120 / length(data('source_0')) - 1)",+ },+ "type": "bar",+ },+}+`;++exports[`ColumnChartSpecModel > snapshot > csv data 1`] = `+{+ "background": "transparent",+ "config": {+ "axis": {+ "domain": false,+ },+ "view": {+ "stroke": "transparent",+ },+ },+ "data": {+ "values": [+ {+ "a": 1,+ "b": 2,+ "c": 3,+ },+ {+ "a": 4,+ "b": 5,+ "c": 6,+ },+ ],+ },+ "encoding": {+ "color": {+ "condition": {+ "test": "datum["bin_maxbins_10_a_range"] === "null"",+ "value": "#cc4e00",+ },+ "value": "#027864",+ },+ "tooltip": [+ {+ "bin": true,+ "field": "a",+ "format": ".2f",+ "title": "a",+ "type": "nominal",+ },+ {+ "aggregate": "count",+ "format": ",d",+ "title": "Count",+ "type": "quantitative",+ },+ ],+ "x": {+ "axis": null,+ "bin": true,+ "field": "a",+ "scale": {+ "align": 0,+ "paddingInner": 0,+ "paddingOuter": {+ "expr": "length(data('data_0')) == 2 ? 1 : length(data('data_0')) == 3 ? 0.5 : length(data('data_0')) == 4 ? 0 : 0",+ },+ },+ "type": "nominal",+ },+ "y": {+ "aggregate": "count",+ "axis": null,+ "scale": {+ "type": "linear",+ },+ "type": "quantitative",+ },+ },+ "height": 100,+ "mark": {+ "align": "right",+ "color": "#027864",+ "size": {+ "expr": "min(28, 120 / length(data('data_0')) - 1)",+ },+ "type": "bar",+ },+}+`;++exports[`ColumnChartSpecModel > snapshot > csv string 1`] = `+{+ "background": "transparent",+ "config": {+ "axis": {+ "domain": false,+ },+ "view": {+ "stroke": "transparent",+ },+ },+ "data": {+ "values": [+ {+ "a": 1,+ "b": 2,+ "c": 3,+ },+ {+ "a": 4,+ "b": 5,+ "c": 6,+ },+ ],+ },+ "encoding": {+ "color": {+ "condition": {+ "test": "datum["bin_maxbins_10_a_range"] === "null"",+ "value": "#cc4e00",+ },+ "value": "#027864",+ },+ "tooltip": [+ {+ "bin": true,+ "field": "a",+ "format": ".2f",+ "title": "a",+ "type": "nominal",+ },+ {+ "aggregate": "count",+ "format": ",d",+ "title": "Count",+ "type": "quantitative",+ },+ ],+ "x": {+ "axis": null,+ "bin": true,+ "field": "a",+ "scale": {+ "align": 0,+ "paddingInner": 0,+ "paddingOuter": {+ "expr": "length(data('data_0')) == 2 ? 1 : length(data('data_0')) == 3 ? 0.5 : length(data('data_0')) == 4 ? 0 : 0",+ },+ },+ "type": "nominal",+ },+ "y": {+ "aggregate": "count",+ "axis": null,+ "scale": {+ "type": "linear",+ },+ "type": "quantitative",+ },+ },+ "height": 100,+ "mark": {+ "align": "right",+ "color": "#027864",+ "size": {+ "expr": "min(28, 120 / length(data('data_0')) - 1)",+ },+ "type": "bar",+ },+}+`;++exports[`ColumnChartSpecModel > snapshot > url data 1`] = `+{+ "background": "transparent",+ "config": {+ "axis": {+ "domain": false,+ },+ "view": {+ "stroke": "transparent",+ },+ },+ "data": {+ "values": [],+ },+ "encoding": {+ "color": {+ "condition": {+ "test": "datum["bin_maxbins_10_date_range"] === "null"",+ "value": "#cc4e00",+ },+ "value": "#027864",+ },+ "tooltip": [+ {+ "bin": true,+ "field": "date",+ "format": "%Y-%m-%d",+ "title": "date",+ "type": "temporal",+ },+ {+ "aggregate": "count",+ "format": ",d",+ "title": "Count",+ "type": "quantitative",+ },+ ],+ "x": {+ "axis": null,+ "bin": true,+ "field": "date",+ "scale": {+ "align": 0,+ "paddingInner": 0,+ "paddingOuter": {+ "expr": "length(data('data_0')) == 2 ? 1 : length(data('data_0')) == 3 ? 0.5 : length(data('data_0')) == 4 ? 0 : 0",+ },+ },+ "type": "temporal",+ },+ "y": {+ "aggregate": "count",+ "axis": null,+ "type": "quantitative",+ },+ },+ "height": 100,+ "mark": {+ "color": "#027864",+ "type": "bar",+ "width": {+ "expr": "min(28, 120 / length(data('data_0')) - 1)",+ },+ },+}+`;diff --git a/frontend/src/components/data-table/__test__/chart-spec-model.test.ts b/frontend/src/components/data-table/__test__/chart-spec-model.test.tsindex f134b22737..0cb834c014 100644--- a/frontend/src/components/data-table/__test__/chart-spec-model.test.ts+++ b/frontend/src/components/data-table/__test__/chart-spec-model.test.ts@@ -91,4 +91,51 @@ describe("ColumnChartSpecModel", () => {"column\\.with\\[special\\:chars\\]",);});++ describe("snapshot", () => {+ const fieldTypes: FieldTypes = {+ ...mockFieldTypes,+ a: "number",+ };++ it("url data", () => {+ const model = new ColumnChartSpecModel(+ mockData,+ fieldTypes,+ mockSummaries,+ { includeCharts: true },+ );+ expect(model.getHeaderSummary("date").spec).toMatchSnapshot();+ });++ it("csv data", () => {+ const model = new ColumnChartSpecModel(+ `data:text/csv;base64,${btoa("a,b,c\n1,2,3\n4,5,6")}`,+ fieldTypes,+ mockSummaries,+ { includeCharts: true },+ );+ expect(model.getHeaderSummary("a").spec).toMatchSnapshot();+ });++ it("csv string", () => {+ const model = new ColumnChartSpecModel(+ "a,b,c\n1,2,3\n4,5,6",+ fieldTypes,+ mockSummaries,+ { includeCharts: true },+ );+ expect(model.getHeaderSummary("a").spec).toMatchSnapshot();+ });++ it("array", () => {+ const model = new ColumnChartSpecModel(+ ["a", "b", "c"],+ fieldTypes,+ mockSummaries,+ { includeCharts: true },+ );+ expect(model.getHeaderSummary("a").spec).toMatchSnapshot();+ });+ });});diff --git a/frontend/src/components/data-table/chart-spec-model.tsx b/frontend/src/components/data-table/chart-spec-model.tsxindex 444c7395f1..3c44f9c6c5 100644--- a/frontend/src/components/data-table/chart-spec-model.tsx+++ b/frontend/src/components/data-table/chart-spec-model.tsx@@ -5,20 +5,12 @@ import type { ColumnHeaderSummary, FieldTypes } from "./types";import { asURL } from "@/utils/url";import { parseCsvData } from "@/plugins/impl/vega/loader";import { logNever } from "@/utils/assertNever";+import type { TopLevelSpec } from "vega-lite";const MAX_BAR_HEIGHT = 24; // pxconst MAX_BAR_WIDTH = 28; // pxconst CONTAINER_WIDTH = 120; // pxconst PAD = 1; // px-const VARIABLE_WIDTH = `min(${MAX_BAR_WIDTH}, ${CONTAINER_WIDTH} / length(data('source_0')) - ${PAD})`;--const scale = {- align: 0,- paddingInner: 0,- paddingOuter: {- expr: "length(data('source_0')) == 2 ? 1 : length(data('source_0')) == 3 ? 0.5 : length(data('source_0')) == 4 ? 0 : 0",- },-};export class ColumnChartSpecModel<T> {private columnSummaries = new Map<string | number, ColumnHeaderSummary>();@@ -27,6 +19,9 @@ export class ColumnChartSpecModel<T> {includeCharts: false,});+ private dataSpec: TopLevelSpec["data"];+ private sourceName: "data_0" | "source_0";+constructor(private readonly data: T[] | string,private readonly fieldTypes: FieldTypes,@@ -35,14 +30,38 @@ export class ColumnChartSpecModel<T> {includeCharts: boolean;},) {- // Support CSV data as a string- const isCsv =- typeof this.data === "string" &&- !this.data.startsWith("./@file") &&- !this.data.startsWith("/@file") &&- !this.data.startsWith("data:text/csv");- if (isCsv) {- this.data = parseCsvData(this.data) as T[];+ // Data may come in from a few different sources:+ // - A URL+ // - A CSV data URI (e.g. "data:text/csv;base64,...")+ // - A CSV string (e.g. "a,b,c\n1,2,3\n4,5,6")+ // - An array of objects+ // For each case, we need to set up the data spec and source name appropriately.+ // If its a file, the source name will be "source_0", otherwise it will be "data_0".+ // We have a few snapshot tests to ensure that the spec is correct for each case.+ if (typeof this.data === "string") {+ if (this.data.startsWith("./@file") || this.data.startsWith("/@file")) {+ this.dataSpec = {+ url: asURL(this.data).href,+ };+ this.sourceName = "source_0";+ } else if (this.data.startsWith("data:text/csv;base64,")) {+ const decoded = atob(this.data.split(",")[1]);+ this.dataSpec = {+ values: parseCsvData(decoded) as T[],+ };+ this.sourceName = "data_0";+ } else {+ // Assume it's a CSV string+ this.dataSpec = {+ values: parseCsvData(this.data) as T[],+ };+ this.sourceName = "data_0";+ }+ } else {+ this.dataSpec = {+ values: this.data,+ };+ this.sourceName = "source_0";}this.columnSummaries = new Map(summaries.map((s) => [s.column, s]));@@ -61,11 +80,7 @@ export class ColumnChartSpecModel<T> {return null;}const base: Omit<TopLevelFacetedUnitSpec, "mark"> = {- data: (typeof this.data === "string"- ? {- url: asURL(this.data).href,- }- : { values: this.data }) as TopLevelFacetedUnitSpec["data"],+ data: this.dataSpec as TopLevelFacetedUnitSpec["data"],background: "transparent",config: {view: {@@ -87,6 +102,9 @@ export class ColumnChartSpecModel<T> {// escape colons in column namescolumn = column.replaceAll(":", "\\:");+ const scale = this.getScale();+ const variableWidth = `min(${MAX_BAR_WIDTH}, ${CONTAINER_WIDTH} / length(data('${this.sourceName}')) - ${PAD})`;+switch (type) {case "date":case "datetime":@@ -96,7 +114,7 @@ export class ColumnChartSpecModel<T> {mark: {type: "bar",color: mint.mint11,- width: { expr: VARIABLE_WIDTH },+ width: { expr: variableWidth },},encoding: {x: {@@ -145,7 +163,7 @@ export class ColumnChartSpecModel<T> {mark: {type: "bar",color: mint.mint11,- size: { expr: VARIABLE_WIDTH },+ size: { expr: variableWidth },align: "right",},encoding: {@@ -253,4 +271,14 @@ export class ColumnChartSpecModel<T> {return null;}}++ private getScale() {+ return {+ align: 0,+ paddingInner: 0,+ paddingOuter: {+ expr: `length(data('${this.sourceName}')) == 2 ? 1 : length(data('${this.sourceName}')) == 3 ? 0.5 : length(data('${this.sourceName}')) == 4 ? 0 : 0`,+ },+ };+ }}