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 { atomWithStorage } from "jotai/utils";
import { z } from "zod";
import type { CellId } from "@/core/cells/ids";
import { Logger } from "@/utils/Logger";
import { NotebookScopedLocalStorage } from "@/utils/storage/typed";
import { capitalize } from "@/utils/strings";
import type { TypedString } from "@/utils/typed";
import { generateUUID } from "@/utils/uuid";
import { ChartSchema, type ChartSchemaType } from "./schemas";
import type { ChartType } from "./types";
export type TabName = TypedString<"TabName">;
export type TabId = TypedString<"TabId">;
export const KEY = "marimo:charts:v2";
interface TabStorage {
id: TabId;
tabName: TabName; // unique within cell
chartType: ChartType;
config: ChartSchemaType;
}
type TabStorageMap = Map<CellId, TabStorage[]>;
const TabStorageSchema = z
.object({
id: z.string().optional(),
tabName: z.string().transform((name) => name as TabName),
chartType: z.string().transform((type) => type as ChartType),
config: ChartSchema,
})
// Stored tabs without an id use their name, which is unique within a cell.
.transform((tab) => ({ ...tab, id: (tab.id ?? tab.tabName) as TabId }));
const TabStorageEntriesSchema = z.array(
z.tuple([
z.string().transform((name) => name as CellId),
z.array(TabStorageSchema),
]),
);
const storage = new NotebookScopedLocalStorage(
KEY,
TabStorageEntriesSchema,
() => [],
);
// Custom storage adapter to ensure objects are serialized as maps
const mapStorage = {
getItem: (key: string): TabStorageMap => {
try {
const value = storage.get(key);
return new Map(value);
} catch (error) {
Logger.warn("Error getting chart storage", error);
return new Map();
}
},
setItem: (key: string, value: TabStorageMap): void => {
storage.set(key, [...value.entries()]);
},
removeItem: (key: string): void => {
storage.remove(key);
},
};
export const tabsStorageAtom = atomWithStorage<TabStorageMap>(
KEY,
new Map(),
mapStorage,
);
export function createTabId(): TabId {
return generateUUID() as TabId;
}
/**
* Convenience function to get the tab name for a given tab number and chart type
*/
export function getChartTabName(tabNum: number, chartType: ChartType) {
const capitalizedChartType = capitalize(chartType);
if (tabNum === 0) {
return `${capitalizedChartType} Chart` as TabName;
}
return `${capitalizedChartType} Chart ${tabNum + 1}` as TabName;
}
const initialStorageValue = mapStorage.getItem(KEY);
/**
* Returns true if the cell has a chart
*/
export function hasChart(cellId: CellId) {
const tabs = initialStorageValue.get(cellId);
if (!tabs) {
return false;
}
return tabs.length > 0;
}