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.

grid layout improvements (#173)

Myles Scolnick6c7053ef8782
diff --git a/frontend/.gitignore b/frontend/.gitignore
index a534798791..30da341fce 100644
--- a/frontend/.gitignore
+++ b/frontend/.gitignore
@@ -2,6 +2,7 @@
/node_modules
/.pnp
.pnp.js
+.swc
# testing
/coverage
diff --git a/frontend/e2e-tests/layout-grid.spec.ts b/frontend/e2e-tests/layout-grid.spec.ts
index fd42e2e821..1e68f12ac3 100644
--- a/frontend/e2e-tests/layout-grid.spec.ts
+++ b/frontend/e2e-tests/layout-grid.spec.ts
@@ -14,7 +14,7 @@ test("can run Grid layout", async ({ page }) => {
await expect(page.getByText("Grid Layout")).toBeVisible();
// Type in search box
- await page.fill("input", "hello");
+ await page.getByRole("textbox").fill("hello");
// Verify dependent output updated
await expect(page.getByText("Searching hello")).toBeVisible();
@@ -46,7 +46,7 @@ test("can edit Grid layout", async ({ page }) => {
await expect(bb1.x).toBeGreaterThan(bb2.x);
// Can still use interactive elements
- await page.fill("input", "hello");
+ await page.getByRole("textbox").fill("hello");
await expect(page.getByText("Searching hello")).toBeVisible();
// Can toggle to Vertical layout
diff --git a/frontend/package.json b/frontend/package.json
index 13fb994278..82b067cf7f 100644
--- a/frontend/package.json
+++ b/frontend/package.json
@@ -128,6 +128,7 @@
"@storybook/react": "^7.4.5",
"@storybook/react-vite": "^7.4.5",
"@storybook/testing-library": "^0.2.1",
+ "@swc-jotai/react-refresh": "^0.1.0",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.5.1",
diff --git a/frontend/pnpm-lock.yaml b/frontend/pnpm-lock.yaml
index c7ebbf71ec..9ed8ba9c7e 100644
--- a/frontend/pnpm-lock.yaml
+++ b/frontend/pnpm-lock.yaml
@@ -268,6 +268,9 @@ devDependencies:
'@storybook/testing-library':
specifier: ^0.2.1
version: 0.2.1
+ '@swc-jotai/react-refresh':
+ specifier: ^0.1.0
+ version: 0.1.0
'@testing-library/jest-dom':
specifier: ^5.17.0
version: 5.17.0
@@ -5479,6 +5482,10 @@ packages:
file-system-cache: 2.3.0
dev: true
+ /@swc-jotai/[email protected]:
+ resolution: {integrity: sha512-CWV4W06GdfQo3cW3CoUM2fp7avH4Z/suR7SvECNJnQbr6Sa9U3p7o10nXjNv2zT6z09bbNUPM55HBw5Nwd24Iw==}
+ dev: true
+
resolution: {integrity: sha512-LVCZQ2yGrX2678uMvW66IF1bzcOMqiABi+ioNDnJtAIsE/zRVMEYp1ivbOrH32FmPplBby6CGgJIOT3P4VaP1g==}
engines: {node: '>=10'}
diff --git a/frontend/src/core/config/config.ts b/frontend/src/core/config/config.ts
index 973521d180..b79fabc751 100644
--- a/frontend/src/core/config/config.ts
+++ b/frontend/src/core/config/config.ts
@@ -1,6 +1,7 @@
/* Copyright 2023 Marimo. All rights reserved. */
import { z } from "zod";
import { assertExists } from "../../utils/assertExists";
+import { Logger } from "@/utils/Logger";
export const UserConfigSchema = z
.object({
@@ -58,7 +59,13 @@ export function parseAppConfig() {
export function parseUserConfig() {
try {
- return UserConfigSchema.parse(JSON.parse(getConfig("user")));
+ const parsed = UserConfigSchema.parse(JSON.parse(getConfig("user")));
+ for (const [key, value] of Object.entries(parsed.experimental)) {
+ if (value === true) {
+ Logger.log(`🧪 Experimental feature "${key}" is enabled.`);
+ }
+ }
+ return parsed;
} catch (error) {
throw new Error(
`Marimo got an unexpected value in the configuration file: ${error}`
diff --git a/frontend/src/editor/renderers/grid-layout/grid-layout.tsx b/frontend/src/editor/renderers/grid-layout/grid-layout.tsx
index 3295c70592..37b5bb1fe0 100644
--- a/frontend/src/editor/renderers/grid-layout/grid-layout.tsx
+++ b/frontend/src/editor/renderers/grid-layout/grid-layout.tsx
@@ -1,5 +1,5 @@
/* Copyright 2023 Marimo. All rights reserved. */
-import React, { memo } from "react";
+import React, { memo, useMemo, useState } from "react";
import { Responsive, WidthProvider } from "react-grid-layout";
import { ICellRendererPlugin, ICellRendererProps } from "../types";
import {
@@ -20,17 +20,15 @@ import { AppMode } from "@/core/mode";
import { TinyCode } from "@/editor/cell/TinyCode";
import { cn } from "@/lib/utils";
import { XIcon } from "lucide-react";
+import { Input } from "@/components/ui/input";
+import { Label } from "@/components/ui/label";
+import useEvent from "react-use-event-hook";
type Props = ICellRendererProps<GridLayout>;
const ReactGridLayout = WidthProvider(Responsive);
-const cols = {
- // we only allow 1 responsive breakpoint
- // we can change this later if we want to support more,
- // but this increases complexity to the user
- lg: 12,
-};
+const MARGIN: [number, number] = [0, 0];
const GridLayoutRenderer: React.FC<Props> = ({
layout,
@@ -39,14 +37,27 @@ const GridLayoutRenderer: React.FC<Props> = ({
mode,
}) => {
const isReading = mode === "read";
- const [isDragging, setIsDragging] = React.useState(false);
+ const [isDragging, setIsDragging] = useState(false);
const inGridIds = new Set(layout.cells.map((cell) => cell.i));
- const [droppingItem, setDroppingItem] = React.useState<{
+ const [droppingItem, setDroppingItem] = useState<{
i: string;
w?: number;
h?: number;
} | null>(null);
+ const cols = useMemo(
+ () => ({
+ // we only allow 1 responsive breakpoint
+ // we can change this later if we want to support more,
+ // but this increases complexity to the user
+ lg: layout.columns,
+ }),
+ [layout.columns]
+ );
+
+ const handleDraggingTrue = useEvent(() => setIsDragging(true));
+ const handleDraggingFalse = useEvent(() => setIsDragging(false));
+
const grid = (
<ReactGridLayout
breakpoint="lg"
@@ -56,11 +67,13 @@ const GridLayoutRenderer: React.FC<Props> = ({
cols={cols}
isBounded={true}
allowOverlap={false}
- autoSize={true}
className={cn(
- !isReading && "min-w-[800px] min-h-[600px]",
+ !isReading && "min-w-[800px] min-h-full",
isReading && "disable-animation"
)}
+ margin={MARGIN}
+ compactType={null}
+ preventCollision={true}
rowHeight={layout.rowHeight}
onLayoutChange={(cellLayouts) =>
setLayout({
@@ -87,8 +100,8 @@ const GridLayoutRenderer: React.FC<Props> = ({
cells: [...cellLayouts, dropped],
});
}}
- onDragStart={() => setIsDragging(true)}
- onDragStop={() => setIsDragging(false)}
+ onDragStart={handleDraggingTrue}
+ onDragStop={handleDraggingFalse}
// When in read mode, disable dragging and resizing
isDraggable={!isReading}
isDroppable={!isReading}
@@ -102,9 +115,8 @@ const GridLayoutRenderer: React.FC<Props> = ({
className={cn(
"relative transparent-when-disconnected",
!isReading &&
- "hover:bg-[var(--sage-2)] border-transparent hover:border-border border hover:rounded hover-actions-parent",
- isDragging && "bg-[var(--sage-2)] border-border rounded",
- "overflow-hidden p-2"
+ "bg-background hover:bg-[var(--slate-2)] border-transparent hover:border-border border hover:rounded hover-actions-parent",
+ isDragging && "bg-[var(--slate-2)] border-border rounded"
)}
>
<GridCell
@@ -139,46 +151,91 @@ const GridLayoutRenderer: React.FC<Props> = ({
const notInGrid = cells.filter((cell) => !inGridIds.has(cell.key));
return (
- <div className={cn("flex h-full overflow-hidden gap-2 px-2 pb-2")}>
- <div className="flex-grow overflow-auto border rounded bg-[var(--sage-1)] shadow-sm transparent-when-disconnected">
- {grid}
- </div>
- <div className="flex-none flex flex-col w-[400px] p-3 gap-3 overflow-auto h-full bg-[var(--sage-3)] border rounded shadow-sm transparent-when-disconnected">
- <div className="text font-bold text-[var(--sage-10)] overflow-auto flex-shrink-0">
- Outputs
+ <>
+ <div className="flex flex-row absolute left-5 top-4 gap-4">
+ <div className="flex flex-row items-center gap-2">
+ <Label>Columns</Label>
+ <Input
+ type="number"
+ value={layout.columns}
+ className="w-[60px]"
+ placeholder="# of Columns"
+ min={1}
+ onChange={(e) => {
+ setLayout({
+ ...layout,
+ columns: Number.parseInt(e.target.value) || 1,
+ });
+ }}
+ />
</div>
- {notInGrid.map((cell) => (
- <div
- key={cell.key}
- draggable={true}
- unselectable="on"
- data-cell-id={cell.key}
- // Firefox requires some kind of initialization which we can do by adding this attribute
- // @see https://bugzilla.mozilla.org/show_bug.cgi?id=568313
- onDragStart={(e) => {
- // get height of self
- const height = e.currentTarget.offsetHeight;
-
- setDroppingItem({
- i: cell.key,
- w: 3,
- h: Math.ceil(height / layout.rowHeight) || 1,
+ <div className="flex flex-row items-center gap-2">
+ <Label>Row Height (px)</Label>
+ <Input
+ type="number"
+ value={layout.rowHeight}
+ className="w-[60px]"
+ placeholder="Row Height (px)"
+ min={1}
+ onChange={(e) => {
+ setLayout({
+ ...layout,
+ rowHeight: Number.parseInt(e.target.value) || 1,
});
- e.dataTransfer.setData("text/plain", "");
}}
- className="droppable-element bg-[var(--sage-2)] border-border border overflow-hidden p-2 rounded flex-shrink-0"
- >
- <GridCell
- code={cell.code}
- mode={mode}
- cellId={cell.key}
- output={cell.output}
- status={cell.status}
- />
+ />
+ </div>
+ </div>
+ <div
+ className={cn("relative flex h-full overflow-hidden gap-2 px-2 pb-2")}
+ >
+ <div
+ className="flex-grow overflow-auto border rounded bg-[var(--slate-2)] shadow-sm transparent-when-disconnected"
+ style={{
+ backgroundImage:
+ "repeating-linear-gradient(var(--gray-4) 0 1px, transparent 1px 100%), repeating-linear-gradient(90deg, var(--gray-4) 0 1px, transparent 1px 100%)",
+ backgroundSize: `calc((100% / ${layout.columns})) ${layout.rowHeight}px`,
+ }}
+ >
+ {grid}
+ </div>
+ <div className="flex-none flex flex-col w-[400px] p-3 gap-3 overflow-auto h-full bg-[var(--slate-2)] border rounded shadow-sm transparent-when-disconnected">
+ <div className="text font-bold text-[var(--slate-20)] overflow-auto flex-shrink-0">
+ Outputs
</div>
- ))}
+ {notInGrid.map((cell) => (
+ <div
+ key={cell.key}
+ draggable={true}
+ unselectable="on"
+ data-cell-id={cell.key}
+ // Firefox requires some kind of initialization which we can do by adding this attribute
+ // @see https://bugzilla.mozilla.org/show_bug.cgi?id=568313
+ onDragStart={(e) => {
+ // get height of self
+ const height = e.currentTarget.offsetHeight;
+
+ setDroppingItem({
+ i: cell.key,
+ w: layout.columns / 4,
+ h: Math.ceil(height / layout.rowHeight) || 1,
+ });
+ e.dataTransfer.setData("text/plain", "");
+ }}
+ className="droppable-element bg-[var(--slate-2)] border-border border overflow-hidden p-2 rounded flex-shrink-0"
+ >
+ <GridCell
+ code={cell.code}
+ mode={mode}
+ cellId={cell.key}
+ output={cell.output}
+ status={cell.status}
+ />
+ </div>
+ ))}
+ </div>
</div>
- </div>
+ </>
);
};
@@ -198,7 +255,7 @@ const GridCell = memo(
}
return (
- <div>
+ <div className="h-full w-full overflow-auto p-2">
<OutputArea output={output} cellId={cellId} stale={loading} />
</div>
);
@@ -217,7 +274,7 @@ export const GridLayoutPlugin: ICellRendererPlugin<
name: "Grid",
validator: z.object({
- columns: z.number().min(1).max(12),
+ columns: z.number().min(1),
rowHeight: z.number().min(1),
cells: z.array(
z.object({
@@ -290,8 +347,8 @@ export const GridLayoutPlugin: ICellRendererPlugin<
Component: GridLayoutRenderer,
getInitialLayout: () => ({
- columns: 12,
- rowHeight: 40,
+ columns: 24,
+ rowHeight: 20,
cells: [],
}),
};
diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts
index 1c09fd21af..cb1f41100b 100644
--- a/frontend/vite.config.ts
+++ b/frontend/vite.config.ts
@@ -28,6 +28,7 @@ const htmlDevPlugin = (): Plugin => {
},
experimental: {
theming: true,
+ layouts: true,
},
})
)
@@ -66,6 +67,7 @@ export default defineConfig({
htmlDevPlugin(),
react({
tsDecorators: true,
+ plugins: [["@swc-jotai/react-refresh", {}]],
}),
tsconfigPaths(),
],