marimo-team / marimo

A reactive notebook for Python — run reproducible experiments, execute as a script, deploy as an app, and version with git.
https://marimo.io
Apache License 2.0
7.95k stars 278 forks source link

Is it possible to share code between marimo js packages and widget packages #2908

Closed paddymul closed 1 day ago

paddymul commented 1 day ago

Description

My widget - Buckaroo depends on react and ag-grid. these are fairly hefty js packages. Anywidget calls sucrase on every widget injection and duplicates js code in the browser DOM. I would prefer to have a slim module.

Marimo includes react and ag-grid. https://github.com/marimo-team/marimo/blob/e8f84e480805732dae84f349cd83d7cb08321f56/frontend/package.json#L86-L87

Do you have any examples of widgets depending on these packages instead of having to pack up their entire js dependency chain for anywidget?

My core library is fairly small (30k of code+ 70k of non marimo shared dependencies), but it baloons to ~400k - 1.2m when everything is bundled depending on how you count. This gets worse with anywidget because it's injected each time a widget is displayed.

Suggested solution

IPYReact fixes this with modules. Anywidget isn't looking to adopt that approach in the near term. https://github.com/widgetti/ipyreact?tab=readme-ov-file#creating-the-es-module

Alternative

No response

Additional context

There have been issues and discussions with anywidget about this, but I can't currently find them.

mscolnick commented 1 day ago

Going to make this a discussion as i think that is a better forum for this.

It is not currently possible. This could increase perf, but also makes versioning a bit tricker since we may not be able to upgrade react, for example, to react 19 if plugins don't support this, so I am not sure we want to support this the way IPYReact does.

If possible, I would try to lazy-load your module. You can even code-split vendor deps (ag-grid and react) so those get cached. You will pay a first time download hit, but won't after that.