Skip to main content

Scaffolding Dagster definitions

info

This feature is considered in a preview stage and is under active development. There may be API changes and feature gaps. Please go to the #dg-components channel in our Slack to report issues or give feedback.

dg can be used to scaffold Dagster definitions such as assets, schedules, and sensors. When you use a project that has been scaffolded using dg, any new definitions added underneath the defs directory will be automatically loaded into the top-level Definitions object. This allows you to easily add new definitions to your project without needing to explicitly import these definitions to your top-level definitions file.

This guide will walk through how to use dg to scaffold a new asset.

Scaffold an asset

You can use the dg scaffold command to scaffold a new asset underneath the defs folder. In this example, we scaffold an asset named my_asset.py and write it to the defs/assets directory:

dg scaffold dagster.asset assets/my_asset.py
Using /.../my-project/.venv/bin/dagster-components
Using /.../my-project/.venv/bin/dagster-components

Once this is done, we can see that a new file has been added to this location, and view its contents:

tree
.
├── pyproject.toml
├── src
│   └── my_project
│   ├── __init__.py
│   ├── definitions.py
│   ├── defs
│   │   ├── __init__.py
│   │   └── assets
│   │   └── my_asset.py
│   └── lib
│   └── __init__.py
├── tests
│   └── __init__.py
└── uv.lock

7 directories, 8 files
cat src/my_project/defs/assets/my_asset.py
import dagster as dg


@dg.asset
def my_asset(context: dg.AssetExecutionContext) -> dg.MaterializeResult: ...

Write a definition

As seen in the above example, the scaffolded asset contains a basic commented-out definition. We can replace this definition with whatever asset code we're interested in:

import dagster as dg


@dg.asset(group_name="my_group")
def my_asset(context: dg.AssetExecutionContext) -> None:
"""Asset that greets you."""
context.log.info("hi!")

Check your work

Finally, we can run dg list defs to confirm that the new asset now appears in the list of definitions:

dg list defs

┏━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Section ┃ Definitions ┃
┡━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ Assets │ ┏━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━┳━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━┓ │
│ │ ┃ Key ┃ Group ┃ Deps ┃ Kinds ┃ Description ┃ │
│ │ ┡━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━╇━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━┩ │
│ │ │ my_asset │ my_group │ │ │ Asset that greets you. │ │
│ │ └──────────┴──────────┴──────┴───────┴────────────────────────┘ │
└─────────┴─────────────────────────────────────────────────────────────────┘