marimo icon indicating copy to clipboard operation
marimo copied to clipboard

Add cached outputs to run mode

Open jpopesculian opened this issue 10 months ago • 6 comments

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

  1. Add an extra setting cache_outputs. I find the control of cache to be confusing based on auto_instantiate
  2. Set the caching extension to be CachingExtension(enabled=cache_outputs, mode="write" if mode == SessionMode.EDIT else "read")
  3. 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

jpopesculian avatar Jan 15 '26 15:01 jpopesculian

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.

jameskermode avatar Mar 30 '26 16:03 jameskermode

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:

  1. A marimo export cache notebook.py (or marimo run --precompute) CLI command that executes the notebook headlessly and writes persistent_cache outputs to disk
  2. marimo run then hits those cache files naturally on startup no changes to SessionMode or CachingExtension behavior needed
  3. 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.

Sushit-prog avatar Apr 07 '26 12:04 Sushit-prog

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

mscolnick avatar Apr 07 '26 16:04 mscolnick

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.

Sushit-prog avatar Apr 07 '26 19:04 Sushit-prog

@jpopesculian this particular issue is closed thought, right? or is there more to followup?

mscolnick avatar Apr 09 '26 03:04 mscolnick

This is closed. This can be turned on via configuration:

[tool.marimo.runtime]
serve_cached_sessions_in_apps = true

mscolnick avatar Jun 10 '26 15:06 mscolnick