Description
I import from marimo notebooks pretty often. Each time it looks something like this:
notebook_a:
@marimo.cell()
def cell_defining_x():
x = 42
return (x,)
notebook_b:
from notebook_a import cell_defining_x
_, cell_x_output = cell_defining_x.run()
x = cell_x_output['x']
It works but it's ugly. I'm repeating my variable name quiet often.
Given that marimo already parses the cells and global variables need to be uniquely defined, there should be a cleaner way right?
Suggested solution
marimo.App could get a method like get_variable which would need to find the variable definition, run the cell and return the variable.
This would give us the shorter syntax of:
x = notebook.app.get_variable('x')
Are you willing to submit a PR?
Alternatives
My ideal syntax would be:
notebook_a:
@marimo.cell()
def _():
x = 42
return x
notebook_b:
But this is not possible in Python.
One proposal that could get pretty close is with __getatr__, see example:
notebook_b.py:
import notebook_a
x = notebook_a.x
notebook_a.py:
import marimo
app = marimo.App(__name__)
@app.cell
def _():
x = 42
return x
marimo.py:
import sys
class App:
def __init__(self, notebook_name):
self.notebook_attributes = {}
sys.modules[notebook_name].__getattr__ = self.get_notebook_attribute
def get_notebook_attribute(self, name):
if name not in self.notebook_attributes:
raise AttributeError(f"notebook has no attribute {name}")
cell = self.notebook_attributes[name]
return cell() // cell.run() including depending cells
def cell(self, func):
# Some magic parsing Marimo already does
var = 'x'
self.notebook_attributes[var] = func
return func
Downsides of this solution:
- The marimo notebook format needs to be updated to include passing
__name__.
- Doesn't feel explicit that
notebook_a.x would run a long chain of events, nor that it's even available.
Additional context
No response
Description
I import from marimo notebooks pretty often. Each time it looks something like this:
notebook_a:notebook_b:It works but it's ugly. I'm repeating my variable name quiet often.
Given that marimo already parses the cells and global variables need to be uniquely defined, there should be a cleaner way right?
Suggested solution
marimo.App could get a method like
get_variablewhich would need to find the variable definition, run the cell and return the variable.This would give us the shorter syntax of:
Are you willing to submit a PR?
Alternatives
My ideal syntax would be:
notebook_a:notebook_b:But this is not possible in Python.
One proposal that could get pretty close is with
__getatr__, see example:notebook_b.py:notebook_a.py:marimo.py:Downsides of this solution:
__name__.notebook_a.xwould run a long chain of events, nor that it's even available.Additional context
No response