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.

perf: don't read the full file for OSFileSystem._is_marimo_file (#8253)

Myles Scolnickf902f3248849
diff --git a/marimo/_server/export/exporter.py b/marimo/_server/export/exporter.py
index 8fb7a11077..51c5cbf4fb 100644
--- a/marimo/_server/export/exporter.py
+++ b/marimo/_server/export/exporter.py
@@ -91,9 +91,11 @@ class Exporter:
session_snapshot
)
+ app_code = app.to_py()
+
# Prepare code for export
code = self._prepare_code(
- request.include_code, app, notebook_snapshot, session_snapshot
+ request.include_code, app_code, notebook_snapshot, session_snapshot
)
# Build fallback virtual_files dict for files not in HTML outputs
@@ -102,7 +104,7 @@ class Exporter:
)
# Generate final HTML
- code_hash = hash_code(app.to_py())
+ code_hash = hash_code(app_code)
html = static_notebook_template(
html=index_html,
user_config=config,
@@ -167,7 +169,7 @@ class Exporter:
def _prepare_code(
self,
include_code: bool,
- app: InternalApp,
+ app_code: str,
notebook_snapshot: NotebookV1,
session_snapshot: NotebookSessionV1,
) -> str:
@@ -181,7 +183,7 @@ class Exporter:
snapshot_cell["console"] = []
return ""
- return app.to_py()
+ return app_code
def _normalize_virtual_file_url(self, url: str) -> str:
"""Normalize virtual file URL format from /@file/ to ./@file/."""
diff --git a/marimo/_server/files/directory_scanner.py b/marimo/_server/files/directory_scanner.py
index 1ea9b36b86..d31ce2d7ef 100644
--- a/marimo/_server/files/directory_scanner.py
+++ b/marimo/_server/files/directory_scanner.py
@@ -36,6 +36,10 @@ def is_marimo_app(full_path: str) -> bool:
try:
path = MarimoPath(full_path)
+ # Fast extension check to avoid I/O for unrelated files
+ if not path.is_python() and not path.is_markdown():
+ return False
+
with open(full_path, "rb") as f:
header = f.read(READ_LIMIT)
diff --git a/marimo/_server/files/os_file_system.py b/marimo/_server/files/os_file_system.py
index ce1fb7aa36..9793e171d9 100644
--- a/marimo/_server/files/os_file_system.py
+++ b/marimo/_server/files/os_file_system.py
@@ -108,10 +108,12 @@ class OSFileSystem(FileSystem):
def _is_marimo_file(self, path: str) -> bool:
file_path = Path(path)
- if not file_path.suffix == ".py":
+ if file_path.suffix not in (".py", ".md", ".qmd"):
return False
- return b"app = marimo.App(" in file_path.read_bytes()
+ from marimo._server.files.directory_scanner import is_marimo_app
+
+ return is_marimo_app(path)
def open_file(self, path: str, encoding: str | None = None) -> str:
file_path = Path(path)
@@ -207,9 +209,11 @@ class OSFileSystem(FileSystem):
if not os.path.exists(search_path):
return []
+ query_lower = query.lower()
+
# Compile regex pattern for case-insensitive search
try:
- pattern = re.compile(re.escape(query.lower()))
+ pattern = re.compile(re.escape(query_lower))
except re.error:
# If regex compilation fails, fall back to simple string matching
pattern = None
@@ -245,7 +249,6 @@ class OSFileSystem(FileSystem):
# Check if name matches query
name_lower = entry.name.lower()
- query_lower = query.lower()
matches = False
if pattern:
@@ -304,7 +307,6 @@ class OSFileSystem(FileSystem):
# Sort results by relevance (exact matches first, then by name)
def sort_key(file_info: FileInfo) -> tuple[int, str]:
name_lower = file_info.name.lower()
- query_lower = query.lower()
# Exact match gets highest priority
if name_lower == query_lower:
diff --git a/tests/_server/files/test_os_file_system.py b/tests/_server/files/test_os_file_system.py
index 09099b02bb..188b62f795 100644
--- a/tests/_server/files/test_os_file_system.py
+++ b/tests/_server/files/test_os_file_system.py
@@ -288,6 +288,68 @@ def test_get_details_non_utf8_encoding_and_contents(
assert result2.contents == "override"
+class TestIsMarimoFile:
+ """Tests for _is_marimo_file which delegates to is_marimo_app."""
+
+ def test_python_marimo_file(
+ self, test_dir: Path, fs: OSFileSystem
+ ) -> None:
+ """Detects a Python marimo app."""
+ py_file = test_dir / "app.py"
+ py_file.write_text(
+ "import marimo\napp = marimo.App()\n"
+ "@app.cell\ndef __():\n return\n"
+ )
+ assert fs._is_marimo_file(str(py_file)) is True
+
+ def test_python_non_marimo_file(
+ self, test_dir: Path, fs: OSFileSystem
+ ) -> None:
+ """Regular Python file is not a marimo app."""
+ py_file = test_dir / "script.py"
+ py_file.write_text("print('hello world')\n")
+ assert fs._is_marimo_file(str(py_file)) is False
+
+ def test_markdown_marimo_file(
+ self, test_dir: Path, fs: OSFileSystem
+ ) -> None:
+ """Detects a markdown marimo notebook."""
+ md_file = test_dir / "notebook.md"
+ md_file.write_text("---\nmarimo-version: 0.1.0\n---\n# Hello\n")
+ assert fs._is_marimo_file(str(md_file)) is True
+
+ def test_markdown_non_marimo_file(
+ self, test_dir: Path, fs: OSFileSystem
+ ) -> None:
+ """Regular markdown is not a marimo file."""
+ md_file = test_dir / "readme.md"
+ md_file.write_text("# Just a readme\nSome text.\n")
+ assert fs._is_marimo_file(str(md_file)) is False
+
+ def test_non_python_non_markdown_file(
+ self, test_dir: Path, fs: OSFileSystem
+ ) -> None:
+ """Non-Python/non-markdown file is not a marimo file."""
+ txt_file = test_dir / "data.txt"
+ txt_file.write_text("some data\n")
+ assert fs._is_marimo_file(str(txt_file)) is False
+
+ def test_nonexistent_file(self, test_dir: Path, fs: OSFileSystem) -> None:
+ """Nonexistent file returns False (no exception)."""
+ assert fs._is_marimo_file(str(test_dir / "nope.py")) is False
+
+ def test_list_files_shows_markdown_marimo(
+ self, test_dir: Path, fs: OSFileSystem
+ ) -> None:
+ """list_files marks markdown marimo files correctly."""
+ md_file = test_dir / "notebook.md"
+ md_file.write_text("---\nmarimo-version: 0.1.0\n---\n# Hello\n")
+ files = fs.list_files(str(test_dir))
+ marimo_files = [f for f in files if f.is_marimo_file]
+ assert len(marimo_files) == 1
+ assert marimo_files[0].name == "notebook.md"
+
+
def test_search_basic(test_dir: Path, fs: OSFileSystem) -> None:
"""Test basic search functionality."""
# Create test files