Add cached outputs to run mode
Description
For long running notebook cells, having the cached outputs through the CachingExtension is useful. For example, if it takes 10 seconds to load a datatable, having the datatable view available as a static element before it loads and becomes interactive is a good user experience. Although I think the changes in https://github.com/marimo-team/marimo/pull/7737 are correct (run mode should not cause side effects where possible), it does remove that cached view when loading. I would like to bring back a read-only version of that cache that just populates the initial session
Suggested solution
- Add an extra setting
cache_outputs. I find the control of cache to be confusing based onauto_instantiate - Set the caching extension to be
CachingExtension(enabled=cache_outputs, mode="write" if mode == SessionMode.EDIT else "read") - Optionally add some way to run notebooks and write to the cache with the marimo CLI so you could something like
marimo cache notebook.py && marimo run notebook.py
Are you willing to submit a PR?
- [x] Yes
Alternatives
No response
Additional context
No response
I've been exploring a related pattern with marimo-precompute: precomputing cell results offline and loading them instantly in both marimo run and WASM.
In the current implementation, with persistent_cache(name="my_calc"): writes cache files to public/__marimo_precompute__/ on first run. On subsequent marimo run invocations, the cache hits and the block is skipped, giving the "instant cached outputs" behaviour this issue requests.
For WASM deployment, I additionally bundle a manifest.json and use await prefetch_all() to load the cache into Pyodide's VFS, with hash-patching to handle cross-environment bytecode differences.
The marimo cache notebook.py && marimo run notebook.py workflow suggested here aligns well with this: the precompute step is essentially marimo export html --no-sandbox (which executes all cells and writes the cache) followed by marimo run.
Hey! I'd like to work on this.
@jameskermode, really interesting work with marimo-precompute. The
marimo export html --no-sandbox → marimo run workflow you describe is
essentially the marimo cache notebook.py && marimo run notebook.py pattern
the issue suggests, and the fact that it already works externally is strong
validation that this is the right approach.
I noticed PR #7895 attempted an in-runtime fix but was reverted in #8281. My read is that mutating cache behavior inside the session lifecycle is risky your approach sidesteps that entirely by precomputing before the session starts, which is much safer.
My proposed direction for a native implementation:
- A
marimo export cache notebook.py(ormarimo run --precompute) CLI command that executes the notebook headlessly and writes persistent_cache outputs to disk marimo runthen hits those cache files naturally on startup no changes to SessionMode or CachingExtension behavior needed- Off by default, opt-in, zero risk to existing users
@mscolnick , does this direction align better with what caused the revert of #7895? Would love your input before starting implementation.
@jameskermode would you be open to collaborating or at least sharing what edge cases you hit with the hash-patching and bytecode differences? That context would be invaluable.
tagging @peter-gy and @dmadisetti since they may be cooking up some things related to this. likely will start with some lower-level primitives (creating the cache hits, reading the cache hits). we can share out a formal doc once soon
Thanks for the heads up @mscolnick! Happy to wait for the formal doc before diving in would love to contribute once the lower-level primitives are defined. Will keep an eye on this thread.
@jpopesculian this particular issue is closed thought, right? or is there more to followup?
This is closed. This can be turned on via configuration:
[tool.marimo.runtime]
serve_cached_sessions_in_apps = true