refactor: add load_notebook helper, simplify AppFileRouter#9438
Merged
Conversation
Extract the ``from_filename → get_unique_file_key → get_file_manager`` boilerplate into ``load_notebook(path)`` / ``new_notebook()`` helpers and migrate single-file consumers (islands, server export, session cache, dev CLI) off the router. Hoist optional router capabilities (``register_allowed_file``, ``register_temp_dir``, ``is_file_in_allowed_temp_dir``, ``mark_stale``, ``toggle_markdown``) to the abstract base as no-op defaults so callers in ``SessionManager`` and the home endpoint stop branching on ``isinstance``.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Contributor
There was a problem hiding this comment.
No issues found across 11 files
Architecture diagram
sequenceDiagram
participant CLI as "CLI / Islands / Export"
participant Loader as "Notebook Loader (NEW)"
participant API as "API Endpoints (Home)"
participant SM as "SessionManager"
participant Router as "AppFileRouter (Base)"
participant FM as "AppFileManager"
Note over CLI, FM: Single File Access Flow (Simplified)
CLI->>Loader: NEW: load_notebook(path)
Loader->>Loader: Resolve absolute path
opt Invalid extension (.txt, etc)
Loader-->>CLI: Raise ValueError
end
Loader->>FM: Instantiate AppFileManager
FM-->>Loader: file_manager
Loader-->>CLI: return file_manager
Note right of CLI: CLI/Islands no longer use Router\nfor single files.
Note over API, Router: Workspace / Directory Flow (Refactored)
API->>SM: Get File Router
SM-->>API: file_router
API->>Router: CHANGED: mark_stale()
Note right of Router: Hoisted to base class.\nNo-op if not supported.
API->>Router: CHANGED: toggle_markdown(bool)
Router-->>API: return self OR new Router
Note right of Router: No more 'isinstance' branching\nin caller.
API->>Router: CHANGED: register_temp_dir(path)
Note right of Router: Allows tutorial/temp files\nvia abstract interface.
Note over SM, FM: Session Lifecycle Flow
SM->>Router: CHANGED: register_allowed_file(path)
Note right of Router: Logic shifted from SessionManager\nto Router implementations.
SM->>Router: get_file_manager(key)
alt Router is NewFileAppFileRouter
Router->>FM: Instantiate (unbacked)
else Router is Directory-backed
Router->>FM: Instantiate (path-backed)
end
Router-->>SM: return file_manager
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a small notebook-loading API in marimo._session.notebook to replace repeated “router → unique key → file manager” boilerplate for single-file workflows, and simplifies router usage by making several router capabilities available as no-op defaults on the base class.
Changes:
- Added
load_notebook(path)/new_notebook()helpers that validate marimo notebook extensions and load notebooks viaAppFileManager. - Migrated single-file consumers (exports, islands, session snapshot/export CLI, dev preview) to use
load_notebookinstead ofAppFileRouter.from_filename(...).get_unique_file_key().... - Hoisted optional router capabilities (e.g.,
mark_stale,toggle_markdown, temp dir registration) toAppFileRouteras no-op defaults, simplifying call sites that previously branched on router type.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| tests/_session/notebook/test_loader.py | Adds coverage for load_notebook/new_notebook behavior (path types, relative resolution, extension validation, basic load). |
| marimo/_session/notebook/loader.py | Implements load_notebook and new_notebook helpers backed by MarimoPath + AppFileManager. |
| marimo/_session/notebook/init.py | Exposes the new loader helpers from the notebook package. |
| marimo/_server/session_manager.py | Removes router-type branching by calling register_allowed_file unconditionally in edit mode (now safe via base no-op). |
| marimo/_server/file_router.py | Makes get_file_manager/resolve_file_path abstract and adds no-op default implementations for optional router capabilities. |
| marimo/_server/export/_session_cache.py | Switches notebook hashing to load_notebook. |
| marimo/_server/export/init.py | Switches multiple export helpers to load_notebook for file manager creation. |
| marimo/_server/api/endpoints/home.py | Simplifies watched-folder refresh and markdown toggle logic using base no-op router methods. |
| marimo/_islands/_island_generator.py | Uses load_notebook for island generation from file. |
| marimo/_cli/export/session.py | Uses load_notebook for session snapshot exports (including subprocess script). |
| marimo/_cli/development/commands.py | Uses load_notebook for the dev preview command’s file manager creation. |
Contributor
|
🚀 Development release published. You may be able to view the changes at https://marimo.app?v=0.23.5-dev10 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Extract the
from_filename → get_unique_file_key → get_file_managerboilerplate intoload_notebook(path)/new_notebook()helpers and migrate single-file consumers (islands, server export, session cache, dev CLI) off the router.Hoist optional router capabilities (
register_allowed_file,register_temp_dir,is_file_in_allowed_temp_dir,mark_stale,toggle_markdown) to the abstract base as no-op defaults so callers inSessionManagerand the home endpoint stop branching onisinstance.