> ## Documentation Index
> Fetch the complete documentation index at: /documents/llms.txt
> Use this file to discover all available pages before exploring further.

# Marimo

> 将 集群与 Marimo 响应式 Python 笔记本集成，实现可复现和交互的数据分析。

将您的 集群与 [Marimo](https://marimo.io/)，一个为可复现性和交互性而构建的响应式 Python 笔记本集成。

## 先决条件

首先，根据 [Marimo 快速入门文档](https://github.com/marimo-team/marimo#quickstart)安装 Marimo 并设置笔记本。

您还需要以下软件包：

```bash theme={null}
pip install starrocks sqlalchemy pandas
```

## 连接到 数据库

使用 [SQLAlchemy](https://www.sqlalchemy.org/) 创建连接引擎。连接字符串格式为：

```
starrocks://username:password@host:port/database
```

```python theme={null}
import marimo as mo
import sqlalchemy as sa

engine = sa.create_engine("starrocks://username:password@<host>:9030")
```

将 `<host>` 替换为您的 数据库 FE 主机。

## 使用 Marimo UI 获取凭据

为避免硬编码凭据，请使用 Marimo 的交互式 UI 元素在运行时收集它们。

**单元格 1** — 渲染输入字段：

```python theme={null}
user = mo.ui.text(label="Username")
pw = mo.ui.text(label="Password", kind="password")
mo.hstack([user, pw])
```

**单元格 2** — 使用输入的值创建引擎：

```python theme={null}
engine = sa.create_engine(
    f"starrocks://{user.value}:{pw.value}@<host>:9030"
)
```

## 查询 数据库

建立引擎后，使用 pandas 运行查询：

```python theme={null}
import pandas as pd

df = pd.read_sql("SELECT * FROM my_database.my_table LIMIT 100", engine)
mo.ui.table(df)
```

![连接到 数据库 的 Marimo 笔记本](https://mintcdn.com/mirrorship-docs/8gpPdHU-sBXASb5i/documents/images/21ac24e8691be17843809f3f.png?fit=max\&auto=format\&n=8gpPdHU-sBXASb5i\&q=85\&s=2c6023943edcd7d4bc919d322de17307)

:::注意

多目录支持需要 Marimo 0.22.5 或更高版本。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.