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: include mo.sidebar() when using Grid layout (#7308)

Myles Scolnickb8655d63e555
diff --git a/frontend/e2e-tests/layout-grid-with-sidebar.spec.ts b/frontend/e2e-tests/layout-grid-with-sidebar.spec.ts
new file mode 100644
index 0000000000..12a40020f9
--- /dev/null
+++ b/frontend/e2e-tests/layout-grid-with-sidebar.spec.ts
@@ -0,0 +1,33 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+
+import { fileURLToPath } from "node:url";
+import { expect, test } from "@playwright/test";
+import { getAppUrl } from "../playwright.config";
+import { takeScreenshot } from "./helper";
+
+const _filename = fileURLToPath(import.meta.url);
+
+const runUrl = getAppUrl("layout_grid_with_sidebar.py//run");
+
+test("sidebar renders in run mode with grid layout", async ({ page }) => {
+ await page.goto(runUrl);
+ // wait 500ms to render
+ await page.waitForTimeout(500);
+
+ // Verify main content is visible
+ await expect(page.getByText("Grid with Sidebar").last()).toBeVisible();
+ await expect(page.getByText("Main Content Area").last()).toBeVisible();
+
+ // Verify sidebar content is visible
+ await expect(page.getByText("Sidebar Title").first()).toBeVisible();
+ await expect(
+ page.getByText("This sidebar should be visible in run mode").first(),
+ ).toBeVisible();
+
+ // Verify nav menu items are visible
+ await expect(page.getByText("Section 1").first()).toBeVisible();
+ await expect(page.getByText("Section 2").first()).toBeVisible();
+ await expect(page.getByText("Section 3").first()).toBeVisible();
+
+ await takeScreenshot(page, _filename);
+});
diff --git a/frontend/e2e-tests/py/layout_grid_with_sidebar.py b/frontend/e2e-tests/py/layout_grid_with_sidebar.py
new file mode 100644
index 0000000000..d720a92ee0
--- /dev/null
+++ b/frontend/e2e-tests/py/layout_grid_with_sidebar.py
@@ -0,0 +1,62 @@
+# /// script
+# [tool.marimo.runtime]
+# auto_instantiate = true
+# ///
+
+import marimo
+
+__generated_with = "0.18.0"
+app = marimo.App(layout_file="layouts/layout_grid_with_sidebar.grid.json")
+
+
+def _(mo):
+ mo.md("""
+ # Grid with Sidebar
+ """)
+ return
+
+
+def _(mo):
+ mo.md("""
+ ## Main Content Area
+ """)
+ return
+
+
+def _(mo):
+ mo.md("""
+ ### More content here
+ """)
+ return
+
+
+def _(mo):
+ mo.sidebar(
+ [
+ mo.md("# Sidebar Title"),
+ mo.md("This sidebar should be visible in run mode"),
+ mo.nav_menu(
+ {
+ "#section1": "Section 1",
+ "#section2": "Section 2",
+ "#section3": "Section 3",
+ },
+ orientation="vertical",
+ ),
+ ]
+ )
+ return
+
+
+def _():
+ import marimo as mo
+ return (mo,)
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/frontend/e2e-tests/py/layouts/layout_grid_with_sidebar.grid.json b/frontend/e2e-tests/py/layouts/layout_grid_with_sidebar.grid.json
new file mode 100644
index 0000000000..25af801f08
--- /dev/null
+++ b/frontend/e2e-tests/py/layouts/layout_grid_with_sidebar.grid.json
@@ -0,0 +1,26 @@
+{
+ "type": "grid",
+ "data": {
+ "columns": 24,
+ "rowHeight": 20,
+ "maxWidth": null,
+ "bordered": true,
+ "cells": [
+ {
+ "position": [1, 3, 20, 4]
+ },
+ {
+ "position": [1, 8, 20, 6]
+ },
+ {
+ "position": [1, 15, 20, 4]
+ },
+ {
+ "position": null
+ },
+ {
+ "position": null
+ }
+ ]
+ }
+}
diff --git a/frontend/playwright.config.ts b/frontend/playwright.config.ts
index c297598a9d..208414f6ca 100644
--- a/frontend/playwright.config.ts
+++ b/frontend/playwright.config.ts
@@ -41,12 +41,14 @@ const appToOptions = {
"kitchen_sink.py": { command: "edit" },
"layout_grid.py": { command: "edit" },
"stdin.py": { command: "edit" },
+ "layout_grid_with_sidebar.py": { command: "edit" },
// Custom server for shutdown
"shutdown.py": { command: "edit", port: port() },
// Run
"components.py": { port: port(), command: "run" },
"layout_grid.py//run": { port: port(), command: "run" },
"layout_grid_max_width.py//run": { port: port(), command: "run" },
+ "layout_grid_with_sidebar.py//run": { port: port(), command: "run" },
"output.py//run": {
port: port(),
command: "run",
diff --git a/frontend/src/components/editor/renderers/grid-layout/grid-layout.tsx b/frontend/src/components/editor/renderers/grid-layout/grid-layout.tsx
index 4a8145b3b9..1e02c06218 100644
--- a/frontend/src/components/editor/renderers/grid-layout/grid-layout.tsx
+++ b/frontend/src/components/editor/renderers/grid-layout/grid-layout.tsx
@@ -249,6 +249,8 @@ export const GridLayoutRenderer: React.FC<Props> = ({
</ReactGridLayout>
);
+ const notInGrid = cells.filter((cell) => !inGridIds.has(cell.id));
+
if (isReading) {
if (layout.bordered) {
grid = (
@@ -262,10 +264,32 @@ export const GridLayoutRenderer: React.FC<Props> = ({
</div>
);
}
- return grid;
- }
- const notInGrid = cells.filter((cell) => !inGridIds.has(cell.id));
+ const sidebarCells = notInGrid.filter((cell) => isSidebarCell(cell));
+
+ return (
+ <>
+ {grid}
+ {/* Render sidebar outputs even if they are not in grid (hidden) */}
+ <div className="hidden">
+ {sidebarCells.map((cell) => {
+ return (
+ <GridCell
+ key={cell.id}
+ code={cell.code}
+ mode={mode}
+ cellId={cell.id}
+ output={cell.output}
+ status={cell.status}
+ isScrollable={false}
+ hidden={false}
+ />
+ );
+ })}
+ </div>
+ </>
+ );
+ }
if (layout.bordered) {
grid = (
@@ -641,6 +665,14 @@ const GridHoverActions: React.FC<GridHoverActionsProps> = ({
);
};
+function isSidebarCell(cell: CellRuntimeState) {
+ // False-positives are ok here because we rendering these cells in a hidden div
+ return (
+ typeof cell.output?.data === "string" &&
+ cell.output.data.includes("marimo-sidebar")
+ );
+}
+
const SIDE_TO_ICON = {
// We are only showing horizontal sides for now
// top: AlignHorizontalSpaceAroundIcon,