Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

docs: motherduck (#2664)

Myles Scolnick84c7984e53d3
diff --git a/docs/_static/css/custom.css b/docs/_static/css/custom.css
index fffc42862a..0e95e6c0f1 100644
--- a/docs/_static/css/custom.css
+++ b/docs/_static/css/custom.css
@@ -1,6 +1,6 @@
@font-face {
- font-family: "CalSans";
- src: url("../CalSans-SemiBold.woff");
+ font-family: 'CalSans';
+ src: url('../CalSans-SemiBold.woff');
}
body .page {
@@ -13,7 +13,7 @@ body .page {
}
.sidebar-brand-text {
- font-family: "CalSans";
+ font-family: 'CalSans';
color: #1c7362;
font-weight: 600;
font-size: 1.85rem;
@@ -23,16 +23,17 @@ body .page {
body {
color-scheme: light;
}
-body[data-theme="dark"] {
+body[data-theme='dark'] {
color-scheme: dark;
}
h1,
h2 {
- font-family: "CalSans";
+ font-family: 'CalSans';
+ letter-spacing: 0.025em;
}
-[data-theme="dark"] .sidebar-brand-text {
+[data-theme='dark'] .sidebar-brand-text {
color: #fff;
}
@@ -70,13 +71,13 @@ iframe.demo.xxlarge {
/* Better formatting of signatures from https://github.com/sphinx-doc/sphinx/issues/1514#issuecomment-742703082 */
/* Newlines (\a) and spaces (\20) before each parameter */
.sig-param::before {
- content: "\a\20\20";
+ content: '\a\20\20';
white-space: pre;
}
/* Newline after the last parameter (so the closing bracket is on a new line) */
dt em.sig-param:last-of-type::after {
- content: "\a";
+ content: '\a';
white-space: pre;
}
@@ -94,7 +95,7 @@ figcaption {
color: gray;
}
-[data-theme="dark"] figcaption {
+[data-theme='dark'] figcaption {
color: var(--color-content-foreground);
}
diff --git a/docs/_static/motherduck/motherduck_dag.png b/docs/_static/motherduck/motherduck_dag.png
new file mode 100644
index 0000000000..e32c9bbc26
Binary files /dev/null and b/docs/_static/motherduck/motherduck_dag.png differ
diff --git a/docs/_static/motherduck/motherduck_db_discovery.png b/docs/_static/motherduck/motherduck_db_discovery.png
new file mode 100644
index 0000000000..28980e1fb5
Binary files /dev/null and b/docs/_static/motherduck/motherduck_db_discovery.png differ
diff --git a/docs/_static/motherduck/motherduck_python_and_sql.png b/docs/_static/motherduck/motherduck_python_and_sql.png
new file mode 100644
index 0000000000..7ef308f302
Binary files /dev/null and b/docs/_static/motherduck/motherduck_python_and_sql.png differ
diff --git a/docs/_static/motherduck/motherduck_reactivity.mp4 b/docs/_static/motherduck/motherduck_reactivity.mp4
new file mode 100644
index 0000000000..f1ce045555
Binary files /dev/null and b/docs/_static/motherduck/motherduck_reactivity.mp4 differ
diff --git a/docs/_static/motherduck/motherduck_sql.png b/docs/_static/motherduck/motherduck_sql.png
new file mode 100644
index 0000000000..23d3265dea
Binary files /dev/null and b/docs/_static/motherduck/motherduck_sql.png differ
diff --git a/docs/apps/README.md b/docs/apps/README.md
new file mode 100644
index 0000000000..4047434ade
--- /dev/null
+++ b/docs/apps/README.md
@@ -0,0 +1,3 @@
+# Documentation Apps
+
+This directory contains marimo apps that are used in the documentation.
diff --git a/docs/apps/motherduck.py b/docs/apps/motherduck.py
new file mode 100644
index 0000000000..ba8cd685cf
--- /dev/null
+++ b/docs/apps/motherduck.py
@@ -0,0 +1,103 @@
+import marimo
+
+__generated_with = "0.9.10"
+app = marimo.App(width="full")
+
+
+def __():
+ import marimo as mo
+ import duckdb
+ return duckdb, mo
+
+
+def __(duckdb):
+ duckdb.sql(
+ "ATTACH 'md:_share/sample_data/23b0d623-1361-421d-ae77-62d701d471e6' AS sample_data"
+ )
+ return (sample_data,)
+
+
+def __(mo):
+ mo.md(r"""## Reactive SQL""")
+ return
+
+
+def __(mo):
+ last_x_months = mo.ui.slider(24, 30, label="Last x months")
+ last_x_months
+ return (last_x_months,)
+
+
+def __(last_x_months, mo):
+ recent_hacker_news = mo.sql(
+ f"""
+ FROM sample_data.hn.hacker_news
+ WHERE timestamp >= (CURRENT_DATE - INTERVAL {last_x_months.value} month)
+ AND type = 'story'
+ """
+ )
+ return (recent_hacker_news,)
+
+
+def __(mo, recent_hacker_news):
+ aggregations = mo.sql(
+ f"""
+ SELECT
+ COUNT(*) AS total_posts, AVG(score) AS avg_score,
+ MAX(score) AS max_score, MIN(score) AS min_score,
+ FROM recent_hacker_news WHERE score IS NOT NULL;
+ """
+ )
+ return (aggregations,)
+
+
+def __(mo):
+ mo.md(r"""## Mix and match Python""")
+ return
+
+
+def __(mo, sample_data, service_requests):
+ agency_tickets = mo.sql(
+ f"""
+ SELECT
+ agency_name,
+ COUNT(*) AS num_requests,
+ CAST(SUM(CASE WHEN status = 'Closed' THEN 1 ELSE 0 END) AS INT64) AS closed_count,
+ CAST(SUM(CASE WHEN status = 'Open' THEN 1 ELSE 0 END) AS INT64) AS open_count
+ FROM sample_data.nyc.service_requests
+ GROUP BY agency_name ORDER BY closed_count DESC LIMIT 20
+ """
+ )
+ return (agency_tickets,)
+
+
+def __(agency_tickets):
+ import altair as alt
+
+ scale = alt.Scale(type="sqrt")
+ base = (
+ alt.Chart(agency_tickets)
+ .encode(y="agency_name", x=alt.X("num_requests", scale=scale))
+ .properties(width="container")
+ )
+ chart_closed = base.mark_bar(color="#FFC080").encode(
+ x=alt.X("closed_count", scale=scale)
+ )
+ chart_open = base.mark_bar(color="#8BC34A").encode(
+ x=alt.X("open_count", scale=scale)
+ )
+ chart_closed + chart_open
+ return alt, base, chart_closed, chart_open, scale
+
+
+if __name__ == "__main__":
+ app.run()
diff --git a/docs/integrations/index.md b/docs/integrations/index.md
index f0f3bfaf82..43241ed403 100644
--- a/docs/integrations/index.md
+++ b/docs/integrations/index.md
@@ -5,15 +5,17 @@
:maxdepth: 2
:hidden:
+ motherduck
google_cloud_storage
google_cloud_bigquery
google_sheets
```
-It is easy to integrate your preferred data sources with marimo. Since marimo is strictly Python, you can utilize any Python library to access your data. In this section, we provide some examples of how to integrate with popular data sources.
+It is easy to integrate your preferred data sources or data warehouses with marimo. Since marimo is strictly Python, you can utilize any Python library to access your data. In this section, we provide some examples of how to integrate with popular data sources.
| | |
| :--------------------------- | :------------------------------------- |
+| {doc}`motherduck` | Integrating with MotherDuck |
| {doc}`google_cloud_storage` | Integrating with Google Cloud Storage |
| {doc}`google_cloud_bigquery` | Integrating with Google Cloud BigQuery |
| {doc}`google_sheets` | Integrating with Google Sheets |
diff --git a/docs/integrations/motherduck.md b/docs/integrations/motherduck.md
new file mode 100644
index 0000000000..1015767f75
--- /dev/null
+++ b/docs/integrations/motherduck.md
@@ -0,0 +1,94 @@
+# MotherDuck
+
+[MotherDuck](https://motherduck.com/) is a cloud-based data warehouse that combines the power of DuckDB with the scalability of the cloud. This guide will help you integrate MotherDuck with marimo.
+
+## 1. Connecting to MotherDuck
+
+To use MotherDuck as a data source, you'll need to install the `marimo[sql]` Python package.
+
+::::{tab-set}
+:::{tab-item} install with pip
+
+```bash
+pip install "marimo[sql]"
+```
+
+:::
+:::{tab-item} install with uv
+
+```bash
+uv pip install "marimo[sql]"
+```
+
+:::
+:::{tab-item} install with conda
+
+```bash
+conda install -c conda-forge marimo duckdb
+```
+
+:::
+::::
+
+To connect to MotherDuck, import `duckdb` and `ATTACH` your MotherDuck database.
+
+```python
+import duckdb
+
+# Connect to MotherDuck
+duckdb.sql("ATTACH 'md:your_motherduck_database_name' AS sample_data")
+```
+
+You will be prompted to authenticate with MotherDuck when you run the above cell. This will open a browser window where you can log in and authorize marimo to access your MotherDuck database. In order to avoid being prompted each time you open a notebook, you can set the `motherduck_token` environment variable:
+
+```bash
+export motherduck_token="your_motherduck_token_here"
+marimo edit
+```
+
+You can obtain this token from your MotherDuck account settings.
+
+Once you've authenticated, your MotherDuck tables get automatically discovered and you can browse them from the Datasources Panel.
+
+<div align="center">
+ <figure>
+ <img src="/_static/motherduck/motherduck_db_discovery.png"/>
+ <figcaption>Browse your MotherDuck databases</figcaption>
+ </figure>
+</div>
+
+## 2. Querying Your Tables
+
+Once connected, you can query your MotherDuck tables using SQL. Here's an example of how to query a table and display the results using marimo:
+
+<div align="center">
+ <figure>
+ <img src="/_static/motherduck/motherduck_sql.png"/>
+ <figcaption>Query a MotherDuck table</figcaption>
+ </figure>
+</div>
+
+marimo's reactive execution model extends into SQL queries, so changes to your SQL will automatically trigger downstream computations for dependent cells.
+
+<video controls width="100%" height="100%" align="center" src="/_static/motherduck/motherduck_reactivity.mp4"> </video>
+
+## 3. Mixing SQL and Python
+
+MotherDuck allows you to seamlessly mix SQL queries with Python code, enabling powerful data manipulation and analysis. Here's an example:
+
+<div align="center">
+ <figure>
+ <img src="/_static/motherduck/motherduck_python_and_sql.png"/>
+ <figcaption>Mixing SQL and Python</figcaption>
+ </figure>
+</div>
+
+This example demonstrates how you can use SQL to query your data, then use Python and marimo to further analyze and visualize the results.
+
+## Example
+
+For a full example of using MotherDuck with marimo, check out our [MotherDuck example notebook](https://github.com/marimo-team/marimo/blob/main/examples/sql/connect_to_motherduck.py).
+
+```bash
+marimo edit https://github.com/marimo-team/marimo/blob/main/examples/sql/connect_to_motherduck.py
+```
diff --git a/examples/sql/connect_to_motherduck.py b/examples/sql/connect_to_motherduck.py
index 10e8d13eb4..e5d777e0d3 100644
--- a/examples/sql/connect_to_motherduck.py
+++ b/examples/sql/connect_to_motherduck.py
@@ -10,7 +10,7 @@
import marimo
-__generated_with = "0.8.19"
+__generated_with = "0.9.10"
app = marimo.App(width="medium")
@@ -72,7 +72,7 @@ def __():
)
# or add your own md instance
# duckdb.sql(f"ATTACH IF NOT EXISTS 'md:sample_data'")
- return duckdb, mo
+ return duckdb, mo, sample_data
@app.cell(hide_code=True)
@@ -200,7 +200,7 @@ def __(mo):
@app.cell
-def __(MONTHS, duckdb, mo):
+def __(MONTHS, duckdb, hacker_news, mo, sample_data):
month_select = mo.ui.multiselect(
MONTHS,
label="Month",
@@ -325,7 +325,7 @@ def __(mo):
@app.cell
-def __(mo, search_value):
+def __(hacker_news, mo, sample_data, search_value):
keyword_results = mo.sql(
f"""
SELECT