MotherDuck

MotherDuck 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.

pip install "marimo[sql]"
uv pip install "marimo[sql]"
conda install -c conda-forge marimo duckdb

To connect to MotherDuck, import duckdb and ATTACH your MotherDuck database.

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:

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.

Browse your MotherDuck databases

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:

Query a MotherDuck table

marimo’s reactive execution model extends into SQL queries, so changes to your SQL will automatically trigger downstream computations for dependent cells.

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:

Mixing SQL and Python

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.

marimo edit https://github.com/marimo-team/marimo/blob/main/examples/sql/connect_to_motherduck.py