diff --git a/docs/content/api/modules.json.gz b/docs/content/api/modules.json.gz index 8406f90943c26..2f0515109e81c 100644 Binary files a/docs/content/api/modules.json.gz and b/docs/content/api/modules.json.gz differ diff --git a/docs/content/api/searchindex.json.gz b/docs/content/api/searchindex.json.gz index dd3eedef6fb30..2d28f17840e23 100644 Binary files a/docs/content/api/searchindex.json.gz and b/docs/content/api/searchindex.json.gz differ diff --git a/docs/content/api/sections.json.gz b/docs/content/api/sections.json.gz index 98b080f19608a..4b4c870698138 100644 Binary files a/docs/content/api/sections.json.gz and b/docs/content/api/sections.json.gz differ diff --git a/docs/content/guides/dagster/code-references.mdx b/docs/content/guides/dagster/code-references.mdx index 5f9e15fcdb6c6..8edc815cc8308 100644 --- a/docs/content/guides/dagster/code-references.mdx +++ b/docs/content/guides/dagster/code-references.mdx @@ -29,7 +29,9 @@ To complete the steps in this guide, you'll need: ## Automatically attaching code references to asset definitions -To automatically attach code references to the asset's function definition, you can use the `dagster._core.definitions.metadata.with_source_code_references` utility. Any asset definitions passed to the utility will have their source file attached as metadata. +### Assets defined in Python + +To automatically attach code references to Python assets' function definitions, you can use the `dagster._core.definitions.metadata.with_source_code_references` utility. Any asset definitions passed to the utility will have their source file attached as metadata. ```python file=/guides/dagster/code_references/with_source_code_references.py from dagster import Definitions, asset @@ -58,6 +60,43 @@ A link to the asset's source in `with_source_code_references.py` will then be vi /> +### dbt assets + +Dagster's dbt integration can automatically attach references to the SQL files backing your dbt assets. To enable this feature, set the `enable_code_references` parameter to `True` in the passed to your : + +```python file=/guides/dagster/code_references/with_dbt_code_references.py +from pathlib import Path + +from dagster_dbt import ( + DagsterDbtTranslator, + DagsterDbtTranslatorSettings, + DbtCliResource, + dbt_assets, +) + +from dagster import AssetExecutionContext, Definitions +from dagster._core.definitions.metadata import with_source_code_references + +manifest_path = Path("path/to/dbt_project/target/manifest.json") + +# links to dbt model source code from assets +dagster_dbt_translator = DagsterDbtTranslator( + settings=DagsterDbtTranslatorSettings(enable_code_references=True) +) + + +@dbt_assets( + manifest=manifest_path, + dagster_dbt_translator=dagster_dbt_translator, +) +def my_dbt_assets(context: AssetExecutionContext, dbt: DbtCliResource): + yield from dbt.cli(["build"], context=context).stream() + + +# optionally, add references to the Python source with with_source_code_references +defs = Definitions(assets=with_source_code_references([my_dbt_assets])) +``` + --- ## Manually attaching code references to asset definitions @@ -83,6 +122,7 @@ from dagster._core.definitions.metadata import ( code_references=[ LocalFileCodeReference( file_path=os.path.join(os.path.dirname(__file__), "source.yaml"), + # Label and line number are optional line_number=1, label="Model YAML", ) diff --git a/examples/docs_snippets/docs_snippets/guides/dagster/code_references/with_dbt_code_references.py b/examples/docs_snippets/docs_snippets/guides/dagster/code_references/with_dbt_code_references.py new file mode 100644 index 0000000000000..8f2cf0f2a452b --- /dev/null +++ b/examples/docs_snippets/docs_snippets/guides/dagster/code_references/with_dbt_code_references.py @@ -0,0 +1,30 @@ +from pathlib import Path + +from dagster_dbt import ( + DagsterDbtTranslator, + DagsterDbtTranslatorSettings, + DbtCliResource, + dbt_assets, +) + +from dagster import AssetExecutionContext, Definitions +from dagster._core.definitions.metadata import with_source_code_references + +manifest_path = Path("path/to/dbt_project/target/manifest.json") + +# links to dbt model source code from assets +dagster_dbt_translator = DagsterDbtTranslator( + settings=DagsterDbtTranslatorSettings(enable_code_references=True) +) + + +@dbt_assets( + manifest=manifest_path, + dagster_dbt_translator=dagster_dbt_translator, +) +def my_dbt_assets(context: AssetExecutionContext, dbt: DbtCliResource): + yield from dbt.cli(["build"], context=context).stream() + + +# optionally, add references to the Python source with with_source_code_references +defs = Definitions(assets=with_source_code_references([my_dbt_assets]))