[hypequery](https://hypequery.com/) › ClickHouse with Flask: Shared Analytics Definitions | hypequery
Index for agents: https://hypequery.com/llms.txt
Canonical page: https://hypequery.com/clickhouse-flask

> Serve ClickHouse analytics from Flask without copying SQL between routes. Define measures once in Python, query them per customer, export typed DataFrames.

[Open sourceApache 2.0](https://github.com/hypequery/hypequery)

ClickHouse Flask

# ClickHouse analytics in Flask routes, defined once

Flask keeps your routes small. hypequery keeps your ClickHouse analytics small too: measures and dimensions defined once in Python, queried by name from any route, and scoped to the customer making the request.

[Flask guide ](https://hypequery.com/docs/python/flask)[Python quick start](https://hypequery.com/docs/quick-start#python)

Works in

Any Flask route

Tenant from

A tenant loader

Exports

Typed pandas DataFrames

## SQL strings in every route

Each report route builds its own ClickHouse query. Renaming a column or changing what “revenue” means touches all of them.

## Request parsing is hand-rolled

Filters, groupings, and page sizes come from query strings, and each route validates them differently, or not at all.

## Customer scoping depends on memory

If a route forgets the organisation filter, it returns every customer’s data.

Flask routes

## Query datasets from your routes

Register the extension and a tenant loader. `hq.client` is then bound to the current customer in each request. hypequery plans and validates the query from your dataset, then adds the tenant filter.

- Config comes from `app.config` or `CLICKHOUSE_URL`, and `init_app` validates it at startup
- `async def` routes use `hq.async_client`; the tenant loader runs once per request
- Queries without a tenant fail instead of reading every customer
- One client per process, closed at exit, never per request

app.py

### A tenant-scoped Flask route

app.py

```
from flask import Flask
from flask_login import current_user
from hypequery.contrib.flask import Hypequery

hq = Hypequery()

@hq.tenant_loader
def current_tenant():
    return current_user.org_id if current_user.is_authenticated else None

def create_app():
    app = Flask(__name__)
    app.config["HYPEQUERY_REGISTRY"] = "analytics.datasets:registry"
    hq.init_app(app)  # connection from HYPEQUERY_URL or CLICKHOUSE_URL

    @app.get("/reports/revenue")
    def revenue():
        # hq.client is scoped to the current user's organization
        result = hq.client.execute(
            "orders", {"dimensions": ["country"], "measures": ["revenue"]}
        )
        return {"data": list(result.data)}

    return app
```

Flask-Login’s `current_user` stands in for your own auth.

Exports

## CSV and DataFrame exports from the same definitions

Results convert to typed pandas, Polars or Arrow frames using the dataset definition, so an export route is a few lines and its numbers match the dashboard.

`pip install "hypequery[flask,pandas]==0.1.0b3"`. Decimals become floats by default; pass `decimals="decimal"` to keep them exact.

For typed React hooks or the MCP bridge, register hypequery’s HTTP endpoints as a blueprint with `blueprint(api)`: no second service, and the session API uses your Flask login.

app.py

### A tenant-scoped CSV export

app.py

```
import io

from flask import Response

@app.get("/reports/revenue.csv")
def revenue_csv():
    # Scoped by the tenant loader, typed by the dataset definition
    result = hq.client.execute(
        "orders", {"measures": ["revenue"], "by": "day"}
    )
    frame = (
        result.to_pandas()
    )  # period: datetime64[us, UTC], revenue: float64
    buffer = io.StringIO()
    frame.to_csv(buffer, index=False)
    return Response(buffer.getvalue(), mimetype="text/csv")
```

The route reads the same tenant as every other route.

Where teams usually get stuck

## Questions teams ask

### Flask ClickHouse API

Return measures by dimension from Flask routes, scoped to the signed-in customer.

### Flask analytics dashboard

Back a dashboard with named measures so every chart uses the same definition.

### clickhouse-connect in Flask

hypequery runs on clickhouse-connect. You get the same driver, plus planning, validation, and tenancy on top.

### Is there a Flask extension?

Yes: `hypequery.contrib.flask.Hypequery` supports app factories, `app.config`, a tenant loader, and async routes.

Further reading

## Go deeper where it actually helps

### [Flask guide](https://hypequery.com/docs/python/flask)

[The extension, tenant loader and async routes.](https://hypequery.com/docs/python/flask)

[Open guide](https://hypequery.com/docs/python/flask)

### [Python quick start](https://hypequery.com/docs/quick-start#python)

[Install hypequery and define your first dataset.](https://hypequery.com/docs/quick-start#python)

[Open guide](https://hypequery.com/docs/quick-start#python)

### [Multi-tenancy](https://hypequery.com/docs/python/datasets/multi-tenancy)

[Tenant keys and tenant-bound clients.](https://hypequery.com/docs/python/datasets/multi-tenancy)

[Open guide](https://hypequery.com/docs/python/datasets/multi-tenancy)

### [pandas, Polars and Arrow](https://hypequery.com/docs/python/dataframes)

[Typed DataFrames for exports.](https://hypequery.com/docs/python/dataframes)

[Open guide](https://hypequery.com/docs/python/dataframes)

Related pillars

[ClickHouse with Python](https://hypequery.com/clickhouse-python)[ClickHouse with Django](https://hypequery.com/clickhouse-django)[ClickHouse with FastAPI](https://hypequery.com/clickhouse-fastapi)[Python multi-tenancy](https://hypequery.com/clickhouse-python-multi-tenant)

Next step

## Move one Flask report onto a dataset

Define the dataset behind your busiest report route, then replace its SQL with a scoped query.

Open source, and it runs in your own backend. Want it hosted?

[Flask guide ](https://hypequery.com/docs/python/flask)[Python quick start](https://hypequery.com/docs/quick-start#python)
