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
refactor: clean up AppFileManager to be more testable (#7100)
Myles Scolnick1fd601a5d4ca
diff --git a/marimo/_ast/load.py b/marimo/_ast/load.pyindex 44192e1b4a..13d1caf589 100644--- a/marimo/_ast/load.py+++ b/marimo/_ast/load.py@@ -57,7 +57,7 @@ def _maybe_contents(filename: Optional[Union[str, Path]]) -> Optional[str]:# Used in tests and current fallback-def _dynamic_load(filename: str) -> Optional[App]:+def _dynamic_load(filename: str | Path) -> Optional[App]:"""Create and execute a module with the provided filename."""contents = _maybe_contents(filename)if not contents:@@ -225,7 +225,7 @@ FAILED_LOAD_NOTEBOOK_MESSAGE = ()-def load_app(filename: Optional[str]) -> Optional[App]:+def load_app(filename: Optional[str | Path]) -> Optional[App]:"""Load and return app from a marimo-generated module.Args:diff --git a/marimo/_islands/_island_generator.py b/marimo/_islands/_island_generator.pyindex 317f821824..54047ca732 100644--- a/marimo/_islands/_island_generator.py+++ b/marimo/_islands/_island_generator.py@@ -14,8 +14,8 @@ from marimo._ast.cell import Cell, CellConfigfrom marimo._ast.compiler import compile_cellfrom marimo._messaging.cell_output import CellOutputfrom marimo._output.utils import uri_encode_component-from marimo._server.file_manager import AppFileManagerfrom marimo._server.file_router import AppFileRouter+from marimo._server.notebook import AppFileManagerfrom marimo._types.ids import CellId_tfrom marimo._utils.marimo_path import MarimoPathfrom marimo._version import __version__diff --git a/marimo/_pyodide/bootstrap.py b/marimo/_pyodide/bootstrap.pyindex 8cca9f7323..1f506c3785 100644--- a/marimo/_pyodide/bootstrap.py+++ b/marimo/_pyodide/bootstrap.py@@ -18,9 +18,9 @@ from marimo._runtime.requests import (SerializedQueryParams,SetUIElementValueRequest,)-from marimo._server.file_manager import AppFileManagerfrom marimo._server.model import SessionModefrom marimo._server.models.models import SaveNotebookRequest+from marimo._server.notebook import AppFileManagerfrom marimo._utils.parse_dataclass import parse_rawif TYPE_CHECKING:diff --git a/marimo/_pyodide/pyodide_session.py b/marimo/_pyodide/pyodide_session.pyindex 06c70642a9..cefeae2132 100644--- a/marimo/_pyodide/pyodide_session.py+++ b/marimo/_pyodide/pyodide_session.py@@ -43,7 +43,6 @@ from marimo._runtime.utils.set_ui_element_request_manager import (SetUIElementRequestManager,)from marimo._server.export.exporter import Exporter-from marimo._server.file_manager import AppFileManagerfrom marimo._server.files.os_file_system import OSFileSystemfrom marimo._server.model import SessionModefrom marimo._server.models.export import ExportAsHTMLRequest@@ -69,6 +68,7 @@ from marimo._server.models.models import (SaveAppConfigurationRequest,SaveNotebookRequest,)+from marimo._server.notebook import AppFileManagerfrom marimo._server.session.session_view import SessionViewfrom marimo._snippets.snippets import read_snippetsfrom marimo._types.ids import CellId_tdiff --git a/marimo/_runtime/layout/layout.py b/marimo/_runtime/layout/layout.pyindex 534af31256..e83c1c67e4 100644--- a/marimo/_runtime/layout/layout.py+++ b/marimo/_runtime/layout/layout.py@@ -4,13 +4,16 @@ from __future__ import annotationsimport jsonimport osfrom dataclasses import dataclass-from typing import Any, Optional+from typing import TYPE_CHECKING, Any, Optionalfrom marimo import _loggersfrom marimo._utils.data_uri import from_data_uriLOGGER = _loggers.marimo_logger()+if TYPE_CHECKING:+ from pathlib import Path+@dataclassclass LayoutConfig:@@ -21,7 +24,7 @@ class LayoutConfig:def save_layout_config(- directory: str, app_name: str, config: LayoutConfig+ directory: str | Path, app_name: str, config: LayoutConfig) -> str:"""Save the layout configuration to disk@@ -50,7 +53,7 @@ def save_layout_config(def read_layout_config(- directory: str, filename: str+ directory: str | Path, filename: str) -> Optional[LayoutConfig]:"""Read the layout configuration from disk.diff --git a/marimo/_server/export/__init__.py b/marimo/_server/export/__init__.pyindex 2170dcefa7..4a3932e95a 100644--- a/marimo/_server/export/__init__.py+++ b/marimo/_server/export/__init__.py@@ -25,11 +25,11 @@ from marimo._messaging.types import KernelMessagefrom marimo._output.hypertext import patch_html_for_non_interactive_outputfrom marimo._runtime.requests import AppMetadata, SerializedCLIArgsfrom marimo._server.export.exporter import Exporter-from marimo._server.file_manager import AppFileManagerfrom marimo._server.file_router import AppFileRouterfrom marimo._server.model import ConnectionState, SessionConsumer, SessionModefrom marimo._server.models.export import ExportAsHTMLRequestfrom marimo._server.models.models import InstantiateRequest+from marimo._server.notebook import AppFileManagerfrom marimo._server.session.session_view import SessionViewfrom marimo._types.ids import ConsumerIdfrom marimo._utils.marimo_path import MarimoPathdiff --git a/marimo/_server/file_manager.py b/marimo/_server/file_manager.pydeleted file mode 100644index e1074dafe5..0000000000--- a/marimo/_server/file_manager.py+++ /dev/null@@ -1,443 +0,0 @@-# Copyright 2024 Marimo. All rights reserved.-from __future__ import annotations--import os-import pathlib-import shutil-from pathlib import Path-from typing import Any, Optional, Union--from marimo import _loggers-from marimo._ast import codegen, load-from marimo._ast.app import App, InternalApp-from marimo._ast.app_config import overloads_from_env-from marimo._ast.cell import CellConfig-from marimo._config.config import ExportType, SqlOutputType, WidthType-from marimo._convert.converters import MarimoConvert-from marimo._runtime.layout.layout import (- LayoutConfig,- read_layout_config,- save_layout_config,-)-from marimo._schemas.serialization import Header, NotebookSerializationV1-from marimo._server.api.status import HTTPException, HTTPStatus-from marimo._server.models.models import (- CopyNotebookRequest,- SaveNotebookRequest,-)-from marimo._server.utils import canonicalize_filename-from marimo._types.ids import CellId_t--LOGGER = _loggers.marimo_logger()---class AppFileManager:- def __init__(- self,- filename: Optional[Union[str, Path]],- *,- default_width: WidthType | None = None,- default_auto_download: list[ExportType] | None = None,- default_sql_output: SqlOutputType | None = None,- ) -> None:- self.filename = str(filename) if filename else None- self._default_width: WidthType | None = default_width- self._default_auto_download: list[ExportType] | None = (- default_auto_download- )- self._default_sql_output: SqlOutputType | None = default_sql_output- self.app = self._load_app(self.path)- # Track the last saved content to avoid reloading our own writes- self._last_saved_content: Optional[str] = None-- @staticmethod- def from_app(app: InternalApp) -> AppFileManager:- manager = AppFileManager(None)- manager.app = app- return manager-- def reload(self) -> set[CellId_t]:- """- Reload the app from the file.-- Return any new cell IDs that were added or code that was changed.- """- prev_cell_manager = self.app.cell_manager- self.app = self._load_app(self.path)- self.app.cell_manager.sort_cell_ids_by_similarity(prev_cell_manager)-- # Return the changes cell IDs- prev_cell_ids = set(prev_cell_manager.cell_ids())- current_cell_ids = set(self.app.cell_manager.cell_ids())-- # Capture deleted cells- changed_cell_ids: set[CellId_t] = prev_cell_ids - current_cell_ids- for cell_id in current_cell_ids:- if cell_id not in prev_cell_ids:- changed_cell_ids.add(cell_id)- new_code = self.app.cell_manager.get_cell_code(cell_id)- prev_code = prev_cell_manager.get_cell_code(cell_id)- if new_code != prev_code:- changed_cell_ids.add(cell_id)- return changed_cell_ids-- def _is_same_path(self, filename: str) -> bool:- if self.filename is None:- return False- return os.path.abspath(self.filename) == os.path.abspath(filename)-- def _assert_path_does_not_exist(self, filename: str) -> None:- if os.path.exists(filename):- raise HTTPException(- status_code=HTTPStatus.BAD_REQUEST,- detail=f"File {filename} already exists",- )-- def _create_parent_directories(self, filename: str) -> None:- try:- pathlib.Path(filename).parent.mkdir(parents=True, exist_ok=True)- except Exception:- pass-- def _create_file(- self,- filename: str,- contents: str = "",- ) -> None:- self._create_parent_directories(filename)- try:- Path(filename).write_text(contents, encoding="utf-8")- except Exception as err:- raise HTTPException(- status_code=HTTPStatus.SERVER_ERROR,- detail=f"Failed to save file {filename}",- ) from err-- def _rename_file(self, new_filename: str) -> None:- assert self.filename is not None- self._create_parent_directories(new_filename)- try:- # Use pathlib.Path for cross-platform path handling- src_path = pathlib.Path(self.filename)- dst_path = pathlib.Path(new_filename)- src_path.rename(dst_path)- except Exception as err:- raise HTTPException(- status_code=HTTPStatus.SERVER_ERROR,- detail=f"Failed to rename from {self.filename} to {new_filename}",- ) from err-- def _save_file(- self,- filename: str,- *,- notebook: NotebookSerializationV1,- # Whether or not to persist the app to the file system- persist: bool,- # Whether save was triggered by a rename- previous_filename: Optional[str] = None,- ) -> str:- LOGGER.debug("Saving app to %s", filename)-- type_changed = (- previous_filename and filename[-2:] != previous_filename[-2:]- )- if filename.endswith(".md") or filename.endswith(".qmd"):- # TODO: Potentially restructure, such that code compilation doesn't- # have to occur multiple times.- from marimo._server.export.exporter import Exporter-- previous = None- if previous_filename:- previous = Path(previous_filename)- contents, _ = Exporter().export_as_md(- self.app.to_ir(),- self.filename,- previous,- )- else:- # Header might be better kept on the AppConfig side, opposed to- # reparsing it. Also would allow for md equivalent in a field like- # `description`.- if type_changed:- from marimo._utils.inline_script_metadata import (- get_headers_from_markdown,- )-- with open(filename, encoding="utf-8") as f:- markdown = f.read()- headers = get_headers_from_markdown(markdown)- header_comments = headers.get("header", None) or headers.get(- "pyproject", None- )- else:- header_comments = codegen.get_header_comments(filename)-- contents = MarimoConvert.from_ir(- NotebookSerializationV1(- app=notebook.app,- cells=notebook.cells,- header=Header(value=header_comments or ""),- )- ).to_py()-- if persist:- self._create_file(filename, contents)- # Record the last saved content to avoid reloading our own writes- self._last_saved_content = contents.strip()-- if self._is_unnamed():- self.rename(filename)-- return contents-- def _load_app(self, path: Optional[str]) -> InternalApp:- """Read the app from the file."""- app = load.load_app(path)- default = overloads_from_env()-- if app is None:- kwargs: dict[str, Any] = default.asdict()- # Add defaults if it is a new file- if self._default_width is not None:- kwargs["width"] = self._default_width- if self._default_auto_download is not None:- kwargs["auto_download"] = self._default_auto_download- if self._default_sql_output is not None:- kwargs["sql_output"] = self._default_sql_output-- empty_app = InternalApp(App(**kwargs))- empty_app.cell_manager.register_cell(- cell_id=None,- code="",- config=CellConfig(),- )- return empty_app- # Manually extend config defaults- app._config.update(default.asdict_difference())-- result = InternalApp(app)- # Ensure at least one cell- result.cell_manager.ensure_one_cell()- return result-- def rename(self, new_filename: str) -> None:- """Rename the file."""- new_filename = canonicalize_filename(new_filename)-- if self._is_same_path(new_filename):- return-- self._assert_path_does_not_exist(new_filename)-- needs_save = False- # Check if filename is not None to satisfy mypy's type checking.- # This ensures that filename is treated as a non-optional str,- # preventing potential type errors in subsequent code.- if self.is_notebook_named and self.filename is not None:- # Force a save after rename in case filetype changed.- needs_save = self.filename[-3:] != new_filename[-3:]- self._rename_file(new_filename)- else:- self._create_file(new_filename)-- previous_filename = self.filename- self.filename = new_filename- if needs_save:- self._save_file(- self.filename,- notebook=self.app.to_ir(),- persist=True,- previous_filename=previous_filename,- )-- def read_layout_config(self) -> Optional[LayoutConfig]:- if self.app.config.layout_file is not None and isinstance(- self.filename, str- ):- app_dir = os.path.dirname(self.filename)- layout = read_layout_config(app_dir, self.app.config.layout_file)- return layout-- return None-- def read_css_file(self) -> Optional[str]:- css_file = self.app.config.css_file- if not css_file or not self.filename:- return None- return read_css_file(css_file, self.filename)-- def read_html_head_file(self) -> Optional[str]:- html_head_file = self.app.config.html_head_file- if not html_head_file or not self.filename:- return None- return read_html_head_file(html_head_file, self.filename)-- @property- def path(self) -> Optional[str]:- if self.filename is None:- return None- try:- return os.path.abspath(self.filename)- except AttributeError:- return None-- def save_app_config(self, config: dict[str, Any]) -> str:- """Save the app configuration."""- # Update the file with the latest app config- # TODO(akshayka): Only change the `app = marimo.App` line (at top level- # of file), instead of overwriting the whole file.- self.app.update_config(config)- if self.filename is not None:- return self._save_file(- self.filename,- notebook=self.app.to_ir(),- persist=True,- )- return ""-- def save(self, request: SaveNotebookRequest) -> str:- """Save the current app."""- cell_ids, codes, configs, names, filename, layout = (- request.cell_ids,- request.codes,- request.configs,- request.names,- request.filename,- request.layout,- )- filename = canonicalize_filename(filename)- self.app.with_data(- cell_ids=cell_ids,- codes=codes,- names=names,- configs=configs,- )-- if self.is_notebook_named and not self._is_same_path(filename):- raise HTTPException(- status_code=HTTPStatus.BAD_REQUEST,- detail="Save handler cannot rename files.",- )-- # save layout- if layout is not None:- app_dir = os.path.dirname(filename)- app_name = os.path.basename(filename)- layout_filename = save_layout_config(- app_dir, app_name, LayoutConfig(**layout)- )- self.app.update_config({"layout_file": layout_filename})- else:- # Remove the layout from the config- # We don't remove the layout file from the disk to avoid- # deleting state that the user might want to keep- self.app.update_config({"layout_file": None})- return self._save_file(- filename,- notebook=self.app.to_ir(),- persist=request.persist,- )-- def copy(self, request: CopyNotebookRequest) -> str:- source, destination = request.source, request.destination- shutil.copy(source, destination)- return os.path.basename(destination)-- def to_code(self) -> str:- """Read the contents of the unsaved file."""- return MarimoConvert.from_ir(self.app.to_ir()).to_py()-- def _is_unnamed(self) -> bool:- return self.filename is None-- @property- def is_notebook_named(self) -> bool:- """Whether the notebook has a name."""- return self.filename is not None-- def read_file(self) -> str:- """Read the contents of the file."""- if self.filename is None:- raise HTTPException(- status_code=HTTPStatus.BAD_REQUEST,- detail="Cannot read code from an unnamed notebook",- )- return Path(self.filename).read_text(encoding="utf-8")-- def file_content_matches_last_save(self) -> bool:- """Check if the current file content matches the last saved content.-- This is used to avoid reloading the file when we detect our own writes.-- Returns:- bool: True if the file content matches the last save, False otherwise.- """- if self.filename is None or self._last_saved_content is None:- return False-- try:- current_content = Path(self.filename).read_text(encoding="utf-8")- return current_content.strip() == self._last_saved_content- except Exception as e:- LOGGER.debug(- f"Error reading file to check if content matches: {e}"- )- return False---def read_css_file(css_file: str, filename: Optional[str]) -> Optional[str]:- """Read the contents of a CSS file.-- Args:- css_file: The path to the CSS file.- filename: The filename of the notebook.-- Returns:- The contents of the CSS file.- """- if not css_file:- return None-- filepath = Path(css_file)-- # If not an absolute path, make it absolute using the filename- if not filepath.is_absolute():- if not filename:- return None- filepath = Path(filename).parent / filepath-- if not filepath.exists():- LOGGER.error("CSS file %s does not exist", filepath)- return None- try:- return filepath.read_text(encoding="utf-8")- except OSError as e:- LOGGER.warning(- "Failed to open custom CSS file %s for reading: %s",- filepath,- str(e),- )- return None---def read_html_head_file(- html_head_file: str, filename: Optional[str]-) -> Optional[str]:- if not html_head_file or not filename:- return None-- app_dir = Path(filename).parent- filepath = app_dir / html_head_file- if not filepath.exists():- LOGGER.error("HTML head file %s does not exist", html_head_file)- return None- try:- return filepath.read_text(encoding="utf-8")- except OSError as e:- LOGGER.warning(- "Failed to open HTML head file %s for reading: %s",- filepath,- str(e),- )- return Nonediff --git a/marimo/_server/file_router.py b/marimo/_server/file_router.pyindex 3af452cd3c..a46c117d68 100644--- a/marimo/_server/file_router.py+++ b/marimo/_server/file_router.py@@ -11,10 +11,10 @@ from typing import TYPE_CHECKING, Optionalfrom marimo import _loggersfrom marimo._config.config import ExportType, SqlOutputType, WidthTypefrom marimo._server.api.status import HTTPException, HTTPStatus-from marimo._server.file_manager import AppFileManagerfrom marimo._server.files.os_file_system import natural_sort_filefrom marimo._server.models.files import FileInfofrom marimo._server.models.home import MarimoFile+from marimo._server.notebook import AppFileManagerfrom marimo._utils.marimo_path import MarimoPathif TYPE_CHECKING:diff --git a/marimo/_server/notebook/__init__.py b/marimo/_server/notebook/__init__.pynew file mode 100644index 0000000000..e6db6e4da1--- /dev/null+++ b/marimo/_server/notebook/__init__.py@@ -0,0 +1,25 @@+# Copyright 2024 Marimo. All rights reserved.+"""Notebook file management and storage abstractions."""++from marimo._server.notebook.file_manager import (+ AppFileManager,+ read_css_file,+ read_html_head_file,+)+from marimo._server.notebook.serializer import (+ MarkdownNotebookSerializer,+ NotebookSerializer,+ PythonNotebookSerializer,+)+from marimo._server.notebook.storage import FilesystemStorage, StorageInterface++__all__ = [+ "AppFileManager",+ "FilesystemStorage",+ "NotebookSerializer",+ "MarkdownNotebookSerializer",+ "PythonNotebookSerializer",+ "StorageInterface",+ "read_css_file",+ "read_html_head_file",+]diff --git a/marimo/_server/notebook/file_manager.py b/marimo/_server/notebook/file_manager.pynew file mode 100644index 0000000000..00defd2049--- /dev/null+++ b/marimo/_server/notebook/file_manager.py@@ -0,0 +1,560 @@+# Copyright 2024 Marimo. All rights reserved.+from __future__ import annotations++import os+from pathlib import Path+from typing import Any, Optional++from marimo import _loggers+from marimo._ast import load+from marimo._ast.app import App, InternalApp+from marimo._ast.app_config import overloads_from_env+from marimo._ast.cell import CellConfig+from marimo._config.config import ExportType, SqlOutputType, WidthType+from marimo._runtime.layout.layout import (+ LayoutConfig,+ read_layout_config,+ save_layout_config,+)+from marimo._schemas.serialization import NotebookSerializationV1+from marimo._server.api.status import HTTPException, HTTPStatus+from marimo._server.models.models import (+ CopyNotebookRequest,+ SaveNotebookRequest,+)+from marimo._server.notebook.serializer import get_format_handler+from marimo._server.notebook.storage import FilesystemStorage, StorageInterface+from marimo._server.utils import canonicalize_filename+from marimo._types.ids import CellId_t++LOGGER = _loggers.marimo_logger()+++class AppFileManager:+ """Manages notebook file operations.++ Responsibilities:+ - App lifecycle management (loading, reloading, state tracking)+ - Coordination between storage backends and serialization+ - Configuration and layout management+ """++ def __init__(+ self,+ filename: Optional[str | Path],+ *,+ storage: Optional[StorageInterface] = None,+ default_width: WidthType | None = None,+ default_auto_download: list[ExportType] | None = None,+ default_sql_output: SqlOutputType | None = None,+ ) -> None:+ self._filename = _maybe_path(filename)++ self.storage: StorageInterface = storage or FilesystemStorage()++ # Configuration defaults+ self._default_width: WidthType | None = default_width+ self._default_auto_download: list[ExportType] | None = (+ default_auto_download+ )+ self._default_sql_output: SqlOutputType | None = default_sql_output++ # Load the app+ self.app = self._load_app(self.path)++ # Track the last saved content to avoid reloading our own writes+ self._last_saved_content: Optional[str] = None++ @property+ def filename(self) -> Optional[str]:+ """Get the current filename as a Path object."""+ return str(self._filename) if self._filename is not None else None++ @filename.setter+ def filename(self, value: Optional[str | Path]) -> None:+ """Set the filename, automatically converting strings to Path objects."""+ self._filename = _maybe_path(value)++ @staticmethod+ def from_app(app: InternalApp) -> AppFileManager:+ """Create AppFileManager from an existing InternalApp.++ Args:+ app: The internal app to wrap++ Returns:+ AppFileManager instance+ """+ manager = AppFileManager(None)+ manager.app = app+ return manager++ def reload(self) -> set[CellId_t]:+ """Reload the app from storage.++ Detects changes by comparing cell IDs and code between the previous+ and newly loaded versions.++ Returns:+ Set of cell IDs that were added, deleted, or modified+ """+ prev_cell_manager = self.app.cell_manager+ self.app = self._load_app(self.path)+ self.app.cell_manager.sort_cell_ids_by_similarity(prev_cell_manager)++ # Return the changed cell IDs+ prev_cell_ids = set(prev_cell_manager.cell_ids())+ current_cell_ids = set(self.app.cell_manager.cell_ids())++ # Capture deleted cells+ changed_cell_ids: set[CellId_t] = prev_cell_ids - current_cell_ids++ # Check for added or modified cells+ for cell_id in current_cell_ids:+ if cell_id not in prev_cell_ids:+ changed_cell_ids.add(cell_id)+ else:+ new_code = self.app.cell_manager.get_cell_code(cell_id)+ prev_code = prev_cell_manager.get_cell_code(cell_id)+ if new_code != prev_code:+ changed_cell_ids.add(cell_id)++ return changed_cell_ids++ def _is_same_path(self, path: Path) -> bool:+ """Check if the given path is the same as the current filename.++ Args:+ path: Path to compare++ Returns:+ True if paths refer to the same location+ """+ if self._filename is None:+ return False+ return self.storage.is_same_path(self._filename, path)++ def _assert_path_does_not_exist(self, path: Path) -> None:+ """Ensure path doesn't exist, raise HTTPException if it does.++ Args:+ path: Path to check++ Raises:+ HTTPException: If path already exists+ """+ if self.storage.exists(path):+ raise HTTPException(+ status_code=HTTPStatus.BAD_REQUEST,+ detail=f"File {path} already exists",+ )++ def _save_file(+ self,+ path: Path,+ *,+ notebook: NotebookSerializationV1,+ persist: bool,+ previous_path: Optional[Path] = None,+ ) -> str:+ """Save notebook to storage using appropriate format handler.++ Args:+ path: Target file path+ notebook: Notebook in IR format+ persist: Whether to actually write to storage+ previous_path: Previous file path (for format conversions)++ Returns:+ Serialized notebook contents+ """+ LOGGER.debug("Saving app to %s", path)++ # Get the appropriate format handler and serialize+ handler = get_format_handler(path)+ contents = handler.serialize(notebook, path, previous_path)++ if persist:+ self.storage.write(path, contents)+ # Record the last saved content to avoid reloading our own writes+ self._last_saved_content = contents.strip()++ # If this is a new unnamed notebook, update the filename+ if self._is_unnamed():+ self._filename = path++ return contents++ def _load_app(self, path: Optional[str]) -> InternalApp:+ """Load app from storage.++ Args:+ path: Path to load from (None for new notebooks)++ Returns:+ Loaded InternalApp instance+ """+ # Load app using existing loader+ app = load.load_app(path)+ default = overloads_from_env()++ if app is None:+ # Create new empty app with defaults+ kwargs: dict[str, Any] = default.asdict()++ # Add custom defaults if provided+ if self._default_width is not None:+ kwargs["width"] = self._default_width+ if self._default_auto_download is not None:+ kwargs["auto_download"] = self._default_auto_download+ if self._default_sql_output is not None:+ kwargs["sql_output"] = self._default_sql_output++ empty_app = InternalApp(App(**kwargs))+ empty_app.cell_manager.register_cell(+ cell_id=None,+ code="",+ config=CellConfig(),+ )+ return empty_app++ # Manually extend config defaults+ app._config.update(default.asdict_difference())++ result = InternalApp(app)+ # Ensure at least one cell+ result.cell_manager.ensure_one_cell()+ return result++ def rename(self, new_filename: str | Path) -> None:+ """Rename the notebook file.++ Args:+ new_filename: New filename (will be canonicalized)++ Raises:+ HTTPException: If rename fails or target exists+ """+ new_path = Path(canonicalize_filename(str(new_filename)))++ if self._is_same_path(new_path):+ return++ self._assert_path_does_not_exist(new_path)++ needs_save = False++ if self.is_notebook_named and self._filename is not None:+ # Force a save after rename if filetype changed+ needs_save = self._filename.suffix != new_path.suffix+ self.storage.rename(self._filename, new_path)+ else:+ # Create new file for unnamed notebooks+ self.storage.write(new_path, "")++ previous_filename = self._filename+ self._filename = new_path++ if needs_save:+ self._save_file(+ new_path,+ notebook=self.app.to_ir(),+ persist=True,+ previous_path=previous_filename,+ )++ def read_layout_config(self) -> Optional[LayoutConfig]:+ """Read layout configuration file.++ Returns:+ Layout configuration or None if not configured+ """+ if self.app.config.layout_file is not None and self._filename:+ app_dir = self._filename.parent+ layout = read_layout_config(app_dir, self.app.config.layout_file)+ return layout++ return None++ def read_css_file(self) -> Optional[str]:+ """Read custom CSS file.++ Returns:+ CSS content or None if not configured+ """+ css_file = self.app.config.css_file+ if not css_file or not self._filename:+ return None+ return self.storage.read_related_file(self._filename, css_file)++ def read_html_head_file(self) -> Optional[str]:+ """Read custom HTML head file.++ Returns:+ HTML head content or None if not configured+ """+ html_head_file = self.app.config.html_head_file+ if not html_head_file or not self._filename:+ return None+ return self.storage.read_related_file(self._filename, html_head_file)++ @property+ def path(self) -> Optional[str]:+ """Get absolute path to notebook file as string.++ Returns:+ Absolute path as string or None if unnamed+ """+ if self._filename is None:+ return None+ return os.path.abspath(str(self._filename))++ def save_app_config(self, config: dict[str, Any]) -> str:+ """Save app configuration.++ Args:+ config: Configuration dictionary++ Returns:+ Serialized notebook content+ """+ self.app.update_config(config)+ if self._filename is not None:+ return self._save_file(+ self._filename,+ notebook=self.app.to_ir(),+ persist=True,+ )+ return ""++ def save(self, request: SaveNotebookRequest) -> str:+ """Save the notebook.++ Args:+ request: Save request with cell data and options++ Returns:+ Serialized notebook content++ Raises:+ HTTPException: If save fails or tries to rename+ """+ cell_ids, codes, configs, names, filename, layout = (+ request.cell_ids,+ request.codes,+ request.configs,+ request.names,+ request.filename,+ request.layout,+ )++ filename_path = Path(canonicalize_filename(filename))++ # Update app with new cell data+ self.app.with_data(+ cell_ids=cell_ids,+ codes=codes,+ names=names,+ configs=configs,+ )++ if self.is_notebook_named and not self._is_same_path(filename_path):+ raise HTTPException(+ status_code=HTTPStatus.BAD_REQUEST,+ detail="Save handler cannot rename files.",+ )++ # Save layout if provided+ if layout is not None:+ app_dir = filename_path.parent+ app_name = filename_path.name+ layout_filename = save_layout_config(+ app_dir, app_name, LayoutConfig(**layout)+ )+ self.app.update_config({"layout_file": layout_filename})+ else:+ # Remove the layout from the config+ self.app.update_config({"layout_file": None})++ return self._save_file(+ filename_path,+ notebook=self.app.to_ir(),+ persist=request.persist,+ )++ def copy(self, request: CopyNotebookRequest) -> str:+ """Copy a notebook file.++ Args:+ request: Copy request with source and destination++ Returns:+ Basename of destination file++ Raises:+ HTTPException: If source doesn't exist, destination exists, or copy fails+ """+ source = Path(request.source)+ destination = Path(request.destination)++ # Validate source exists+ if not self.storage.exists(source):+ raise HTTPException(+ status_code=HTTPStatus.BAD_REQUEST,+ detail=f"Source file {source} does not exist",+ )++ # Check destination doesn't already exist+ self._assert_path_does_not_exist(destination)++ try:+ content = self.storage.read(source)+ self.storage.write(destination, content)+ except HTTPException:+ # Re-raise HTTP exceptions as-is+ raise+ except Exception as err:+ raise HTTPException(+ status_code=HTTPStatus.SERVER_ERROR,+ detail=f"Failed to copy from {source} to {destination}",+ ) from err++ return destination.name++ def to_code(self) -> str:+ """Convert app to Python code without saving.++ Returns:+ Python code representation of the notebook+ """+ from marimo._convert.converters import MarimoConvert++ return MarimoConvert.from_ir(self.app.to_ir()).to_py()++ def _is_unnamed(self) -> bool:+ """Check if notebook is unnamed.++ Returns:+ True if filename is None+ """+ return self._filename is None++ @property+ def is_notebook_named(self) -> bool:+ """Check if notebook has a name.++ Returns:+ True if filename is not None+ """+ return self._filename is not None++ def read_file(self) -> str:+ """Read the current file contents from storage.++ Returns:+ File contents as string++ Raises:+ HTTPException: If notebook is unnamed or read fails+ """+ if self._filename is None:+ raise HTTPException(+ status_code=HTTPStatus.BAD_REQUEST,+ detail="Cannot read code from an unnamed notebook",+ )+ return self.storage.read(self._filename)++ def file_content_matches_last_save(self) -> bool:+ """Check if current file content matches the last save.++ Used to avoid reloading the file when we detect our own writes.++ Returns:+ True if content matches last save, False otherwise+ """+ if self._filename is None or self._last_saved_content is None:+ return False++ try:+ current_content = self.storage.read(self._filename)+ return current_content.strip() == self._last_saved_content+ except Exception as e:+ LOGGER.debug(+ f"Error reading file to check if content matches: {e}"+ )+ return False+++def read_css_file(css_file: str, filename: Optional[str]) -> Optional[str]:+ """Read the contents of a CSS file.++ Args:+ css_file: The path to the CSS file.+ filename: The filename of the notebook.++ Returns:+ The contents of the CSS file.+ """+ if not css_file:+ return None++ filepath = Path(css_file)++ # If not an absolute path, make it absolute using the filename+ if not filepath.is_absolute():+ if not filename:+ return None+ filepath = Path(filename).parent / filepath++ if not filepath.exists():+ LOGGER.error("CSS file %s does not exist", filepath)+ return None+ try:+ return filepath.read_text(encoding="utf-8")+ except OSError as e:+ LOGGER.warning(+ "Failed to open custom CSS file %s for reading: %s",+ filepath,+ str(e),+ )+ return None+++def read_html_head_file(+ html_head_file: str, filename: Optional[str]+) -> Optional[str]:+ """Read the contents of an HTML head file.++ Args:+ html_head_file: The path to the HTML head file.+ filename: The filename of the notebook.++ Returns:+ The contents of the HTML head file.+ """+ if not html_head_file or not filename:+ return None++ app_dir = Path(filename).parent+ filepath = app_dir / html_head_file+ if not filepath.exists():+ LOGGER.error("HTML head file %s does not exist", html_head_file)+ return None+ try:+ return filepath.read_text(encoding="utf-8")+ except OSError as e:+ LOGGER.warning(+ "Failed to open HTML head file %s for reading: %s",+ filepath,+ str(e),+ )+ return None+++def _maybe_path(path: Optional[str | Path]) -> Optional[Path]:+ """Convert a string or Path to a Path object."""+ if path is None:+ return None+ if isinstance(path, Path):+ return path+ return Path(path)diff --git a/marimo/_server/notebook/serializer.py b/marimo/_server/notebook/serializer.pynew file mode 100644index 0000000000..4435c71ea2--- /dev/null+++ b/marimo/_server/notebook/serializer.py@@ -0,0 +1,149 @@+# Copyright 2024 Marimo. All rights reserved.+"""Format handlers for different notebook file formats."""++from __future__ import annotations++from abc import ABC, abstractmethod+from pathlib import Path+from typing import Optional++from marimo._ast import codegen+from marimo._convert.converters import MarimoConvert+from marimo._schemas.serialization import Header, NotebookSerializationV1+++class NotebookSerializer(ABC):+ """Abstract base class for notebook format handlers."""++ @abstractmethod+ def serialize(+ self,+ notebook: NotebookSerializationV1,+ path: Path,+ previous_path: Optional[Path] = None,+ ) -> str:+ """Convert notebook IR to the target format.++ Args:+ notebook: Notebook in intermediate representation+ path: Target file path+ previous_path: Previous file path (for format conversions)++ Returns:+ Serialized notebook content as string+ """+ pass++ @abstractmethod+ def extract_header(self, path: Path) -> Optional[str]:+ """Extract header/metadata from an existing file.++ Args:+ path: File path to extract header from++ Returns:+ Header content or None+ """+ pass+++class PythonNotebookSerializer(NotebookSerializer):+ """Handler for Python (.py) notebook files."""++ def serialize(+ self,+ notebook: NotebookSerializationV1,+ path: Path,+ previous_path: Optional[Path] = None,+ ) -> str:+ """Serialize notebook to Python format.++ Handles header preservation when converting from other formats.+ """+ # Path changed, extract headers from previous path+ if previous_path is not None and path.suffix != previous_path.suffix:+ prev_header = get_format_handler(previous_path).extract_header(+ previous_path+ )+ header_comments = prev_header+ else:+ header_comments = self.extract_header(path)++ # Convert to Python with headers+ contents = MarimoConvert.from_ir(+ NotebookSerializationV1(+ app=notebook.app,+ cells=notebook.cells,+ header=Header(value=header_comments or ""),+ )+ ).to_py()++ return contents++ def extract_header(self, path: Path) -> Optional[str]:+ """Extract header comments from Python file."""+ return codegen.get_header_comments(path)+++class MarkdownNotebookSerializer(NotebookSerializer):+ """Handler for Markdown (.md) notebook files."""++ def serialize(+ self,+ notebook: NotebookSerializationV1,+ path: Path,+ previous_path: Optional[Path] = None,+ ) -> str:+ """Serialize notebook to Markdown format."""+ from marimo._server.export.exporter import Exporter++ contents, _ = Exporter().export_as_md(+ notebook,+ str(path),+ previous_path,+ )++ return contents++ def extract_header(self, path: Path) -> Optional[str]:+ """Extract YAML frontmatter from Markdown file."""+ from marimo._utils.inline_script_metadata import (+ get_headers_from_markdown,+ )++ markdown = path.read_text(encoding="utf-8")+ headers = get_headers_from_markdown(markdown)+ return headers.get("header", None) or headers.get("pyproject", None)+++# Default format handlers+DEFAULT_FORMAT_HANDLERS = {+ ".py": PythonNotebookSerializer(),+ ".md": MarkdownNotebookSerializer(),+ ".qmd": MarkdownNotebookSerializer(),+}+++def get_format_handler(path: Path) -> NotebookSerializer:+ """Get the appropriate format handler for a file.++ Args:+ path: File path++ Returns:+ Appropriate format handler++ Raises:+ ValueError: If no handler supports the file format+ """+ # Ensure path is a Path object+ if not isinstance(path, Path):+ path = Path(path)++ ext = path.suffix+ handler = DEFAULT_FORMAT_HANDLERS.get(ext)+ if handler is None:+ raise ValueError(+ f"No format handler found for {path}. Supported extensions: {list(DEFAULT_FORMAT_HANDLERS.keys())}"+ )+ return handlerdiff --git a/marimo/_server/notebook/storage.py b/marimo/_server/notebook/storage.pynew file mode 100644index 0000000000..9e344edd77--- /dev/null+++ b/marimo/_server/notebook/storage.py@@ -0,0 +1,234 @@+# Copyright 2024 Marimo. All rights reserved.+"""Storage abstraction for notebook persistence."""++from __future__ import annotations++from abc import ABC, abstractmethod+from pathlib import Path+from typing import Optional++from marimo import _loggers+from marimo._server.api.status import HTTPException, HTTPStatus++LOGGER = _loggers.marimo_logger()+++class StorageInterface(ABC):+ """Abstract interface for storage operations."""++ @abstractmethod+ def read(self, path: Path) -> str:+ """Read content from storage.++ Args:+ path: Path to read from++ Returns:+ File contents as string++ Raises:+ HTTPException: If read fails+ """+ pass++ @abstractmethod+ def write(self, path: Path, content: str) -> None:+ """Write content to storage.++ Args:+ path: Path to write to+ content: Content to write++ Raises:+ HTTPException: If write fails+ """+ pass++ @abstractmethod+ def exists(self, path: Path) -> bool:+ """Check if path exists in storage.++ Args:+ path: Path to check++ Returns:+ True if path exists, False otherwise+ """+ pass++ @abstractmethod+ def rename(self, old_path: Path, new_path: Path) -> None:+ """Rename/move a file in storage.++ Args:+ old_path: Source path+ new_path: Destination path++ Raises:+ HTTPException: If rename fails+ """+ pass++ @abstractmethod+ def is_same_path(self, path1: Path, path2: Path) -> bool:+ """Check if two paths refer to the same location.++ Args:+ path1: First path+ path2: Second path++ Returns:+ True if paths refer to same location+ """+ pass++ @abstractmethod+ def get_absolute_path(self, path: Path) -> Path:+ """Get absolute path.++ Args:+ path: Path to make absolute++ Returns:+ Absolute path+ """+ pass++ @abstractmethod+ def read_related_file(+ self, base_path: Path, relative_path: str+ ) -> Optional[str]:+ """Read a file relative to a base path.++ Used for reading CSS files, layout configs, etc.++ Args:+ base_path: Base path (typically the notebook path)+ relative_path: Relative path from base++ Returns:+ File contents or None if not found+ """+ pass+++class FilesystemStorage(StorageInterface):+ """Filesystem-based storage implementation."""++ def read(self, path: Path) -> str:+ """Read file from filesystem."""+ path = _ensure_path(path)+ try:+ return path.read_text(encoding="utf-8")+ except Exception as err:+ raise HTTPException(+ status_code=HTTPStatus.SERVER_ERROR,+ detail=f"Failed to read file {path}",+ ) from err++ def write(self, path: Path, content: str) -> None:+ """Write file to filesystem."""+ # Ensure path is a Path object+ path = _ensure_path(path)+ self.ensure_parent_dirs(path)+ try:+ path.write_text(content, encoding="utf-8")+ except Exception as err:+ raise HTTPException(+ status_code=HTTPStatus.SERVER_ERROR,+ detail=f"Failed to save file {path}",+ ) from err++ def exists(self, path: Path) -> bool:+ """Check if path exists on filesystem."""+ # Ensure path is a Path object+ path = _ensure_path(path)+ return path.exists()++ def rename(self, old_path: Path, new_path: Path) -> None:+ """Rename file on filesystem."""+ # Ensure paths are Path objects+ old_path = _ensure_path(old_path)+ new_path = _ensure_path(new_path)+ self.ensure_parent_dirs(new_path)+ try:+ old_path.rename(new_path)+ except Exception as err:+ raise HTTPException(+ status_code=HTTPStatus.SERVER_ERROR,+ detail=f"Failed to rename from {old_path} to {new_path}",+ ) from err++ def ensure_parent_dirs(self, path: Path) -> None:+ """Create parent directories if they don't exist."""+ path = _ensure_path(path)+ try:+ path.parent.mkdir(parents=True, exist_ok=True)+ except OSError as e:+ LOGGER.warning(+ f"Failed to create parent directories for {path}: {e}"+ )++ def is_same_path(self, path1: Path, path2: Path) -> bool:+ """Check if two paths refer to the same file."""+ path1 = _ensure_path(path1)+ path2 = _ensure_path(path2)+ try:+ return path1.resolve() == path2.resolve()+ except OSError as e:+ LOGGER.debug(f"Could not resolve paths {path1} and {path2}: {e}")+ try:+ return path1.absolute() == path2.absolute()+ except Exception as e2:+ LOGGER.debug(+ f"Could not get absolute paths for {path1} and {path2}: {e2}"+ )+ return str(path1) == str(path2)++ def get_absolute_path(self, path: Path) -> Path:+ """Get absolute path, resolving symlinks."""+ path = _ensure_path(path)+ try:+ return path.resolve()+ except OSError as e:+ LOGGER.warning(f"Could not resolve path {path}: {e}")+ return path.absolute()++ def read_related_file(+ self, base_path: Path, relative_path: str+ ) -> Optional[str]:+ """Read a file relative to the base path.++ Args:+ base_path: Base notebook path+ relative_path: Relative path to the related file++ Returns:+ File contents or None if not found+ """+ filepath = Path(relative_path)++ # If not an absolute path, make it relative to base_path's directory+ if not filepath.is_absolute():+ filepath = base_path.parent / filepath++ if not filepath.exists():+ LOGGER.error("Related file %s does not exist", filepath)+ return None++ try:+ return filepath.read_text(encoding="utf-8")+ except OSError as e:+ LOGGER.warning(+ "Failed to open related file %s for reading: %s",+ filepath,+ str(e),+ )+ return None+++def _ensure_path(path: str | Path) -> Path:+ """Ensure path is a Path object."""+ if not isinstance(path, Path):+ path = Path(path)+ return pathdiff --git a/marimo/_server/sessions.py b/marimo/_server/sessions.pyindex 9b9183d2a8..046e8f4de7 100644--- a/marimo/_server/sessions.py+++ b/marimo/_server/sessions.py@@ -60,11 +60,11 @@ from marimo._runtime.requests import (SyncGraphRequest,)from marimo._server.exceptions import InvalidSessionException-from marimo._server.file_manager import AppFileManagerfrom marimo._server.file_router import AppFileRouter, MarimoFileKeyfrom marimo._server.lsp import LspServerfrom marimo._server.model import ConnectionState, SessionConsumer, SessionModefrom marimo._server.models.models import InstantiateRequest+from marimo._server.notebook import AppFileManagerfrom marimo._server.recents import RecentFilesManagerfrom marimo._server.session.serialize import (SessionCacheKey,diff --git a/marimo/_server/templates/templates.py b/marimo/_server/templates/templates.pyindex 4259ad0fee..1bf4b4e281 100644--- a/marimo/_server/templates/templates.py+++ b/marimo/_server/templates/templates.py@@ -15,8 +15,8 @@ from marimo._output.utils import uri_encode_componentfrom marimo._schemas.notebook import NotebookV1from marimo._schemas.session import NotebookSessionV1from marimo._server.api.utils import parse_title-from marimo._server.file_manager import read_css_file, read_html_head_filefrom marimo._server.model import SessionMode+from marimo._server.notebook import read_css_file, read_html_head_filefrom marimo._server.tokens import SkewProtectionTokenfrom marimo._utils.versions import is_editablefrom marimo._version import __version__diff --git a/tests/_pyodide/test_pyodide_session.py b/tests/_pyodide/test_pyodide_session.pyindex 46789e4f88..5a8f6b06ff 100644--- a/tests/_pyodide/test_pyodide_session.py+++ b/tests/_pyodide/test_pyodide_session.py@@ -30,8 +30,8 @@ from marimo._runtime.requests import (SetUIElementValueRequest,StopRequest,)-from marimo._server.file_manager import AppFileManagerfrom marimo._server.model import SessionMode+from marimo._server.notebook import AppFileManagerfrom marimo._types.ids import CellId_t, UIElementIdif TYPE_CHECKING:diff --git a/tests/_server/export/test_exporter.py b/tests/_server/export/test_exporter.pyindex ef1b40daa6..86787186e3 100644--- a/tests/_server/export/test_exporter.py+++ b/tests/_server/export/test_exporter.py@@ -20,8 +20,8 @@ from marimo._server.export import (run_app_until_completion,)from marimo._server.export.exporter import Exporter-from marimo._server.file_manager import AppFileManagerfrom marimo._server.models.export import ExportAsHTMLRequest+from marimo._server.notebook import AppFileManagerfrom marimo._server.session.session_view import SessionViewfrom marimo._utils.marimo_path import MarimoPathfrom tests.mocks import snapshotterdiff --git a/tests/_server/notebook/test_serializer.py b/tests/_server/notebook/test_serializer.pynew file mode 100644index 0000000000..23b340d50c--- /dev/null+++ b/tests/_server/notebook/test_serializer.py@@ -0,0 +1,206 @@+# Copyright 2024 Marimo. All rights reserved.+"""Tests for serializer.py"""++from pathlib import Path++import pytest++from marimo._schemas.serialization import (+ AppInstantiation,+ CellDef,+ NotebookSerializationV1,+)+from marimo._server.notebook.serializer import (+ DEFAULT_FORMAT_HANDLERS,+ MarkdownNotebookSerializer,+ PythonNotebookSerializer,+ get_format_handler,+)+++class TestPythonNotebookSerializer:+ def test_serialize_basic(self, tmp_path: Path) -> None:+ serializer = PythonNotebookSerializer()+ notebook = NotebookSerializationV1(+ app=AppInstantiation(),+ cells=[+ CellDef(+ name="cell1",+ code="x = 1",+ )+ ],+ )+ path = tmp_path / "notebook.py"++ result = serializer.serialize(notebook, path)++ assert "import marimo" in result+ assert "x = 1" in result++ def test_serialize_with_existing_header(self, tmp_path: Path) -> None:+ """Test that headers are preserved from existing file."""+ serializer = PythonNotebookSerializer()++ # Create existing file with header+ path = tmp_path / "notebook.py"+ path.write_text(+ "# Header comment\n# Another line\nimport marimo\n",+ encoding="utf-8",+ )++ notebook = NotebookSerializationV1(+ app=AppInstantiation(),+ cells=[+ CellDef(+ name="cell1",+ code="x = 1",+ )+ ],+ )++ result = serializer.serialize(notebook, path)++ assert "# Header comment" in result+ assert "import marimo" in result++ def test_extract_header_with_comments(self, tmp_path: Path) -> None:+ serializer = PythonNotebookSerializer()+ test_file = tmp_path / "notebook.py"+ test_file.write_text(+ "# Header comment\n# Another line\nimport marimo\n",+ encoding="utf-8",+ )++ header = serializer.extract_header(test_file)++ assert header is not None+ assert "Header comment" in header++ def test_extract_header_no_comments(self, tmp_path: Path) -> None:+ serializer = PythonNotebookSerializer()+ test_file = tmp_path / "notebook.py"+ test_file.write_text("import marimo\n", encoding="utf-8")++ header = serializer.extract_header(test_file)++ # No header comments should return None or empty+ assert header is None or header == ""++ def test_serialize_format_conversion(self, tmp_path: Path) -> None:+ """Test serializing when converting from one format to another."""+ serializer = PythonNotebookSerializer()++ # Create a markdown file with header+ md_file = tmp_path / "notebook.md"+ md_file.write_text(+ "---\nmarimo-version: 0.1.0\n---\n# Content\n",+ encoding="utf-8",+ )++ notebook = NotebookSerializationV1(+ app=AppInstantiation(),+ cells=[+ CellDef(+ name="cell1",+ code="x = 1",+ )+ ],+ )++ py_file = tmp_path / "notebook.py"+ result = serializer.serialize(notebook, py_file, previous_path=md_file)++ # Should extract header from markdown file+ assert "import marimo" in result+++class TestMarkdownNotebookSerializer:+ def test_serialize_basic(self, tmp_path: Path) -> None:+ serializer = MarkdownNotebookSerializer()+ notebook = NotebookSerializationV1(+ app=AppInstantiation(),+ cells=[+ CellDef(+ name="cell1",+ code="print('hello')",+ )+ ],+ )+ path = tmp_path / "notebook.md"++ result = serializer.serialize(notebook, path)++ assert "```python" in result+ assert "print('hello')" in result++ def test_extract_header_with_frontmatter(self, tmp_path: Path) -> None:+ serializer = MarkdownNotebookSerializer()+ test_file = tmp_path / "notebook.md"+ markdown_content = """---+marimo-version: 0.1.0+header: |+ # Custom header+---++# Title+"""+ test_file.write_text(markdown_content, encoding="utf-8")++ header = serializer.extract_header(test_file)++ assert header is not None+ assert "Custom header" in header++ def test_extract_header_no_frontmatter(self, tmp_path: Path) -> None:+ serializer = MarkdownNotebookSerializer()+ test_file = tmp_path / "notebook.md"+ test_file.write_text("# Just markdown\n", encoding="utf-8")++ header = serializer.extract_header(test_file)++ assert header is None or header == ""+++class TestGetFormatHandler:+ def test_get_python_handler(self, tmp_path: Path) -> None:+ path = tmp_path / "notebook.py"++ handler = get_format_handler(path)++ assert isinstance(handler, PythonNotebookSerializer)++ def test_get_markdown_handler(self, tmp_path: Path) -> None:+ path = tmp_path / "notebook.md"++ handler = get_format_handler(path)++ assert isinstance(handler, MarkdownNotebookSerializer)++ def test_get_qmd_handler(self, tmp_path: Path) -> None:+ path = tmp_path / "notebook.qmd"++ handler = get_format_handler(path)++ assert isinstance(handler, MarkdownNotebookSerializer)++ def test_get_handler_with_string_path(self, tmp_path: Path) -> None:+ path_str = str(tmp_path / "notebook.py")++ handler = get_format_handler(Path(path_str))++ assert isinstance(handler, PythonNotebookSerializer)++ def test_unsupported_extension(self, tmp_path: Path) -> None:+ path = tmp_path / "notebook.txt"++ with pytest.raises(ValueError) as exc_info:+ get_format_handler(path)++ assert "No format handler found" in str(exc_info.value)+ assert ".txt" in str(exc_info.value)++ def test_default_handlers_registered(self) -> None:+ assert ".py" in DEFAULT_FORMAT_HANDLERS+ assert ".md" in DEFAULT_FORMAT_HANDLERS+ assert ".qmd" in DEFAULT_FORMAT_HANDLERS+ assert len(DEFAULT_FORMAT_HANDLERS) == 3diff --git a/tests/_server/notebook/test_storage.py b/tests/_server/notebook/test_storage.pynew file mode 100644index 0000000000..5bb2f3f42a--- /dev/null+++ b/tests/_server/notebook/test_storage.py@@ -0,0 +1,191 @@+# Copyright 2024 Marimo. All rights reserved.+"""Tests for storage.py"""++from pathlib import Path++import pytest++from marimo._server.api.status import HTTPException+from marimo._server.notebook.storage import FilesystemStorage+++class TestFilesystemStorage:+ def test_read_success(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ test_file = tmp_path / "test.txt"+ test_file.write_text("hello world", encoding="utf-8")++ content = storage.read(test_file)+ assert content == "hello world"++ def test_read_nonexistent_file(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ test_file = tmp_path / "nonexistent.txt"++ with pytest.raises(HTTPException) as exc_info:+ storage.read(test_file)+ assert exc_info.value.status_code == 500++ def test_write_success(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ test_file = tmp_path / "output.txt"++ storage.write(test_file, "test content")++ assert test_file.exists()+ assert test_file.read_text(encoding="utf-8") == "test content"++ def test_write_creates_parent_dirs(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ test_file = tmp_path / "subdir" / "nested" / "file.txt"++ storage.write(test_file, "nested content")++ assert test_file.exists()+ assert test_file.read_text(encoding="utf-8") == "nested content"++ def test_write_with_string_path(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ test_file = tmp_path / "string_path.txt"++ storage.write(str(test_file), "content via string")++ assert test_file.exists()+ assert test_file.read_text(encoding="utf-8") == "content via string"++ def test_exists_true(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ test_file = tmp_path / "exists.txt"+ test_file.write_text("exists", encoding="utf-8")++ assert storage.exists(test_file) is True++ def test_exists_false(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ test_file = tmp_path / "does_not_exist.txt"++ assert storage.exists(test_file) is False++ def test_rename_success(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ old_file = tmp_path / "old.txt"+ new_file = tmp_path / "new.txt"+ old_file.write_text("content", encoding="utf-8")++ storage.rename(old_file, new_file)++ assert not old_file.exists()+ assert new_file.exists()+ assert new_file.read_text(encoding="utf-8") == "content"++ def test_rename_creates_parent_dirs(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ old_file = tmp_path / "old.txt"+ new_file = tmp_path / "nested" / "dir" / "new.txt"+ old_file.write_text("content", encoding="utf-8")++ storage.rename(old_file, new_file)++ assert not old_file.exists()+ assert new_file.exists()++ def test_rename_nonexistent_file(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ old_file = tmp_path / "nonexistent.txt"+ new_file = tmp_path / "new.txt"++ with pytest.raises(HTTPException) as exc_info:+ storage.rename(old_file, new_file)+ assert exc_info.value.status_code == 500++ def test_is_same_path_identical(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ test_file = tmp_path / "file.txt"+ test_file.write_text("content", encoding="utf-8")++ assert storage.is_same_path(test_file, test_file) is True++ def test_is_same_path_different(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ file1 = tmp_path / "file1.txt"+ file2 = tmp_path / "file2.txt"+ file1.write_text("content", encoding="utf-8")+ file2.write_text("content", encoding="utf-8")++ assert storage.is_same_path(file1, file2) is False++ def test_is_same_path_relative_vs_absolute(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ test_file = tmp_path / "file.txt"+ test_file.write_text("content", encoding="utf-8")++ # Compare absolute and relative representations+ assert storage.is_same_path(test_file, test_file.resolve()) is True++ def test_get_absolute_path(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ test_file = tmp_path / "file.txt"++ abs_path = storage.get_absolute_path(test_file)++ assert abs_path.is_absolute()+ assert abs_path.name == "file.txt"++ def test_read_related_file_absolute(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ base_file = tmp_path / "notebook.py"+ related_file = tmp_path / "style.css"+ related_file.write_text("/* CSS */", encoding="utf-8")++ content = storage.read_related_file(base_file, str(related_file))++ assert content == "/* CSS */"++ def test_read_related_file_relative(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ base_file = tmp_path / "notebook.py"+ related_file = tmp_path / "style.css"+ related_file.write_text("/* CSS */", encoding="utf-8")++ content = storage.read_related_file(base_file, "style.css")++ assert content == "/* CSS */"++ def test_read_related_file_nonexistent(self, tmp_path: Path) -> None:+ storage = FilesystemStorage()+ base_file = tmp_path / "notebook.py"++ content = storage.read_related_file(base_file, "nonexistent.css")++ assert content is None++ def test_read_related_file_nested(self, tmp_path: Path) -> None:+ """Test reading related file in subdirectory."""+ storage = FilesystemStorage()+ base_file = tmp_path / "notebook.py"+ subdir = tmp_path / "assets"+ subdir.mkdir()+ related_file = subdir / "style.css"+ related_file.write_text("/* nested CSS */", encoding="utf-8")++ content = storage.read_related_file(base_file, "assets/style.css")++ assert content == "/* nested CSS */"++ def test_ensure_parent_dirs_with_string(self, tmp_path: Path) -> None:+ """Test ensure_parent_dirs with string path."""+ storage = FilesystemStorage()+ test_file = str(tmp_path / "nested" / "dir" / "file.txt")++ storage.ensure_parent_dirs(Path(test_file))++ assert Path(test_file).parent.exists()++ def test_is_same_path_with_strings(self, tmp_path: Path) -> None:+ """Test is_same_path handles string paths correctly."""+ storage = FilesystemStorage()+ test_file = tmp_path / "file.txt"+ test_file.write_text("content", encoding="utf-8")++ # Pass as strings to test _ensure_path+ assert storage.is_same_path(Path(str(test_file)), test_file) is Truediff --git a/tests/_server/test_file_manager.py b/tests/_server/test_file_manager.pyindex d659446629..e35f13024c 100644--- a/tests/_server/test_file_manager.py+++ b/tests/_server/test_file_manager.py@@ -7,10 +7,11 @@ from typing import TYPE_CHECKINGimport pytestfrom marimo import __version__+from marimo._ast.app import App, InternalAppfrom marimo._ast.cell import CellConfigfrom marimo._server.api.status import HTTPException, HTTPStatus-from marimo._server.file_manager import AppFileManagerfrom marimo._server.models.models import SaveNotebookRequest+from marimo._server.notebook import AppFileManagerfrom marimo._types.ids import CellId_tfrom marimo._utils.cell_matching import similarity_score@@ -76,7 +77,7 @@ def test_rename_to_existing_filename(app_file_manager: AppFileManager) -> None:def test_successful_rename(app_file_manager: AppFileManager) -> None:- existing_filename = os.path.abspath(app_file_manager.filename or "")+ existing_filename = os.path.abspath(str(app_file_manager.filename or ""))new_filename = os.path.join(os.path.dirname(existing_filename), "new_file.py")@@ -84,7 +85,7 @@ def test_successful_rename(app_file_manager: AppFileManager) -> None:os.remove(new_filename)try:app_file_manager.rename(new_filename)- assert app_file_manager.filename == new_filename+ assert str(app_file_manager.filename) == new_filenamefinally:os.remove(new_filename)@@ -129,16 +130,16 @@ def test_rename_create_new_directory_file(def test_rename_different_filetype(app_file_manager: AppFileManager) -> None:initial_filename = app_file_manager.filenameassert initial_filename- assert initial_filename.endswith(".py")- with open(initial_filename) as f:+ assert str(initial_filename).endswith(".py")+ with open(str(initial_filename)) as f:contents = f.read()assert "app = marimo.App()" in contentsassert "marimo-version" not in contents- app_file_manager.rename(initial_filename[:-3] + ".md")+ app_file_manager.rename(str(initial_filename)[:-3] + ".md")next_filename = app_file_manager.filenameassert next_filename- assert next_filename.endswith(".md")- with open(next_filename) as f:+ assert str(next_filename).endswith(".md")+ with open(str(next_filename)) as f:contents = f.read()assert "marimo-version" in contentsassert "app = marimo.App()" not in contents@@ -147,17 +148,17 @@ def test_rename_different_filetype(app_file_manager: AppFileManager) -> None:def test_rename_to_qmd(app_file_manager: AppFileManager) -> None:initial_filename = app_file_manager.filenameassert initial_filename- assert initial_filename.endswith(".py")- with open(initial_filename) as f:+ assert str(initial_filename).endswith(".py")+ with open(str(initial_filename)) as f:contents = f.read()assert "app = marimo.App()" in contentsassert "marimo-team/marimo" not in contentsassert "marimo-version" not in contents- app_file_manager.rename(initial_filename[:-3] + ".qmd")+ app_file_manager.rename(str(initial_filename)[:-3] + ".qmd")next_filename = app_file_manager.filenameassert next_filename- assert next_filename.endswith(".qmd")- with open(next_filename) as f:+ assert str(next_filename).endswith(".qmd")+ with open(str(next_filename)) as f:contents = f.read()assert "marimo-version" in contentsassert "filters:" in contents@@ -485,7 +486,7 @@ def test_rename_with_special_chars(# Try to rename to path with special charactersnew_path = tmp_path / "test & space.py"app_file_manager.rename(str(new_path))- assert app_file_manager.filename == str(new_path)+ assert str(app_file_manager.filename) == str(new_path)assert new_path.exists()@@ -853,3 +854,205 @@ if __name__ == "__main__":# Now the file should not match the last saveassert manager.file_content_matches_last_save() is False+++def test_file_content_matches_last_save_no_filename() -> None:+ """Test file_content_matches_last_save returns False when filename is None."""+ # Create an unnamed manager (in-memory notebook)+ manager = AppFileManager.from_app(InternalApp(App()))++ # Should return False when there's no filename+ assert manager.file_content_matches_last_save() is False+++def test_file_content_matches_last_save_no_previous_save(+ tmp_path: Path,+) -> None:+ """Test file_content_matches_last_save returns False when no save has been made."""+ temp_file = tmp_path / "test_no_save.py"+ temp_file.write_text(+ """+import marimo+app = marimo.App()++def cell1():+ x = 1+ return x++if __name__ == "__main__":+ app.run()+"""+ )++ manager = AppFileManager(filename=str(temp_file))++ # Should return False when _last_saved_content is None+ assert manager._last_saved_content is None+ assert manager.file_content_matches_last_save() is False+++def test_file_content_matches_last_save_file_deleted(tmp_path: Path) -> None:+ """Test file_content_matches_last_save handles deleted files gracefully."""+ temp_file = tmp_path / "test_deleted.py"+ temp_file.write_text(+ """+import marimo+app = marimo.App()++def cell1():+ x = 1+ return x++if __name__ == "__main__":+ app.run()+"""+ )++ manager = AppFileManager(filename=str(temp_file))++ # Save the file+ manager.save(+ SaveNotebookRequest(+ cell_ids=[CellId_t("1")],+ filename=str(temp_file),+ codes=["x = 1"],+ names=["cell1"],+ configs=[CellConfig()],+ persist=True,+ )+ )++ # Verify it matches+ assert manager.file_content_matches_last_save() is True++ # Delete the file+ temp_file.unlink()++ # Should return False (and not crash) when file doesn't exist+ assert manager.file_content_matches_last_save() is False+++def test_file_content_matches_last_save_whitespace_handling(+ tmp_path: Path,+) -> None:+ """Test that file_content_matches_last_save handles whitespace correctly."""+ temp_file = tmp_path / "test_whitespace.py"+ temp_file.write_text(+ """+import marimo+app = marimo.App()++def cell1():+ x = 1+ return x++if __name__ == "__main__":+ app.run()+"""+ )++ manager = AppFileManager(filename=str(temp_file))++ # Save the file+ manager.save(+ SaveNotebookRequest(+ cell_ids=[CellId_t("1")],+ filename=str(temp_file),+ codes=["x = 1"],+ names=["cell1"],+ configs=[CellConfig()],+ persist=True,+ )+ )++ # Should match (content is the same)+ assert manager.file_content_matches_last_save() is True++ # Read the saved content and add trailing whitespace+ content = temp_file.read_text()+ temp_file.write_text(content + " \n\n")++ # Should still match because we strip whitespace when comparing+ assert manager.file_content_matches_last_save() is True++ # Now add a meaningful change+ temp_file.write_text(content + "\n# comment\n")++ # Should NOT match because we added actual content+ assert manager.file_content_matches_last_save() is False+++def test_file_content_matches_last_save_multiple_saves(tmp_path: Path) -> None:+ """Test that file_content_matches_last_save tracks the most recent save."""+ temp_file = tmp_path / "test_multiple_saves.py"+ temp_file.write_text(+ """+import marimo+app = marimo.App()++def cell1():+ x = 1+ return x++if __name__ == "__main__":+ app.run()+"""+ )++ manager = AppFileManager(filename=str(temp_file))++ # First save+ manager.save(+ SaveNotebookRequest(+ cell_ids=[CellId_t("1")],+ filename=str(temp_file),+ codes=["x = 1"],+ names=["cell1"],+ configs=[CellConfig()],+ persist=True,+ )+ )++ assert manager.file_content_matches_last_save() is True++ # Second save with different content+ manager.save(+ SaveNotebookRequest(+ cell_ids=[CellId_t("1")],+ filename=str(temp_file),+ codes=["x = 2"],+ names=["cell1"],+ configs=[CellConfig()],+ persist=True,+ )+ )++ # Should still match after second save+ assert manager.file_content_matches_last_save() is True++ # Externally modify back to first save's content+ temp_file.write_text(+ """+import marimo++__generated_with = "0.0.0"+app = marimo.App()+++def cell1():+ x = 1+ return (x,)+++if __name__ == "__main__":+ app.run()+"""+ )++ # Should NOT match (content is from first save, not most recent)+ assert manager.file_content_matches_last_save() is Falsediff --git a/tests/_server/test_file_manager_filename.py b/tests/_server/test_file_manager_filename.pyindex 8b483583ec..9cba399b60 100644--- a/tests/_server/test_file_manager_filename.py+++ b/tests/_server/test_file_manager_filename.py@@ -6,7 +6,7 @@ from pathlib import Pathimport pytestfrom marimo._server.api.status import HTTPException-from marimo._server.file_manager import AppFileManager+from marimo._server.notebook import AppFileManagerfrom tests.mocks import EDGE_CASE_FILENAMESdiff --git a/tests/_server/test_session_manager.py b/tests/_server/test_session_manager.pyindex e1ad17f7a6..4b17963e17 100644--- a/tests/_server/test_session_manager.py+++ b/tests/_server/test_session_manager.py@@ -7,9 +7,9 @@ from unittest.mock import MagicMock, Mockimport pytestfrom marimo._config.manager import get_default_config_manager-from marimo._server.file_manager import AppFileManagerfrom marimo._server.file_router import AppFileRouterfrom marimo._server.model import ConnectionState, SessionConsumer, SessionMode+from marimo._server.notebook import AppFileManagerfrom marimo._server.sessions import (KernelManager,LspServer,diff --git a/tests/_server/test_sessions.py b/tests/_server/test_sessions.pyindex 76e9141f2b..27867a4097 100644--- a/tests/_server/test_sessions.py+++ b/tests/_server/test_sessions.py@@ -30,9 +30,9 @@ from marimo._runtime.requests import (SetUIElementValueRequest,SyncGraphRequest,)-from marimo._server.file_manager import AppFileManagerfrom marimo._server.file_router import AppFileRouterfrom marimo._server.model import ConnectionState, SessionMode+from marimo._server.notebook import AppFileManagerfrom marimo._server.session.session_view import SessionViewfrom marimo._server.sessions import (KernelManager,@@ -1095,3 +1095,131 @@ def test_session_with_script_config_overrides(# Cleanupsession.close()+++@save_and_restore_main+async def test_file_change_handler_skips_own_writes(tmp_path: Path) -> None:+ """Test that file change handler skips reloading when it detects its own writes."""+ from marimo._ast.cell import CellConfig+ from marimo._server.models.models import SaveNotebookRequest+ from marimo._types.ids import CellId_t++ # Create a temporary file with initial content+ temp_file = tmp_path / "test_own_writes.py"+ temp_file.write_text(+ dedent(+ """+ import marimo+ app = marimo.App()++ @app.cell+ def cell1():+ x = 1+ return x++ if __name__ == "__main__":+ app.run()+ """+ )+ )++ # Set up session with minimal dependencies+ session_consumer = MagicMock()+ session_consumer.connection_state.return_value = ConnectionState.OPEN+ app_file_manager = AppFileManager(filename=str(temp_file))++ session = Session.create(+ initialization_id="test_id",+ session_consumer=session_consumer,+ mode=SessionMode.EDIT,+ app_metadata=app_metadata,+ app_file_manager=app_file_manager,+ config_manager=get_default_config_manager(current_path=None),+ virtual_files_supported=True,+ redirect_console_to_browser=False,+ ttl_seconds=None,+ )++ # Save the file to track the last saved content+ session.app_file_manager.save(+ SaveNotebookRequest(+ cell_ids=[CellId_t("1")],+ filename=str(temp_file),+ codes=["x = 1"],+ names=["cell1"],+ configs=[CellConfig()],+ persist=True,+ )+ )++ # Create a minimal session manager for the file change handler+ file_router = AppFileRouter.from_filename(MarimoPath(str(temp_file)))+ session_manager = SessionManager(+ file_router=file_router,+ mode=SessionMode.EDIT,+ development_mode=False,+ quiet=False,+ include_code=True,+ lsp_server=MagicMock(),+ config_manager=get_default_config_manager(current_path=None),+ watch=False,+ cli_args={},+ argv=None,+ auth_token=None,+ redirect_console_to_browser=False,+ ttl_seconds=None,+ )++ # Create file change handler+ file_change_handler = session_manager.file_change_handler++ # Mock the reload method to verify it's not called+ original_reload = session.app_file_manager.reload+ reload_called = False++ def mock_reload():+ nonlocal reload_called+ reload_called = True+ return original_reload()++ session.app_file_manager.reload = mock_reload++ # Call _handle_file_change - should skip reload because content matches+ file_change_handler._handle_file_change(str(temp_file), session)++ # Verify reload was NOT called (early return triggered)+ assert not reload_called, (+ "reload() should not have been called for own writes"+ )++ # Now externally modify the file+ temp_file.write_text(+ dedent(+ """+ import marimo+ app = marimo.App()++ @app.cell+ def cell1():+ x = 2 # Changed by external editor+ return x++ if __name__ == "__main__":+ app.run()+ """+ )+ )++ # Reset the flag+ reload_called = False++ # Call _handle_file_change again - should now trigger reload+ file_change_handler._handle_file_change(str(temp_file), session)++ # Verify reload WAS called (content changed externally)+ assert reload_called, (+ "reload() should have been called for external changes"+ )++ # Cleanup+ session.close()