From c34c825789f962495125bcdb4862ab7cc1554397 Mon Sep 17 00:00:00 2001 From: George Sittas Date: Wed, 5 Mar 2025 22:38:32 +0200 Subject: [PATCH] Chore: improve python model blueprint example docs --- docs/concepts/models/python_models.md | 40 ++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/docs/concepts/models/python_models.md b/docs/concepts/models/python_models.md index e838f4ae00..201cb60ec6 100644 --- a/docs/concepts/models/python_models.md +++ b/docs/concepts/models/python_models.md @@ -327,7 +327,45 @@ Note that arguments must be specified explicitly - variables cannot be accessed ## Python model blueprinting -A Python model can also serve as a template for creating multiple models, or _blueprints_, by specifying a list of key-value dicts in the `blueprints` property. Please refer to the Python-based SQL model [documentation page](./sql_models.md#Python-model-blueprinting)) for more details. +A Python model can also serve as a template for creating multiple models, or _blueprints_, by specifying a list of key-value dicts in the `blueprints` property. In order to achieve this, the model's name must be parameterized with a variable that exists in this mapping. + +For instance, the following model will result into two new models, each using the corresponding mapping in the `blueprints` property: + +```python linenums="1" +import typing as t +from datetime import datetime + +import pandas as pd +from sqlmesh import ExecutionContext, model + +@model( + "@{customer}.some_table", + kind="FULL", + blueprints=[ + {"customer": "customer1", "field_a": "x", "field_b": "y"}, + {"customer": "customer2", "field_a": "z", "field_b": "w"}, + ], + columns={ + "field_a": "text", + "field_b": "text", + "customer": "text", + }, +) +def entrypoint( + context: ExecutionContext, + start: datetime, + end: datetime, + execution_time: datetime, + **kwargs: t.Any, +) -> pd.DataFrame: + return pd.DataFrame( + { + "field_a": [context.var("field_a")], + "field_b": [context.var("field_b")], + "customer": [context.var("customer")], + } + ) +``` ## Examples ### Basic