PyMalloy
Author and run Malloy semantic models from Python. Malloy defines fields, measures, joins, and queries and compiles them to SQL. DuckDB executes the SQL.
pip install pymalloy
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 draft prints ordinary Malloy source. The query returns:
[{'region': 'North', 'revenue': Decimal('42')}, {'region': 'South', 'revenue': Decimal('30')}]
Drafts are immutable. Validate assumptions with named counterexample queries,
capture prepared Python data with pm.data(frame), and bundle the accepted model
with its inputs. The package includes the compiler, DuckDB execution, data
capture, and browser widgets.
- Run Python queries
- Author a model or edit an existing model
- Capture Python data
- Show a widget or run Python in the browser
- Export a notebook or ship a source bundle
- Use the Node or browser JavaScript API
For versioned agent guidance, start with help(pymalloy.agent) after
import pymalloy.agent.
PyMalloy builds on Malloy, which provides the language compiler and renderer. PyMalloy is an independent project and is not affiliated with or endorsed by the Malloy project.