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.

feat: support markdown in file tree notebook creation (#8770)

Daizu90fc851cedb2
diff --git a/marimo/_server/files/os_file_system.py b/marimo/_server/files/os_file_system.py
index 845d2ffca0..b990eb880f 100644
--- a/marimo/_server/files/os_file_system.py
+++ b/marimo/_server/files/os_file_system.py
@@ -155,10 +155,17 @@ class OSFileSystem(FileSystem):
if file_type == "directory":
full_path.mkdir(parents=True, exist_ok=True)
elif file_type == "notebook" and not contents:
+ from marimo._convert.converters import MarimoConvert
+
full_path.parent.mkdir(parents=True, exist_ok=True)
# Create a new AppFileManager to get the default notebook code
# We pass None as filename to get the empty notebook template
- notebook_code = AppFileManager(None).to_code()
+ ir = AppFileManager(None).app.to_ir()
+ converter = MarimoConvert.from_ir(ir)
+ if full_path.suffix in (".md", ".qmd"):
+ notebook_code = converter.to_markdown(full_path.name)
+ else:
+ notebook_code = converter.to_py()
full_path.write_text(notebook_code, encoding="utf-8")
contents = notebook_code.encode("utf-8")
else:
@@ -352,7 +359,6 @@ class OSFileSystem(FileSystem):
return True
except Exception as e:
LOGGER.error(f"Error opening with EDITOR: {e}")
- pass
# Use system default if no editor specified
if platform.system() == "Darwin": # macOS
diff --git a/tests/_server/files/test_os_file_system.py b/tests/_server/files/test_os_file_system.py
index 8a22087e8c..dfb43343d4 100644
--- a/tests/_server/files/test_os_file_system.py
+++ b/tests/_server/files/test_os_file_system.py
@@ -29,8 +29,18 @@ def test_create_file(test_dir: Path, fs: OSFileSystem) -> None:
assert expected_path.exists()
-def test_create_notebook(test_dir: Path, fs: OSFileSystem) -> None:
- test_notebook_name = "test_notebook.py"
+ ("ext", "expected"),
+ [
+ ("py", "__generated_with"),
+ ("md", "marimo-version:"),
+ ("qmd", "marimo-team/marimo"),
+ ],
+)
+def test_create_notebook(
+ test_dir: Path, fs: OSFileSystem, ext: str, expected: str
+) -> None:
+ test_notebook_name = f"test_notebook.{ext}"
fs.create_file_or_directory(
str(test_dir),
"notebook",
@@ -40,8 +50,7 @@ def test_create_notebook(test_dir: Path, fs: OSFileSystem) -> None:
expected_path = test_dir / test_notebook_name
assert expected_path.exists()
notebook_code = expected_path.read_text("utf-8")
- assert "import marimo" in notebook_code
- assert "app.run()" in notebook_code
+ assert expected in notebook_code
def test_create_notebook_with_contents(