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: create public folder and then file (#4299)

Shahmir Varqhade0742a237b7
diff --git a/frontend/src/core/codemirror/markdown/__tests__/commands.test.ts b/frontend/src/core/codemirror/markdown/__tests__/commands.test.ts
index 93010ec269..bb1d554b4b 100644
--- a/frontend/src/core/codemirror/markdown/__tests__/commands.test.ts
+++ b/frontend/src/core/codemirror/markdown/__tests__/commands.test.ts
@@ -21,6 +21,8 @@ import {
insertUL,
} from "../commands";
import { sendCreateFileOrFolder } from "@/core/network/requests";
+import { filenameAtom } from "@/core/saving/filename";
+import { store } from "@/core/state/jotai";
function createEditor(content: string) {
const state = EditorState.create({
@@ -220,7 +222,7 @@ describe("insertImage", () => {
vi.mocked(sendCreateFileOrFolder).mockResolvedValueOnce({
success: true,
info: {
- path: "hello.png",
+ path: "public/hello.png",
name: "hello.png",
isMarimoFile: true,
isDirectory: false,
@@ -233,8 +235,56 @@ describe("insertImage", () => {
await insertImage(view, mockPngFile());
expect(sendCreateFileOrFolder).toHaveBeenCalledTimes(1);
+ expect(sendCreateFileOrFolder).toHaveBeenCalledWith({
+ path: "public",
+ type: "file",
+ name: "hello.png",
+ contents: "AQID",
+ });
+
+ expect(view.state.doc.toString()).toMatchInlineSnapshot(
+ `"Hello, ![](public/hello.png)world!"`,
+ );
+ });
+
+ test("saves image in public folder of notebook directory", async () => {
+ view = createEditor("Hello, world!");
+ view.dispatch({
+ selection: { anchor: 7, head: 7 },
+ });
+
+ // mock filenameAtom
+ vi.spyOn(store, "get").mockImplementation((atom) => {
+ if (atom === filenameAtom) {
+ return "nested/hello.py";
+ }
+ });
+
+ vi.mocked(sendCreateFileOrFolder).mockResolvedValueOnce({
+ success: true,
+ info: {
+ path: "nested/public/hello.png",
+ name: "hello.png",
+ children: [],
+ id: "",
+ isDirectory: false,
+ isMarimoFile: true,
+ lastModified: null,
+ },
+ });
+
+ await insertImage(view, mockPngFile());
+
+ expect(sendCreateFileOrFolder).toHaveBeenCalledTimes(1);
+ expect(sendCreateFileOrFolder).toHaveBeenCalledWith({
+ path: "nested/public", // store in public folder of notebook directory
+ type: "file",
+ name: "hello.png",
+ contents: "AQID",
+ });
+
expect(view.state.doc.toString()).toMatchInlineSnapshot(
- `"Hello, ![](hello.png)world!"`,
+ `"Hello, ![](nested/public/hello.png)world!"`,
);
});
@@ -247,7 +297,7 @@ describe("insertImage", () => {
vi.mocked(sendCreateFileOrFolder).mockResolvedValueOnce({
success: true,
info: {
- path: "hello.jpg",
+ path: "public/hello.jpg",
name: "hello.jpg",
children: [],
id: "",
@@ -265,7 +315,7 @@ describe("insertImage", () => {
expect(sendCreateFileOrFolder).toHaveBeenCalledTimes(1);
expect(view.state.doc.toString()).toMatchInlineSnapshot(
- `"Hello, ![](hello.jpg)world!"`,
+ `"Hello, ![](public/hello.jpg)world!"`,
);
});
diff --git a/frontend/src/core/codemirror/markdown/commands.ts b/frontend/src/core/codemirror/markdown/commands.ts
index c688e7216c..a15f8e7707 100644
--- a/frontend/src/core/codemirror/markdown/commands.ts
+++ b/frontend/src/core/codemirror/markdown/commands.ts
@@ -1,7 +1,9 @@
/* Copyright 2024 Marimo. All rights reserved. */
import { toast } from "@/components/ui/use-toast";
import { sendCreateFileOrFolder } from "@/core/network/requests";
-import type { FilePath } from "@/utils/paths";
+import { filenameAtom } from "@/core/saving/filename";
+import { store } from "@/core/state/jotai";
+import { Paths, type FilePath } from "@/utils/paths";
import {
EditorSelection,
type SelectionRange,
@@ -297,18 +299,24 @@ export async function insertImage(view: EditorView, file: File) {
inputFilename = `${inputFilename}.${extension}`;
}
- const fileCreationResponse = await sendCreateFileOrFolder({
- path: "" as FilePath, // Root path
+ const filepath = store.get(filenameAtom);
+ const notebookDir = filepath ? Paths.dirname(filepath) : null;
+ const publicFolderPath = notebookDir
+ ? `${notebookDir}/public`
+ : "public";
+
+ const createFileRes = await sendCreateFileOrFolder({
+ path: publicFolderPath as FilePath,
type: "file",
name: inputFilename,
contents: base64,
});
- if (fileCreationResponse.success) {
- savedFilePath = fileCreationResponse.info?.path;
+ if (createFileRes.success) {
+ savedFilePath = createFileRes.info?.path;
toast({
title: "Image uploaded successfully",
- description: `We've uploaded your image as ${savedFilePath}`,
+ description: `We've uploaded your image at ${savedFilePath}`,
});
} else {
toast({
diff --git a/frontend/src/core/saving/filename.ts b/frontend/src/core/saving/filename.ts
index b0e7d85765..1f35199bfc 100644
--- a/frontend/src/core/saving/filename.ts
+++ b/frontend/src/core/saving/filename.ts
@@ -12,7 +12,7 @@ import { useImperativeModal } from "@/components/modal/ImperativeModal";
import { connectionAtom } from "../network/connection";
import { getAppConfig } from "../config/config";
-const filenameAtom = atom<string | null>(getFilenameFromDOM());
+export const filenameAtom = atom<string | null>(getFilenameFromDOM());
export function useFilename() {
return useAtomValue(filenameAtom);
diff --git a/marimo/_server/files/os_file_system.py b/marimo/_server/files/os_file_system.py
index 54da49e402..970bc80185 100644
--- a/marimo/_server/files/os_file_system.py
+++ b/marimo/_server/files/os_file_system.py
@@ -144,6 +144,7 @@ class OSFileSystem(FileSystem):
if file_type == "directory":
full_path.mkdir(parents=True, exist_ok=True)
else:
+ full_path.parent.mkdir(parents=True, exist_ok=True)
full_path.write_bytes(contents or b"")
# encoding latin-1 to get an invertible representation of the
# bytes as a string ...
diff --git a/tests/_server/files/test_os_file_system.py b/tests/_server/files/test_os_file_system.py
index c53aafa07d..3ad5cb2c5c 100644
--- a/tests/_server/files/test_os_file_system.py
+++ b/tests/_server/files/test_os_file_system.py
@@ -43,6 +43,17 @@ def test_create_file_with_duplicate_name(
assert os.path.exists(expected_path)
+def test_create_file_and_parent_directories(
+ test_dir: str, fs: OSFileSystem
+) -> None:
+ test_file_name = "test_file.txt"
+ fs.create_file_or_directory(
+ f"{test_dir}/parent", "file", test_file_name, None
+ )
+ expected_path = os.path.join(test_dir, "parent", test_file_name)
+ assert os.path.exists(expected_path)
+
+
def test_create_directory(test_dir: str, fs: OSFileSystem) -> None:
test_dir_name = "test_dir"
fs.create_file_or_directory(test_dir, "directory", test_dir_name, None)