Skip to main content

Scaffolding Dagster definitions

warning

This feature is considered in a preview stage and is under active development. It can change significantly, or be removed completely. It is not considered ready for production use.

dg can be used to help 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
.
├── my_project
│   ├── __init__.py
│   ├── definitions.py
│   ├── defs
│   │   ├── __init__.py
│   │   └── assets
│   │   └── my_asset.py
│   └── lib
│   └── __init__.py
├── my_project_tests
│   └── __init__.py
├── pyproject.toml
└── uv.lock

6 directories, 8 files
cat 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

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