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
/* Copyright 2026 Marimo. All rights reserved. */
import { Objects } from "@/utils/objects";
import type { DataType, FieldTypes, VegaDataType } from "./vega-loader";
export type ContainerWidth = number | "container";
/**
* Get the container width from a Vega-Lite spec.
*
* For unit specs, `width` is at the top level. For facet/repeat specs,
* `width` is nested inside `spec`. This does not handle hconcat/vconcat and Vega spec
* where the width may be a signal. These cases are covered by
* the CSS fallback `.vega-embed:has(> .chart-wrapper.fit-x)`.
*/
export function getContainerWidth(spec: unknown): ContainerWidth | undefined {
if (typeof spec === "object" && spec !== null) {
if ("width" in spec) {
return spec.width as ContainerWidth | undefined;
}
// Faceted/repeated spec
if ("spec" in spec) {
return getContainerWidth(spec.spec);
}
}
return undefined;
}
export function mergeAsArrays<T>(
left: T | T[] | undefined,
right: T | T[] | undefined,
): T[] {
return [...toArray(left), ...toArray(right)];
}
function toArray<T>(value: T | T[] | undefined): T[] {
if (!value) {
return [];
}
if (Array.isArray(value)) {
return value;
}
return [value];
}
export function getVegaFieldTypes(
types: Record<string, DataType> | undefined | null,
{ parseDates = false }: { parseDates?: boolean } = {},
): FieldTypes | "auto" {
if (!types || Object.keys(types).length === 0) {
// If fieldTypes is provided, use it to parse the data
// Otherwise, infer the data types
return "auto";
}
// Preserve date-only strings for callers such as the data editor.
// Parsing creates Date objects that JSON serializes as full timestamps.
// Charts opt into date parsing for temporal axes.
return Objects.mapValues(types, (type): VegaDataType => {
if (type === "geometry") {
return "string";
}
if (type === "date") {
return parseDates ? "date" : "string";
}
if (type === "time") {
return "string";
}
if (type === "datetime") {
return "date";
}
return type;
});
}