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.

improvement: add new-notebook button (#3027)

Myles Scolnick39a98a406b98
diff --git a/frontend/src/components/editor/actions/useNotebookActions.tsx b/frontend/src/components/editor/actions/useNotebookActions.tsx
index 51d150842f..af1786fc21 100644
--- a/frontend/src/components/editor/actions/useNotebookActions.tsx
+++ b/frontend/src/components/editor/actions/useNotebookActions.tsx
@@ -28,6 +28,7 @@ import {
Files,
SettingsIcon,
XCircleIcon,
+ FilePlus2Icon,
} from "lucide-react";
import { commandPaletteAtom } from "../controls/command-palette";
import { useCellActions, useNotebook } from "@/core/cells/cells";
@@ -65,6 +66,7 @@ import { isWasm } from "@/core/wasm/utils";
import { settingDialogAtom } from "@/components/app-config/app-config-button";
import { renderShortcut } from "@/components/shortcuts/renderShortcut";
import { copyToClipboard } from "@/utils/copy";
+import { newNotebookURL } from "@/utils/urls";
const NOOP_HANDLER = (event?: Event) => {
event?.preventDefault();
@@ -393,9 +395,24 @@ export function useNotebookActions() {
divider: true,
icon: <Home size={14} strokeWidth={1.5} />,
label: "Return home",
+ // If file is in the url, then we ran `marimo edit`
+ // without a specific file
hidden: !location.search.includes("file"),
handle: () => {
- window.location.href = document.baseURI;
+ const withoutSearch = document.baseURI.split("?")[0];
+ window.open(withoutSearch, "_self");
+ },
+ },
+
+ {
+ icon: <FilePlus2Icon size={14} strokeWidth={1.5} />,
+ label: "New notebook",
+ // If file is in the url, then we ran `marimo edit`
+ // without a specific file
+ hidden: !location.search.includes("file"),
+ handle: () => {
+ const url = newNotebookURL();
+ window.open(url, "_blank");
},
},
diff --git a/frontend/src/components/pages/home-page.tsx b/frontend/src/components/pages/home-page.tsx
index e1efd1e4cf..134275e698 100644
--- a/frontend/src/components/pages/home-page.tsx
+++ b/frontend/src/components/pages/home-page.tsx
@@ -35,7 +35,6 @@ import {
import { ShutdownButton } from "../editor/controls/shutdown-button";
import {
type SessionId,
- generateSessionId,
getSessionId,
isSessionId,
} from "@/core/kernel/session";
@@ -86,6 +85,7 @@ import { CaretDownIcon } from "@radix-ui/react-icons";
import { ErrorBoundary } from "../editor/boundary/ErrorBoundary";
import { Banner } from "@/plugins/impl/common/error-banner";
import { prettyError } from "@/utils/errors";
+import { newNotebookURL } from "@/utils/urls";
function tabTarget(path: string) {
// Consistent tab target so we open in the same tab when clicking on the same notebook
@@ -525,16 +525,16 @@ const SessionShutdownButton: React.FC<{ filePath: string }> = ({
};
const CreateNewNotebook: React.FC = () => {
- const sessionId = generateSessionId();
- const initializationId = `__new__${sessionId}`;
+ const url = newNotebookURL();
return (
<a
className="relative rounded-lg p-6 group
text-primary hover:bg-[var(--blue-2)] shadow-mdSolid shadow-accent border
transition-all duration-300 cursor-pointer
"
- href={asURL(`?file=${initializationId}`).toString()}
- target={tabTarget(initializationId)}
+ href={url}
+ target="_blank"
+ rel="noreferrer"
>
<h2 className="text-lg font-semibold">Create a new notebook</h2>
<div className="group-hover:opacity-100 opacity-0 absolute right-5 top-0 bottom-0 rounded-lg flex items-center justify-center transition-all duration-300">
diff --git a/frontend/src/utils/urls.ts b/frontend/src/utils/urls.ts
index 78a0ab237e..e088900d3e 100644
--- a/frontend/src/utils/urls.ts
+++ b/frontend/src/utils/urls.ts
@@ -1,4 +1,7 @@
/* Copyright 2024 Marimo. All rights reserved. */
+import { generateSessionId } from "@/core/kernel/session";
+import { asURL } from "./url";
+
export function updateQueryParams(updater: (params: URLSearchParams) => void) {
const url = new URL(window.location.href);
updater(url.searchParams);
@@ -17,3 +20,9 @@ export function hasQueryParam(key: string, value?: string): boolean {
return urlParams.get(key) === value;
}
+
+export function newNotebookURL() {
+ const sessionId = generateSessionId();
+ const initializationId = `__new__${sessionId}`;
+ return asURL(`?file=${initializationId}`).toString();
+}