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: marimo edit with absolute path to directory (#1897)

Akshay Agrawaleca4daa0636e
diff --git a/frontend/src/components/pages/home-page.tsx b/frontend/src/components/pages/home-page.tsx
index 6b3d0fea27..02493e16e9 100644
--- a/frontend/src/components/pages/home-page.tsx
+++ b/frontend/src/components/pages/home-page.tsx
@@ -279,7 +279,7 @@ const Node = ({ node, style }: NodeRendererProps<FileInfo>) => {
const relativePath =
node.data.path.startsWith(root) && Paths.isAbsolute(node.data.path)
- ? node.data.path.slice(root.length + 1)
+ ? Paths.rest(node.data.path, root)
: node.data.path;
const isMarkdown = relativePath.endsWith(".md");
diff --git a/frontend/src/utils/__tests__/path.test.ts b/frontend/src/utils/__tests__/path.test.ts
index fec46e05fd..3c7db7d612 100644
--- a/frontend/src/utils/__tests__/path.test.ts
+++ b/frontend/src/utils/__tests__/path.test.ts
@@ -48,6 +48,38 @@ describe("Paths", () => {
expect(Paths.basename("C:\\user\\docs\\Letter.txt")).toBe("Letter.txt");
});
});
+
+ describe("rest", () => {
+ it("should return the suffix of a path", () => {
+ expect(Paths.rest("/user/docs/Letter.txt", "/user/docs")).toBe(
+ "Letter.txt",
+ );
+ expect(Paths.rest("/user/docs/Letter.txt", "/user/docs/")).toBe(
+ "Letter.txt",
+ );
+ expect(Paths.rest("/user/docs/Letter.txt", "/user")).toBe(
+ "docs/Letter.txt",
+ );
+ expect(Paths.rest("/user/docs/Letter.txt", "/user/")).toBe(
+ "docs/Letter.txt",
+ );
+ });
+
+ it("should handle windows-style paths", () => {
+ expect(Paths.rest("C:\\user\\docs\\Letter.txt", "C:\\user\\docs")).toBe(
+ "Letter.txt",
+ );
+ expect(Paths.rest("C:\\user\\docs\\Letter.txt", "C:\\user\\docs\\")).toBe(
+ "Letter.txt",
+ );
+ expect(Paths.rest("C:\\user\\docs\\Letter.txt", "C:\\user")).toBe(
+ "docs\\Letter.txt",
+ );
+ expect(Paths.rest("C:\\user\\docs\\Letter.txt", "C:\\user\\")).toBe(
+ "docs\\Letter.txt",
+ );
+ });
+ });
});
describe("PathBuilder", () => {
diff --git a/frontend/src/utils/paths.ts b/frontend/src/utils/paths.ts
index 1234a702da..355bdb4989 100644
--- a/frontend/src/utils/paths.ts
+++ b/frontend/src/utils/paths.ts
@@ -16,6 +16,12 @@ export const Paths = {
basename: (path: string) => {
return PathBuilder.guessDeliminator(path).basename(path as FilePath);
},
+ rest: (path: string, root: string) => {
+ return PathBuilder.guessDeliminator(path).rest(
+ path as FilePath,
+ root as FilePath,
+ );
+ },
extension: (filename: string): string => {
const parts = filename.split(".");
if (parts.length === 1) {
@@ -41,6 +47,18 @@ export class PathBuilder {
return (parts.pop() ?? "") as FilePath;
}
+ rest(path: FilePath, root: FilePath): FilePath {
+ const pathParts = path.split(this.deliminator);
+ const rootParts = root.split(this.deliminator);
+ let i = 0;
+ for (; i < pathParts.length && i < rootParts.length; ++i) {
+ if (pathParts[i] !== rootParts[i]) {
+ break;
+ }
+ }
+ return pathParts.slice(i).join(this.deliminator) as FilePath;
+ }
+
dirname(path: FilePath): FilePath {
const parts = path.split(this.deliminator);
parts.pop();
diff --git a/marimo/_server/file_router.py b/marimo/_server/file_router.py
index 44c904d164..1fb19bf3c6 100644
--- a/marimo/_server/file_router.py
+++ b/marimo/_server/file_router.py
@@ -3,6 +3,7 @@ from __future__ import annotations
import abc
import os
+import pathlib
from typing import List, Optional
from marimo import _loggers
@@ -27,6 +28,10 @@ class AppFileRouter(abc.ABC):
NEW_FILE: MarimoFileKey = "__new__"
+ @property
+ def directory(self) -> str | None:
+ return None
+
@staticmethod
def infer(path: str) -> AppFileRouter:
if os.path.isfile(path):
@@ -85,13 +90,19 @@ class AppFileRouter(abc.ABC):
if file.path == key:
return AppFileManager(file.path, default_width)
+ path = (
+ os.path.join(self.directory, key)
+ if self.directory is not None
+ else key
+ )
+
# Absolute path
- if os.path.isabs(key):
- return AppFileManager(key, default_width)
+ if os.path.isabs(path):
+ return AppFileManager(path, default_width)
# Relative path
- if os.path.exists(key):
- return AppFileManager(key, default_width)
+ if os.path.exists(path):
+ return AppFileManager(path, default_width)
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND,
@@ -169,10 +180,15 @@ class ListOfFilesAppFileRouter(AppFileRouter):
class LazyListOfFilesAppFileRouter(AppFileRouter):
def __init__(self, directory: str, include_markdown: bool) -> None:
- self.directory = directory
+ # pass through Path to canonicalize, strips trailing slashes
+ self._directory = str(pathlib.Path(directory))
self.include_markdown = include_markdown
self._lazy_files: Optional[List[FileInfo]] = None
+ @property
+ def directory(self) -> str:
+ return self._directory
+
def toggle_markdown(
self, include_markdown: bool
) -> LazyListOfFilesAppFileRouter:
diff --git a/tests/_server/test_file_router.py b/tests/_server/test_file_router.py
index f468a82f8d..40ffb5d5d6 100644
--- a/tests/_server/test_file_router.py
+++ b/tests/_server/test_file_router.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import os
+import shutil
import tempfile
import unittest
@@ -46,7 +47,7 @@ class TestAppFileRouter(unittest.TestCase):
os.unlink(self.test_file1.name)
os.unlink(self.test_file2.name)
os.unlink(self.test_file_3.name)
- os.rmdir(self.test_dir)
+ shutil.rmtree(self.test_dir)
def test_infer_file(self):
# Test infer method with a file path
@@ -118,3 +119,11 @@ class TestAppFileRouter(unittest.TestCase):
router = router.toggle_markdown(True)
files = router.files
assert len(files) == 3
+
+ def test_lazy_list_of_get_app_file_manager(self):
+ router = LazyListOfFilesAppFileRouter(
+ self.test_dir, include_markdown=False
+ )
+ basename = os.path.basename(self.test_file1.name)
+ file_manager = router.get_file_manager(key=basename)
+ assert file_manager.filename == os.path.join(self.test_dir, basename)