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: allow removing Grid layout (#1048)

Myles Scolnickdf3e76f808e9
diff --git a/frontend/src/components/editor/renderers/cells-renderer.tsx b/frontend/src/components/editor/renderers/cells-renderer.tsx
index 43f9ebf48c..766785851a 100644
--- a/frontend/src/components/editor/renderers/cells-renderer.tsx
+++ b/frontend/src/components/editor/renderers/cells-renderer.tsx
@@ -4,8 +4,7 @@ import React, { PropsWithChildren, memo } from "react";
import { cellRendererPlugins } from "./plugins";
import { AppConfig } from "@/core/config/config-schema";
import { AppMode } from "@/core/mode";
-import { layoutDataAtom, layoutViewAtom } from "@/core/layout/layout";
-import { useAtom } from "jotai";
+import { useLayoutActions, useLayoutState } from "@/core/layout/layout";
interface Props {
appConfig: AppConfig;
@@ -15,15 +14,15 @@ interface Props {
export const CellsRenderer: React.FC<PropsWithChildren<Props>> = memo(
({ appConfig, mode, children }) => {
const notebook = useNotebook();
- const [layoutData, setLayoutData] = useAtom(layoutDataAtom);
- const [layoutType] = useAtom(layoutViewAtom);
+ const { selectedLayout, layoutData } = useLayoutState();
+ const { setCurrentLayoutData } = useLayoutActions();
// Just render children if we are in edit mode
if (mode === "edit") {
return children;
}
- const plugin = cellRendererPlugins.find((p) => p.type === layoutType);
+ const plugin = cellRendererPlugins.find((p) => p.type === selectedLayout);
// Just render children if there is no plugin
if (!plugin) {
@@ -38,8 +37,8 @@ export const CellsRenderer: React.FC<PropsWithChildren<Props>> = memo(
appConfig={appConfig}
mode={mode}
cells={cells}
- layout={layoutData || plugin.getInitialLayout(cells)}
- setLayout={setLayoutData}
+ layout={layoutData[selectedLayout] || plugin.getInitialLayout(cells)}
+ setLayout={setCurrentLayoutData}
/>
);
diff --git a/frontend/src/components/editor/renderers/layout-select.tsx b/frontend/src/components/editor/renderers/layout-select.tsx
index 1100293a05..fa3df5441e 100644
--- a/frontend/src/components/editor/renderers/layout-select.tsx
+++ b/frontend/src/components/editor/renderers/layout-select.tsx
@@ -1,7 +1,5 @@
/* Copyright 2024 Marimo. All rights reserved. */
import React from "react";
-import { layoutViewAtom } from "@/core/layout/layout";
-import { useAtom } from "jotai";
import {
Select,
SelectContent,
@@ -14,9 +12,11 @@ import {
import { LayoutType } from "./types";
import { SquareIcon, Grid3x3Icon, ListIcon } from "lucide-react";
import { isPyodide } from "@/core/pyodide/utils";
+import { useLayoutActions, useLayoutState } from "@/core/layout/layout";
export const LayoutSelect: React.FC = () => {
- const [layoutType, setLayoutType] = useAtom(layoutViewAtom);
+ const { selectedLayout } = useLayoutState();
+ const { setLayoutView } = useLayoutActions();
const layouts: LayoutType[] = ["vertical", "grid"];
// Layouts are not supported in Pyodide
@@ -27,8 +27,8 @@ export const LayoutSelect: React.FC = () => {
return (
<Select
data-testid="layout-select"
- value={layoutType}
- onValueChange={(v) => setLayoutType(v as LayoutType)}
+ value={selectedLayout}
+ onValueChange={(v) => setLayoutView(v as LayoutType)}
>
<SelectTrigger
className="min-w-[110px] border-border bg-background"
diff --git a/frontend/src/core/App.tsx b/frontend/src/core/App.tsx
index 935b1997b6..b3350e39a9 100644
--- a/frontend/src/core/App.tsx
+++ b/frontend/src/core/App.tsx
@@ -48,8 +48,8 @@ import { CellId, HTMLCellId } from "./cells/ids";
import { CellArray } from "../components/editor/renderers/CellArray";
import { RuntimeState } from "./kernel/RuntimeState";
import { CellsRenderer } from "../components/editor/renderers/cells-renderer";
-import { getSerializedLayout, layoutDataAtom } from "./layout/layout";
-import { useAtom, useAtomValue } from "jotai";
+import { getSerializedLayout, useLayoutState } from "./layout/layout";
+import { useAtom } from "jotai";
import { useRunStaleCells } from "../components/editor/cell/useRunCells";
import { formatAll } from "./codemirror/format";
import { cn } from "@/utils/cn";
@@ -69,7 +69,7 @@ export const App: React.FC<AppProps> = ({ userConfig, appConfig }) => {
const [filename, setFilename] = useFilename();
const [lastSavedNotebook, setLastSavedNotebook] =
useState<LastSavedNotebook>();
- const layoutData = useAtomValue(layoutDataAtom);
+ const layout = useLayoutState();
const { openModal, closeModal, openAlert } = useImperativeModal();
const isEditing = viewState.mode === "edit";
@@ -126,7 +126,7 @@ export const App: React.FC<AppProps> = ({ userConfig, appConfig }) => {
const codes = cells.map((cell) => cell.code);
const cellNames = cells.map((cell) => cell.name);
const configs = cells.map((cell) => cell.config);
- const needsSave = notebookNeedsSave(notebook, layoutData, lastSavedNotebook);
+ const needsSave = notebookNeedsSave(notebook, layout, lastSavedNotebook);
// Save the notebook with the given filename
const saveNotebook = useEvent((filename: string, userInitiated: boolean) => {
@@ -165,7 +165,7 @@ export const App: React.FC<AppProps> = ({ userConfig, appConfig }) => {
names: cellNames,
codes,
configs,
- layout: layoutData,
+ layout,
});
});
});
diff --git a/frontend/src/core/cells/cells.ts b/frontend/src/core/cells/cells.ts
index efec8d719b..649c52a35e 100644
--- a/frontend/src/core/cells/cells.ts
+++ b/frontend/src/core/cells/cells.ts
@@ -32,7 +32,7 @@ import { CellLog, getCellLogsForMessage } from "./logs";
import { deserializeBase64ToJson } from "@/utils/json/base64";
import { historyField } from "@codemirror/commands";
import { clamp } from "@/utils/math";
-import { LayoutData } from "../layout/layout";
+import { LayoutState } from "../layout/layout";
import { isEqual } from "lodash-es";
/**
@@ -78,7 +78,7 @@ export interface LastSavedNotebook {
codes: string[];
configs: CellConfig[];
names: string[];
- layout: LayoutData;
+ layout: LayoutState;
}
/**
@@ -735,7 +735,7 @@ export function notebookIsRunning(state: NotebookState) {
export function notebookNeedsSave(
state: NotebookState,
- layout: LayoutData,
+ layout: LayoutState,
lastSavedNotebook: LastSavedNotebook | undefined,
) {
if (!lastSavedNotebook) {
@@ -750,7 +750,8 @@ export function notebookNeedsSave(
!arrayShallowEquals(codes, lastSavedNotebook.codes) ||
!arrayShallowEquals(configs, lastSavedNotebook.configs) ||
!arrayShallowEquals(names, lastSavedNotebook.names) ||
- !isEqual(layout, lastSavedNotebook.layout)
+ !isEqual(layout.selectedLayout, lastSavedNotebook.layout.selectedLayout) ||
+ !isEqual(layout.layoutData, lastSavedNotebook.layout.layoutData)
);
}
diff --git a/frontend/src/core/layout/layout.ts b/frontend/src/core/layout/layout.ts
index e4abe45e29..11984b5435 100644
--- a/frontend/src/core/layout/layout.ts
+++ b/frontend/src/core/layout/layout.ts
@@ -1,48 +1,102 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { GridLayout } from "@/components/editor/renderers/grid-layout/types";
import { LayoutType } from "@/components/editor/renderers/types";
+import { atom, useAtomValue, useSetAtom } from "jotai";
+import { createReducer } from "@/utils/createReducer";
+import { useMemo } from "react";
import { cellRendererPlugins } from "@/components/editor/renderers/plugins";
-import { Logger } from "@/utils/Logger";
-import { atom } from "jotai";
import { getNotebook, notebookCells } from "../cells/cells";
import { store } from "../state/jotai";
+import { Logger } from "@/utils/Logger";
export type LayoutData = GridLayout | undefined;
-/**
- * The currently selected layout type to show.
- */
-export const layoutViewAtom = atom<LayoutType>("vertical");
+export interface LayoutState {
+ selectedLayout: LayoutType;
+ layoutData: Partial<Record<LayoutType, LayoutData>>;
+}
-/**
- * The layout data
- */
-export const layoutDataAtom = atom<LayoutData>(undefined);
+export function initialLayoutState(): LayoutState {
+ return {
+ selectedLayout: "vertical",
+ layoutData: {},
+ };
+}
+
+const { reducer, createActions } = createReducer(initialLayoutState, {
+ setLayoutView: (state, payload: LayoutType) => {
+ return {
+ ...state,
+ selectedLayout: payload,
+ };
+ },
+ setLayoutData: (
+ state,
+ payload: { layoutView: LayoutType; data: LayoutData },
+ ) => {
+ return {
+ ...state,
+ selectedLayout: payload.layoutView,
+ layoutData: {
+ ...state.layoutData,
+ [payload.layoutView]: payload.data,
+ },
+ };
+ },
+ setCurrentLayoutData: (state, payload: LayoutData) => {
+ return {
+ ...state,
+ layoutData: {
+ ...state.layoutData,
+ [state.selectedLayout]: payload,
+ },
+ };
+ },
+});
+
+const layoutStateAtom = atom<LayoutState>(initialLayoutState());
+
+export const useLayoutState = () => {
+ return useAtomValue(layoutStateAtom);
+};
+
+export const useLayoutActions = () => {
+ const setState = useSetAtom(layoutStateAtom);
+
+ return useMemo(() => {
+ const actions = createActions((action) => {
+ setState((state) => reducer(state, action));
+ });
+ return actions;
+ }, [setState]);
+};
/**
* Get the serialized layout data, to be used when saving.
*/
export function getSerializedLayout() {
const notebook = getNotebook();
- const layoutData = store.get(layoutDataAtom);
- const layoutViewType = store.get(layoutViewAtom);
+ const { layoutData, selectedLayout } = store.get(layoutStateAtom);
- if (layoutData === undefined) {
+ // Vertical layout has no data, as it is the default.
+ if (selectedLayout === "vertical") {
return undefined;
}
- // Vertical layout has no data, as it is the default.
- if (layoutViewType === "vertical") {
+
+ if (layoutData === undefined) {
return undefined;
}
+
+ const data = layoutData[selectedLayout];
const plugin = cellRendererPlugins.find(
- (plugin) => plugin.type === layoutViewType,
+ (plugin) => plugin.type === selectedLayout,
);
if (plugin === undefined) {
- Logger.error(`Unknown layout type: ${layoutViewType}`);
+ Logger.error(`Unknown layout type: ${selectedLayout}`);
return undefined;
}
return {
- type: layoutViewType,
- data: plugin.serializeLayout(layoutData, notebookCells(notebook)),
+ type: selectedLayout,
+ data: plugin.serializeLayout(data, notebookCells(notebook)),
};
}
diff --git a/frontend/src/core/websocket/useMarimoWebSocket.tsx b/frontend/src/core/websocket/useMarimoWebSocket.tsx
index 132e2ce65e..8f14e5936a 100644
--- a/frontend/src/core/websocket/useMarimoWebSocket.tsx
+++ b/frontend/src/core/websocket/useMarimoWebSocket.tsx
@@ -1,6 +1,6 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { WebSocketClosedReason, WebSocketState } from "./types";
-import { useAtom, useSetAtom } from "jotai";
+import { useAtom } from "jotai";
import { connectionAtom } from "../network/connection";
import { useWebSocket } from "@/core/websocket/useWebSocket";
import { logNever } from "@/utils/assertNever";
@@ -14,7 +14,11 @@ import { CellData } from "../cells/types";
import { createCell } from "../cells/types";
import { useErrorBoundary } from "react-error-boundary";
import { Logger } from "@/utils/Logger";
-import { LayoutData, layoutDataAtom, layoutViewAtom } from "../layout/layout";
+import {
+ LayoutState,
+ initialLayoutState,
+ useLayoutActions,
+} from "../layout/layout";
import { deserializeLayout } from "@/components/editor/renderers/plugins";
import { useVariablesActions } from "../variables/state";
import { toast } from "@/components/ui/use-toast";
@@ -38,7 +42,7 @@ import { createWsUrl } from "./createWsUrl";
export function useMarimoWebSocket(opts: {
sessionId: SessionId;
autoInstantiate: boolean;
- setCells: (cells: CellData[], layout: LayoutData) => void;
+ setCells: (cells: CellData[], layout: LayoutState) => void;
}) {
// Track whether we want to try reconnecting.
const shouldTryReconnecting = useRef<boolean>(true);
@@ -47,8 +51,7 @@ export function useMarimoWebSocket(opts: {
const { handleCellMessage } = useCellActions();
const { setVariables, setMetadata } = useVariablesActions();
- const setLayoutView = useSetAtom(layoutViewAtom);
- const setLayoutData = useSetAtom(layoutDataAtom);
+ const { setLayoutData } = useLayoutActions();
const [connStatus, setConnStatus] = useAtom(connectionAtom);
const { addBanner } = useBannersActions();
const { addPackageAlert } = useAlertActions();
@@ -97,13 +100,14 @@ export function useMarimoWebSocket(opts: {
});
});
- let layoutData: LayoutData = undefined;
+ const layoutState = initialLayoutState();
if (layout) {
- setLayoutView(layout.type);
- layoutData = deserializeLayout(layout.type, layout.data, cells);
- setLayoutData(layoutData);
+ const layoutData = deserializeLayout(layout.type, layout.data, cells);
+ layoutState.selectedLayout = layout.type;
+ layoutState.layoutData[layout.type] = layoutData;
+ setLayoutData({ layoutView: layout.type, data: layoutData });
}
- setCells(cells, layoutData);
+ setCells(cells, layoutState);
// If resumed, we don't need to instantiate the UI elements,
// and we should read in th existing values from the kernel.
diff --git a/marimo/_server/file_manager.py b/marimo/_server/file_manager.py
index b95448ee1f..a7cb636366 100644
--- a/marimo/_server/file_manager.py
+++ b/marimo/_server/file_manager.py
@@ -168,6 +168,11 @@ class AppFileManager:
app_dir, app_name, LayoutConfig(**layout)
)
self.app.update_config({"layout_file": layout_filename})
+ else:
+ # Remove the layout from the config
+ # We don't remove the layout file from the disk to avoid
+ # deleting state that the user might want to keep
+ self.app.update_config({"layout_file": None})
# try to save the app under the name `filename`
contents = codegen.generate_filecontents(
codes,