> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-mintlify-8c05c8a2.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> marimo is a next-generation Python notebook for interacting with data

# Using marimo with ClickHouse

export const CommunityMaintainedBadge = () => {
  return <div className="CommunityMaintainedBadge">
            <div className="CommunityMaintainedIcon">
            <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" fill="currentColor" viewBox="0 0 256 256">
                <path d="M244.8,150.4a8,8,0,0,1-11.2-1.6A51.6,51.6,0,0,0,192,128a8,8,0,0,1-7.37-4.89,8,8,0,0,1,0-6.22A8,8,0,0,1,192,112a24,24,0,1,0-23.24-30,8,8,0,1,1-15.5-4A40,40,0,1,1,219,117.51a67.94,67.94,0,0,1,27.43,21.68A8,8,0,0,1,244.8,150.4ZM190.92,212a8,8,0,1,1-13.84,8,57,57,0,0,0-98.16,0,8,8,0,1,1-13.84-8,72.06,72.06,0,0,1,33.74-29.92,48,48,0,1,1,58.36,0A72.06,72.06,0,0,1,190.92,212ZM128,176a32,32,0,1,0-32-32A32,32,0,0,0,128,176ZM72,120a8,8,0,0,0-8-8A24,24,0,1,1,87.24,82a8,8,0,1,0,15.5-4A40,40,0,1,0,37,117.51,67.94,67.94,0,0,0,9.6,139.19a8,8,0,1,0,12.8,9.61A51.6,51.6,0,0,1,64,128,8,8,0,0,0,72,120Z"></path>
            </svg>
        </div>
            Community Maintained
        </div>;
};

export const Image = ({img, alt, size}) => {
  return <Frame>
      <img src={img} alt={alt} />
    </Frame>;
};

[marimo](https://marimo.io/) is an open-source reactive notebook for Python with SQL built-in. When you run a cell or interact with a UI element, marimo automatically runs affected cells (or marks them as stale), keeping code and outputs consistent and preventing bugs before they happen. Every marimo notebook is stored as pure Python, executable as a script, and deployable as an app.

<Image img="https://mintcdn.com/private-7c7dfe99-mintlify-8c05c8a2/Sna5oHHnZYlq11fb/images/integrations/sql-clients/marimo/clickhouse-connect.gif?s=4deff1c015b99c67e7cfe9ec5c434cd3" size="md" border alt="Connect to ClickHouse" width="1536" height="864" data-path="images/integrations/sql-clients/marimo/clickhouse-connect.gif" />

<h2 id="install-marimo-sql">
  1. Install marimo with SQL support
</h2>

```shell theme={null}
pip install "marimo[sql]" clickhouse_connect
marimo edit clickhouse_demo.py
```

This should open up a web browser running on localhost.

<h2 id="connect-to-clickhouse">
  2. Connecting to ClickHouse.
</h2>

Navigate to the datasources panel on the left side of the marimo editor and click on 'Add database'.

<Image img="https://mintcdn.com/private-7c7dfe99-mintlify-8c05c8a2/EtZF01T1n3kdpgn9/images/integrations/sql-clients/marimo/panel-arrow.png?fit=max&auto=format&n=EtZF01T1n3kdpgn9&q=85&s=a1a248876ad1c0b4165da505d7fbab2a" size="sm" border alt="Add a new database" width="491" height="561" data-path="images/integrations/sql-clients/marimo/panel-arrow.png" />

You will be prompted to fill in the database details.

<Image img="https://mintcdn.com/private-7c7dfe99-mintlify-8c05c8a2/EtZF01T1n3kdpgn9/images/integrations/sql-clients/marimo/add-db-details.png?fit=max&auto=format&n=EtZF01T1n3kdpgn9&q=85&s=1082ee76bbab37c3d946b70c92ba882e" size="md" border alt="Fill in the database details" width="818" height="487" data-path="images/integrations/sql-clients/marimo/add-db-details.png" />

You will then have a cell that can be run to establish a connection.

<Image img="https://mintcdn.com/private-7c7dfe99-mintlify-8c05c8a2/EtZF01T1n3kdpgn9/images/integrations/sql-clients/marimo/run-cell.png?fit=max&auto=format&n=EtZF01T1n3kdpgn9&q=85&s=5244662df8112efe61f1c0e2652aa994" size="md" border alt="Run the cell to connect to ClickHouse" width="995" height="283" data-path="images/integrations/sql-clients/marimo/run-cell.png" />

<h2 id="run-sql">
  3. Run SQL
</h2>

Once you have set up a connection, you can create a new SQL cell and choose the clickhouse engine.

<Image img="https://mintcdn.com/private-7c7dfe99-mintlify-8c05c8a2/EtZF01T1n3kdpgn9/images/integrations/sql-clients/marimo/choose-sql-engine.png?fit=max&auto=format&n=EtZF01T1n3kdpgn9&q=85&s=bea91a9b71b67c831bdc6ed46ca9dfa7" size="md" border alt="Choose SQL engine" width="2762" height="709" data-path="images/integrations/sql-clients/marimo/choose-sql-engine.png" />

For this guide, we will use the New York Taxi dataset.

```sql theme={null}
CREATE TABLE trips (
    trip_id             UInt32,
    pickup_datetime     DateTime,
    dropoff_datetime    DateTime,
    pickup_longitude    Nullable(Float64),
    pickup_latitude     Nullable(Float64),
    dropoff_longitude   Nullable(Float64),
    dropoff_latitude    Nullable(Float64),
    passenger_count     UInt8,
    trip_distance       Float32,
    fare_amount         Float32,
    extra               Float32,
    tip_amount          Float32,
    tolls_amount        Float32,
    total_amount        Float32,
    payment_type        Enum('CSH' = 1, 'CRE' = 2, 'NOC' = 3, 'DIS' = 4, 'UNK' = 5),
    pickup_ntaname      LowCardinality(String),
    dropoff_ntaname     LowCardinality(String)
)
ENGINE = MergeTree
PRIMARY KEY (pickup_datetime, dropoff_datetime);
```

```sql theme={null}
INSERT INTO trips
SELECT
    trip_id,
    pickup_datetime,
    dropoff_datetime,
    pickup_longitude,
    pickup_latitude,
    dropoff_longitude,
    dropoff_latitude,
    passenger_count,
    trip_distance,
    fare_amount,
    extra,
    tip_amount,
    tolls_amount,
    total_amount,
    payment_type,
    pickup_ntaname,
    dropoff_ntaname
FROM gcs(
    'https://storage.googleapis.com/clickhouse-public-datasets/nyc-taxi/trips_0.gz',
    'TabSeparatedWithNames'
);
```

```sql theme={null}
SELECT * FROM trips LIMIT 1000;
```

<Image img="https://mintcdn.com/private-7c7dfe99-mintlify-8c05c8a2/EtZF01T1n3kdpgn9/images/integrations/sql-clients/marimo/results.png?fit=max&auto=format&n=EtZF01T1n3kdpgn9&q=85&s=97fa123343186731cbcd7b224466be7b" size="lg" border alt="Results in a dataframe" width="947" height="415" data-path="images/integrations/sql-clients/marimo/results.png" />

Now, you're able to view the results in a dataframe. I would like to visualize the most expensive drop-offs from a given pickup location. marimo provides several UI components to help you. I will use a dropdown to select the location and altair for charting.

<Image img="https://mintcdn.com/private-7c7dfe99-mintlify-8c05c8a2/EtZF01T1n3kdpgn9/images/integrations/sql-clients/marimo/dropdown-cell-chart.png?fit=max&auto=format&n=EtZF01T1n3kdpgn9&q=85&s=0c242f474ad5bfc7c054a55af69c7393" size="lg" border alt="Combination of dropdown, table and chart" width="2720" height="2115" data-path="images/integrations/sql-clients/marimo/dropdown-cell-chart.png" />

marimo's reactive execution model extends into SQL queries, so changes to your SQL will automatically trigger downstream computations for dependent cells (or optionally mark cells as stale for expensive computations). Hence the chart and table changes when the query is updated.

You can also toggle App View to have a clean interface for exploring your data.

<Image img="https://mintcdn.com/private-7c7dfe99-mintlify-8c05c8a2/EtZF01T1n3kdpgn9/images/integrations/sql-clients/marimo/run-app-view.png?fit=max&auto=format&n=EtZF01T1n3kdpgn9&q=85&s=041fd69eeef22e9dbca6e3b82bf7ff9b" size="md" border alt="Run app view" width="2209" height="1534" data-path="images/integrations/sql-clients/marimo/run-app-view.png" />
