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: opening a notebook from the sidebar when in a subpath (#5561)

Myles Scolnick7f91aa6854a3
diff --git a/frontend/src/components/editor/actions/useCopyNotebook.tsx b/frontend/src/components/editor/actions/useCopyNotebook.tsx
index cabbe3f339..eb6504c29b 100644
--- a/frontend/src/components/editor/actions/useCopyNotebook.tsx
+++ b/frontend/src/components/editor/actions/useCopyNotebook.tsx
@@ -2,6 +2,7 @@
import { useImperativeModal } from "@/components/modal/ImperativeModal";
import { toast } from "@/components/ui/use-toast";
import { sendCopy } from "@/core/network/requests";
+import { openNotebook } from "@/utils/links";
import { PathBuilder, Paths } from "@/utils/paths";
export function useCopyNotebook(source: string | null) {
@@ -31,7 +32,7 @@ export function useCopyNotebook(source: string | null) {
title: "Notebook copied",
description: "A copy of the notebook has been created.",
});
- window.open(`/?file=${destination}`, "_blank");
+ openNotebook(destination);
});
},
});
diff --git a/frontend/src/components/editor/file-tree/file-explorer.tsx b/frontend/src/components/editor/file-tree/file-explorer.tsx
index 83b79c46c8..97d81f11ae 100644
--- a/frontend/src/components/editor/file-tree/file-explorer.tsx
+++ b/frontend/src/components/editor/file-tree/file-explorer.tsx
@@ -57,6 +57,7 @@ import { ErrorBanner } from "@/plugins/impl/common/error-banner";
import { cn } from "@/utils/cn";
import { copyToClipboard } from "@/utils/copy";
import { downloadBlob } from "@/utils/download";
+import { openNotebook } from "@/utils/links";
import type { FilePath } from "@/utils/paths";
import { fileSplit } from "@/utils/pathUtils";
import { FileViewer } from "./file-viewer";
@@ -675,5 +676,5 @@ function openMarimoNotebook(
) {
event.stopPropagation();
event.preventDefault();
- window.open(`/?file=${path}`, "_blank");
+ openNotebook(path);
}
diff --git a/frontend/src/components/home/components.tsx b/frontend/src/components/home/components.tsx
index d2d468228a..49a2c046e3 100644
--- a/frontend/src/components/home/components.tsx
+++ b/frontend/src/components/home/components.tsx
@@ -30,8 +30,8 @@ import {
import { Constants } from "@/core/constants";
import { openTutorial } from "@/core/network/requests";
import type { TutorialId } from "@/core/network/types";
+import { openNotebook } from "@/utils/links";
import { Objects } from "@/utils/objects";
-import { asURL } from "@/utils/url";
const TUTORIALS: Record<
TutorialId,
@@ -89,7 +89,7 @@ export const OpenTutorialDropDown: React.FC = () => {
if (!file) {
return;
}
- window.open(asURL(`?file=${file.path}`).toString(), "_blank");
+ openNotebook(file.path);
}}
>
<Icon
diff --git a/frontend/src/utils/links.ts b/frontend/src/utils/links.ts
new file mode 100644
index 0000000000..0decd5e9b6
--- /dev/null
+++ b/frontend/src/utils/links.ts
@@ -0,0 +1,11 @@
+/* Copyright 2024 Marimo. All rights reserved. */
+import { asURL } from "./url";
+
+/**
+ * Open a notebook in a new tab.
+ * @param path - The path to the notebook.
+ */
+export function openNotebook(path: string) {
+ // There is no leading `/` in the path in order to work when marimo is at a subpath.
+ window.open(asURL(`?file=${path}`).toString(), "_blank");
+}