The file format
Part 8 of 10 in Learn marimo.
File Format
Section titled “File Format”marimo apps are stored as pure Python files.
These files are:
- 🤖 legible for both humans and machines
- ✏️ formattable using your tool of choice
- ➕ easily versioned with git, producing small diffs
- 🐍 usable as Python scripts, with UI elements taking their default values
- 🧩 modular, exposing functions and classes that can be imported from the notebook
Example
Section titled “Example”Consider a marimo notebook with the following four cells.
First cell:
print(text.value)Second cell:
def say_hello(name="World"): return f"Hello, {name}!"Third cell:
text = mo.ui.text(value=say_hello())textFourth cell:
import marimo as moFor the above example, marimo would generate the following file contents:
import marimo
__generated_with = "0.0.0"app = marimo.App()
@app.celldef _(text): print(text.value) return
@app.functiondef say_hello(name="World"): return f"Hello, {name}!"
@app.celldef _(mo): text = mo.ui.text(value=say_hello()) text return (text,)
@app.celldef _(): import marimo as mo return mo,
if __name__ == "__main__": app.run()As you can see, this is pure Python. This is part of the reason why marimo’s generated files are git-friendly: small changes made using the marimo editor result in small changes to the file that marimo generates.
Moreover, the cell defining a single pure function say_hello was saved “top-level” in the notebook file, making it possible for you to import it into other Python files or notebooks.
Properties
Section titled “Properties”marimo’s file format was designed to be easy to read and easy to work with, while also serving the needs of the marimo library. You can even edit the generated file’s cells directly, using your favorite text editor, and format the file with your favorite code formatter.
We explain some properties of marimo’s file format below.
Importing functions and classes from notebooks
Section titled “Importing functions and classes from notebooks”The details of marimo’s file format are important if you want to import functions and classes defined in your notebook into other Python modules. If you don’t intend to do so, you can skip this section.
Declaring imports used by functions and classes
Section titled “Declaring imports used by functions and classes”marimo can serialize functions and classes into the top-level of a file, so you can import them with regular Python syntax:
from my_notebook import my_functionIn particular, if a cell defines just a single function or class, and if that function or class is pure save for references to variables defined in a special setup cell, it will be serialized top-level.
Setup cell. Notebooks can optionally include a setup cell that imports modules, written in the file as:
with app.setup: import marimo as mo import dataclassesModules imported in a setup cell can be used in “top-level” functions or classes. You can add the setup cell from the general menu of the editor under: ::lucide:diamond-plus:: Add setup cell.
Functions and classes
Section titled “Functions and classes”Notebook files expose functions and classes that depend only on variables defined in the setup cell (or on other such functions or classes). For example, the following cell:
… is saved in the notebook file as
@app.functiondef roll_die(): ''' A reusable function.
Notice the indicator in the bottom right of the cell. ''' return random.randint(1, 7)Making it importable as
from my_notebook import roll_dieStandalone classes are also exposed:
This class is saved in the file as
@app.class_definition@dataclasses.dataclassclass SimulationExample: n_rolls: int
def simulate(self) -> list[int]: return [roll_die() for _ in range(self.n_rolls)]/// attention | Heads up ///
Not all standalone functions will be exposed in the module. If your function depends on variables that are defined in other cells, then it won’t be exposed top-level.
For example, this function will not be exposed:
I want to edit notebooks in a different editor, what do I need to know?
Section titled “I want to edit notebooks in a different editor, what do I need to know?”See the docs on using your own editor.
I want to import functions from a marimo notebook, what do I need to know?
Section titled “I want to import functions from a marimo notebook, what do I need to know?”See the docs on reusable functions and classes.
I want to run pytest on marimo notebooks, what do I need to know?
Section titled “I want to run pytest on marimo notebooks, what do I need to know?”See the docs on testing.
This notebook’s source code
Section titled “This notebook’s source code”The source code of this notebook is shown below: