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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix: normalize imported notebook filenames (#11620)
Myles Scolnick638ba8bae238
diff --git a/marimo/_cli/files/file_path.py b/marimo/_cli/files/file_path.pyindex edabc92656..1b5328219b 100644--- a/marimo/_cli/files/file_path.py+++ b/marimo/_cli/files/file_path.py@@ -14,6 +14,7 @@ from marimo import _loggersfrom marimo._cli.print import greenfrom marimo._utils import requestsfrom marimo._utils.marimo_path import MarimoPath+from marimo._utils.paths import is_valid_filenamefrom marimo._utils.url import is_urlLOGGER = _loggers.marimo_logger()@@ -294,6 +295,46 @@ class FileHandler(abc.ABC):pass+def _create_tmp_file_from_content(+ content: str,+ name: str,+ temp_dir: TemporaryDirectory[str],+ *,+ infer_suffix: bool = False,+) -> str:+ import click++ default_filename = Path(StaticNotebookReader.DEFAULT_FILENAME)+ if not is_valid_filename(name):+ name = default_filename.stem if infer_suffix else default_filename.name++ filename = Path(name)+ if infer_suffix and filename.suffix not in (".py", ".md", ".qmd"):+ suffix = (+ ".md"+ if "marimo-version" in content+ and "__generated_with" not in content+ else ".py"+ )+ filename = filename.with_suffix(suffix)+ if not is_valid_filename(filename.name):+ filename = default_filename.with_suffix(suffix)++ path_to_app = Path(temp_dir.name) / filename+ LOGGER.info("Creating temporary file")+ try:+ if os.path.lexists(path_to_app):+ raise FileExistsError(path_to_app)+ with path_to_app.open("x", encoding="utf-8") as file:+ file.write(content)+ except FileExistsError as e:+ raise click.ClickException(+ f"Cannot create notebook - {path_to_app.name} already exists"+ ) from e+ LOGGER.info("App saved to %s", path_to_app)+ return str(path_to_app)++class LocalFileHandler(FileHandler):def __init__(self, allow_new_file: bool, allow_directory: bool):self.allow_new_file = allow_new_file@@ -326,7 +367,7 @@ class LocalFileHandler(FileHandler):reader = StaticNotebookReader()if reader.can_read(name):content, filename = reader.read(name)- path_to_app = self._create_tmp_file_from_content(+ path_to_app = _create_tmp_file_from_content(content, filename, temp_dir)return path_to_app, temp_dir@@ -352,16 +393,6 @@ class LocalFileHandler(FileHandler):return name, None- @staticmethod- def _create_tmp_file_from_content(- content: str, name: str, temp_dir: TemporaryDirectory[str]- ) -> str:- LOGGER.info("Creating temporary file")- path_to_app = Path(temp_dir.name) / name- path_to_app.write_text(content, encoding="utf-8")- LOGGER.info("App saved to %s", path_to_app)- return str(path_to_app)-class RemoteFileHandler(FileHandler):def __init__(self) -> None:@@ -379,30 +410,11 @@ class RemoteFileHandler(FileHandler):import clickraise click.ClickException(f"Failed to read URL: {e}") # noqa: B904- path_to_app = self._create_tmp_file_from_content(- content, filename, temp_dir+ path_to_app = _create_tmp_file_from_content(+ content, filename, temp_dir, infer_suffix=True)return path_to_app, temp_dir- @staticmethod- def _create_tmp_file_from_content(- content: str, name: str, temp_dir: TemporaryDirectory[str]- ) -> str:- LOGGER.info("Creating temporary file")- path_to_app = Path(temp_dir.name) / name- # If doesn't end in .py, add it- if path_to_app.suffix not in (".py", ".md", ".qmd"):- if "__generated_with" in content:- path_to_app = path_to_app.with_suffix(".py")- elif "marimo-version" in content:- path_to_app = path_to_app.with_suffix(".md")- else:- # Fallback to .py- path_to_app = path_to_app.with_suffix(".py")- path_to_app.write_text(content, encoding="utf-8")- LOGGER.info("App saved to %s", path_to_app)- return str(path_to_app)-def validate_name(name: str, allow_new_file: bool, allow_directory: booldiff --git a/marimo/_utils/paths.py b/marimo/_utils/paths.pyindex 71716178d1..b2e6010d18 100644--- a/marimo/_utils/paths.py+++ b/marimo/_utils/paths.py@@ -1,15 +1,37 @@# Copyright 2026 Marimo. All rights reserved.from __future__ import annotations+import ntpathimport os+import reimport sys-from pathlib import Path+from pathlib import Path, PureWindowsPathfrom typing import TYPE_CHECKINGif TYPE_CHECKING:from importlib.abc import Traversable+def is_valid_filename(name: str) -> bool:+ """Whether a name is a portable filename of at most 255 UTF-8 bytes."""+ if (+ not name+ or name.endswith((".", " "))+ or re.search(r'[<>:"/\\|?*\x00-\x1f\x7f-\x9f]', name)+ ):+ return False++ try:+ if len(name.encode("utf-8")) > 255:+ return False+ except UnicodeEncodeError:+ return False++ if sys.version_info >= (3, 13):+ return not ntpath.isreserved(name)+ return not PureWindowsPath(name).is_reserved()++def import_files(filename: str) -> Traversable:from importlib.resources import files as importlib_filesdiff --git a/tests/_cli/test_file_path.py b/tests/_cli/test_file_path.pyindex 7c1f4cd748..de77c1ca5d 100644--- a/tests/_cli/test_file_path.py+++ b/tests/_cli/test_file_path.py@@ -18,6 +18,7 @@ from marimo._cli.files.file_path import (GitHubSourceReader,LocalFileReader,StaticNotebookReader,+ _create_tmp_file_from_content,get_gist_src_url,is_gist_src,is_github_src,@@ -560,6 +561,132 @@ def test_gist_source_reader() -> None:assert content_call_args[0] == expected_raw_url[email protected]("infer_suffix", [False, True])+ "entry", ["file", "directory", "symlink", "dangling-symlink", "hardlink"]+)[email protected]("filename", ["notebook.py", "../notebook.py"])+def test_temporary_filename_already_exists(+ tmp_path: Path, infer_suffix: bool, entry: str, filename: str+) -> None:+ original = tmp_path / "original.py"+ original.write_text("original", encoding="utf-8")+ missing = tmp_path / "missing.py"+ temp_directory = tempfile.TemporaryDirectory(dir=tmp_path)+ with temp_directory as directory:+ destination = Path(directory) / "notebook.py"+ if entry == "file":+ destination.write_text("original", encoding="utf-8")+ elif entry == "directory":+ destination.mkdir()+ elif entry == "symlink":+ destination.symlink_to(original)+ elif entry == "dangling-symlink":+ destination.symlink_to(missing)+ else:+ destination.hardlink_to(original)++ with pytest.raises(click.ClickException, match="already exists"):+ _create_tmp_file_from_content(+ "new content",+ filename,+ temp_directory,+ infer_suffix=infer_suffix,+ )++ assert original.read_text(encoding="utf-8") == "original"+ assert not missing.exists()+ if entry in ("file", "symlink", "hardlink"):+ assert destination.read_text(encoding="utf-8") == "original"+ elif entry == "directory":+ assert destination.is_dir()+ else:+ assert destination.is_symlink()+++ ("name", "content", "expected"),+ [+ ("example", "print('hello')", "example.py"),+ ("example.txt", "__generated_with = '0.1.0'", "example.py"),+ ("example.txt", "marimo-version: 0.1.0", "example.md"),+ ("example.txt", "__generated_with marimo-version", "example.py"),+ ("example.py", "marimo-version: 0.1.0", "example.py"),+ ("example.md", "__generated_with = '0.1.0'", "example.md"),+ ("example.qmd", "print('hello')", "example.qmd"),+ ("", "marimo-version: 0.1.0", "notebook.md"),+ ("file:name", "marimo-version: 0.1.0", "notebook.md"),+ ("../example.md", "marimo-version: 0.1.0", "notebook.md"),+ ("", "__generated_with marimo-version", "notebook.py"),+ ("file:name", "print('hello')", "notebook.py"),+ pytest.param(+ "a" * 252,+ "print('hello')",+ "a" * 252 + ".py",+ id="suffix-at-limit",+ ),+ pytest.param(+ "a" * 255, "print('hello')", "notebook.py", id="suffix-over-limit"+ ),+ pytest.param(+ "a" * 253 + ".x",+ "marimo-version: 0.1.0",+ "notebook.md",+ id="replacement-suffix-over-limit",+ ),+ pytest.param(+ "é" * 127,+ "marimo-version: 0.1.0",+ "notebook.md",+ id="unicode-suffix-over-limit",+ ),+ pytest.param(+ "a" * 256,+ "marimo-version: 0.1.0",+ "notebook.md",+ id="long-markdown-name",+ ),+ ],+)+def test_temporary_filename_suffix(+ name: str, content: str, expected: str+) -> None:+ temp_directory = tempfile.TemporaryDirectory()+ with temp_directory as directory:+ result = _create_tmp_file_from_content(+ content, name, temp_directory, infer_suffix=True+ )+ assert Path(result) == Path(directory) / expected+ assert Path(result).read_text(encoding="utf-8") == content+ with pytest.raises(click.ClickException, match="already exists"):+ _create_tmp_file_from_content(+ content + "\nnew content",+ name,+ temp_directory,+ infer_suffix=True,+ )+ assert Path(result).read_text(encoding="utf-8") == content+++def test_temporary_filename_created_before_open() -> None:+ temp_directory = tempfile.TemporaryDirectory()+ with temp_directory as directory:+ destination = Path(directory) / "notebook.py"++ def create_file(path: Path) -> bool:+ path.write_text("original", encoding="utf-8")+ return False++ with (+ patch("marimo._cli.files.file_path.os.path.lexists", create_file),+ pytest.raises(click.ClickException, match="already exists"),+ ):+ _create_tmp_file_from_content(+ "new content", "notebook.py", temp_directory+ )+ assert destination.read_text(encoding="utf-8") == "original"++class TestStaticNotebooks:VALID_HTML_CONTENT = dedent("""\<html>@@ -579,6 +706,113 @@ class TestStaticNotebooks:INVALID_HTML_CONTENT = """<html><body>no code</body></html>"""PYTHON_CODE = "import marimo as mo"+ @pytest.mark.parametrize("remote", [False, True], ids=["local", "remote"])+ @pytest.mark.parametrize(+ ("filename", "expected"),+ [+ (name, name)+ for name in [+ "custom.py",+ "custom.md",+ "custom.qmd",+ "test file.py",+ " café 测试 🚀.py",+ ".notebook.py",+ "a..b.py",+ "CONSOLE.py",+ "COM10.py",+ "%2e%2e%2fexample.py",+ ]+ ]+ + [+ (name, "notebook.py")+ for name in [+ None,+ "",+ " ",+ ".",+ "..",+ "...",+ "./custom.py",+ "../existing.py",+ "../../existing.py",+ "folder/../existing.py",+ "folder/custom.py",+ "folder//custom.py",+ "custom.py/",+ "/existing.py",+ "//server/share/custom.py",+ r"..\existing.py",+ r"folder\custom.py",+ r"folder/..\existing.py",+ r"\existing.py",+ r"C:\existing.py",+ "C:/existing.py",+ "C:existing.py",+ r"\\server\share\custom.py",+ r"\\?\C:\existing.py",+ r"\\.\NUL",+ "custom.py:stream",+ "custom.py.",+ "custom.py ",+ "custom?.py",+ "custom*.py",+ "custom|name.py",+ 'custom"name.py',+ "custom<name>.py",+ "NUL",+ "nul.py",+ "CON.py",+ "PRN.md",+ "AUX.qmd",+ "CONIN$.py",+ "CONOUT$.py",+ "COM1.py",+ "LPT9.py",+ "COM¹.py",+ "LPT².py",+ "NUL .py",+ "a" * 256 + ".py",+ "é" * 128 + ".py",+ *[+ f"custom{chr(char)}.py"+ for char in [0, 1, 9, 10, 13, 31, 127, 159]+ ],+ ]+ ],+ )+ def test_imported_filename(+ self, tmp_path: Path, remote: bool, filename: str | None, expected: str+ ) -> None:+ existing = tmp_path / "existing.py"+ existing.write_text("original", encoding="utf-8")+ if filename is None:+ filename = str(existing)+ html = (+ f"<marimo-code hidden>{uri_encode_component(self.PYTHON_CODE)}</marimo-code>"+ f"<marimo-filename hidden>{uri_encode_component(filename)}</marimo-filename>"+ )+ source = tmp_path / "source.html"+ source.write_text(html, encoding="utf-8")+ temp_directory = tempfile.TemporaryDirectory(dir=tmp_path)+ with (+ temp_directory as directory,+ patch(+ "marimo._cli.files.file_path.TemporaryDirectory",+ return_value=temp_directory,+ ),+ patch("marimo._utils.requests.get") as mock_get,+ ):+ mock_get.return_value = Response(200, html.encode("utf-8"), {})+ path, _ = validate_name(+ "https://example.com/source.html" if remote else str(source),+ allow_new_file=False,+ allow_directory=False,+ )+ assert Path(path) == Path(directory) / expected+ assert Path(path).read_text(encoding="utf-8") == self.PYTHON_CODE+ assert existing.read_text(encoding="utf-8") == "original"+@patch("marimo._utils.requests.get")@patch("marimo._cli.files.file_path.Path.read_text")def test_static_notebook_reader(self, mock_read_text, mock_get):diff --git a/tests/_utils/test_paths.py b/tests/_utils/test_paths.pyindex 3d7e1fe547..d25693c01d 100644--- a/tests/_utils/test_paths.py+++ b/tests/_utils/test_paths.py@@ -9,12 +9,38 @@ import pytestfrom marimo._utils.paths import (is_cloudpath,+ is_valid_filename,normalize_path,notebook_output_dir,pretty_path,)+ ("name", "expected"),+ [+ ("a" * 252 + ".py", True),+ ("a" * 253 + ".py", False),+ ("é" * 126 + ".py", True),+ ("é" * 127 + ".py", False),+ ("🚀" * 63 + ".py", True),+ ("🚀" * 64 + ".py", False),+ ("\ud800.py", False),+ ],+ ids=[+ "ascii-limit",+ "ascii-over",+ "unicode-limit",+ "unicode-over",+ "emoji-limit",+ "emoji-over",+ "invalid-unicode",+ ],+)+def test_filename_length(name: str, expected: bool) -> None:+ assert is_valid_filename(name) is expected++def test_normalize_path_makes_absolute() -> None:"""Test that relative paths are converted to absolute paths."""relative_path = Path("foo") / "bar"