marimo
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinesemarimo
marimo
Overview
概述
marimo is an open-source reactive Python notebook that reinvents notebooks as reproducible, interactive, and shareable Python programs. Unlike traditional Jupyter notebooks, marimo notebooks:
- Store as pure files (Git-friendly, no JSON)
.py - Execute reactively (like a spreadsheet)
- Run as scripts or deploy as web apps
- Prevent bugs through automatic dependency tracking
marimo是一款开源的响应式Python笔记本,它将笔记本重新定义为可复现、交互式且可共享的Python程序。与传统Jupyter笔记本不同,marimo笔记本具备以下特性:
- 以纯文件格式存储(对Git友好,无JSON冗余)
.py - 响应式执行(类似电子表格)
- 可作为脚本运行或部署为Web应用
- 通过自动依赖追踪避免错误
Installation
安装
bash
pip install marimo # Basic
pip install marimo[recommended] # With extras
pip install marimo[sql] # With SQL support
marimo tutorial intro # Start tutorialbash
pip install marimo # 基础版
pip install marimo[recommended] # 含扩展功能
pip install marimo[sql] # 含SQL支持
marimo tutorial intro # 启动教程CLI Commands
CLI命令
bash
undefinedbash
undefinedEdit
编辑
marimo edit # New notebook
marimo edit notebook.py # Edit existing
marimo edit --watch --sandbox # Watch files, isolate deps
marimo edit # 创建新笔记本
marimo edit notebook.py # 编辑现有笔记本
marimo edit --watch --sandbox # 监听文件变化,隔离依赖
Run as app
作为应用运行
marimo run notebook.py # Read-only app
marimo run notebook.py --watch # Auto-reload on changes
marimo run notebook.py # 只读模式运行应用
marimo run notebook.py --watch # 文件变化时自动重载
Run as script
作为脚本运行
python notebook.py
python notebook.py
Create from prompt
根据提示创建
marimo new "analyze sales data"
marimo new "analyze sales data"
Convert
格式转换
marimo convert notebook.ipynb -o notebook.py
marimo convert notebook.ipynb -o notebook.py
Export
导出
marimo export html notebook.py -o output.html
marimo export html-wasm notebook.py -o output.html # Browser-executable
undefinedmarimo export html notebook.py -o output.html
marimo export html-wasm notebook.py -o output.html # 可在浏览器中执行的版本
undefinedKey Concepts
核心概念
Reactivity
响应式
Running a cell automatically runs all cells that depend on it. Execution order is determined by variable dependencies (DAG), not cell position.
运行一个单元格时,会自动运行所有依赖于它的单元格。执行顺序由变量依赖关系(DAG)决定,而非单元格位置。
Cell Rules
单元格规则
- Each global variable must be defined in exactly one cell
- Mutations like aren't tracked; reassign instead
list.append() - If mutating is needed, do it in the same cell as the declaration
- 每个全局变量必须在恰好一个单元格中定义
- 类似的突变操作不会被追踪;请改用重新赋值
list.append() - 若必须进行突变操作,请在变量声明的同一单元格中完成
File Format
文件格式
Notebooks are pure Python files with marimo decorators:
python
import marimo
app = marimo.App()
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell
def _(mo):
mo.md("# My Notebook")
return ()笔记本是带有marimo装饰器的纯Python文件:
python
import marimo
app = marimo.App()
@app.cell
def _():
import marimo as mo
return (mo,)
@app.cell
def _(mo):
mo.md("# My Notebook")
return ()API Reference
API参考
Detailed documentation for each API is available in the directory. Consult these files for comprehensive examples and parameters.
references/各API的详细文档可在目录中查看。如需全面的示例和参数说明,请查阅这些文件。
references/Core
核心
| API | Reference File | Description |
|---|---|---|
| Markdown | | |
| HTML | | |
| Outputs | | |
| API | 参考文档 | 描述 |
|---|---|---|
| Markdown | | |
| HTML | | |
| Outputs | | |
UI Elements
UI组件
| API | Reference File | Description |
|---|---|---|
| Inputs | | Sliders, text, dropdowns, tables, forms, etc. |
| Layouts | | |
| Media | | Images, audio, video, PDF, downloads |
| Plotting | | Altair, Plotly, matplotlib integration |
| Diagrams | | Mermaid diagrams |
| Status | | Progress bars, spinners |
| API | 参考文档 | 描述 |
|---|---|---|
| Inputs | | 滑块、文本框、下拉菜单、表格、表单等 |
| Layouts | | |
| Media | | 图片、音频、视频、PDF、下载功能 |
| Plotting | | 与Altair、Plotly、matplotlib的集成 |
| Diagrams | | Mermaid图表 |
| Status | | 进度条、加载动画 |
Data
数据处理
| API | Reference File | Description |
|---|---|---|
| SQL | | |
| API | 参考文档 | 描述 |
|---|---|---|
| SQL | | |
Advanced
进阶功能
| API | Reference File | Description |
|---|---|---|
| Control Flow | | |
| State | | |
| Caching | | |
| Query Params | | |
| CLI Args | | |
| Watch | | |
| App | | Embedding notebooks, |
| Cell | | Cross-notebook execution, testing |
| API | 参考文档 | 描述 |
|---|---|---|
| Control Flow | | |
| State | | |
| Caching | | |
| Query Params | | |
| CLI Args | | |
| Watch | | |
| App | | 笔记本嵌入、 |
| Cell | | 跨笔记本执行、测试 |
Quick Examples
快速示例
Basic UI
基础UI
python
import marimo as mo
slider = mo.ui.slider(0, 100, value=50, label="Threshold")
sliderpython
import marimo as mo
slider = mo.ui.slider(0, 100, value=50, label="Threshold")
sliderIn another cell
在另一个单元格中
mo.md(f"Selected value: {slider.value}")
undefinedmo.md(f"Selected value: {slider.value}")
undefinedInteractive Table
交互式表格
python
table = mo.ui.table(df, selection="multi")
tablepython
table = mo.ui.table(df, selection="multi")
tableIn another cell
在另一个单元格中
selected = table.value # Selected rows as DataFrame
undefinedselected = table.value # 选中的行以DataFrame形式返回
undefinedSQL Query
SQL查询
python
result = mo.sql(f"SELECT * FROM {df} WHERE value > {threshold.value}")python
result = mo.sql(f"SELECT * FROM {df} WHERE value > {threshold.value}")Conditional Execution
条件执行
python
mo.stop(form.value is None, mo.md("Submit the form to continue"))python
mo.stop(form.value is None, mo.md("Submit the form to continue"))Rest of cell runs only after form submission
仅在表单提交后,单元格剩余代码才会运行
undefinedundefinedRunning as Apps
作为应用运行
bash
marimo run notebook.py # Local app
marimo export html-wasm notebook.py # Static WASM appLayout options: Vertical (default), Grid (drag-drop in editor), Slides.
bash
marimo run notebook.py # 本地运行应用
marimo export html-wasm notebook.py # 导出为静态WASM应用布局选项:垂直布局(默认)、网格布局(编辑器中可拖拽调整)、幻灯片布局。
Best Practices
最佳实践
- Reactivity: Declare and mutate variables in the same cell
- Performance: Use for expensive computations
@mo.cache - UI Gating: Use to prevent expensive ops until ready
mo.stop() - State: Avoid unless synchronizing multiple UI elements
mo.state() - Organization: Cell position doesn't matter; organize for readability
- 响应式编程:在同一单元格中声明并修改变量
- 性能优化:对耗时计算使用
@mo.cache - UI管控:使用避免在准备就绪前执行昂贵操作
mo.stop() - 状态管理:除非需要同步多个UI元素,否则避免使用
mo.state() - 组织规范:单元格位置不影响执行顺序;请按可读性组织内容