Skip to content

PyMalloyAuthor and run Malloy models from Python.

Define semantic models, query with DuckDB, and share models and inputs in notebooks or source bundles.

Malloy defines sources, measures, joins, and queries and compiles them to SQL. DuckDB executes the SQL. Install PyMalloy:

sh
pip install pymalloy
python
import pymalloy as pm

orders = pm.sql(
    "SELECT * FROM (VALUES ('North', 20), ('North', 22), ('South', 30)) t(region, amount)"
)
model = (
    pm.draft()
    .sources(
        orders=orders.measure(
            revenue=pm.col("amount").sum().doc("Booked amount in USD.")
        )
    )
    .queries(
        by_region=pm.source("orders").pipe(
            pm.view().group_by("region").aggregate("revenue").order_by("region")
        )
    )
)
print(model.text)
with pm.connect() as con:
    print(con.model(model).query("by_region").run().to_pylist())

The query returns North revenue 42 and South revenue 30 as exact decimal values.

Choose a task ​

Your goalStart here
Run your first Python queryGetting started
Reuse a model, select queries, or inspect SQLRun queries from Python
Understand drafts, models, and captured evidenceConcepts and boundaries
Compose sources, measures, joins, and viewsAuthor a model
Change an existing Malloy fileEdit a model
Connect files or catalog tablesData access
Bring prepared Python data into a modelCapture Python data
Bind typed query parametersGivens
Display and update a notebook widgetWidgets
Run Python in a browser notebookBrowser Python
Inspect intermediate values in a notebookNotebook editing
Share an executable notebookExport notebooks
Ship Malloy files and copied inputsSource bundles
Diagnose a failureTroubleshooting
Use JavaScriptNode or browser
Look up an exact contractPython API or CLI