Adding Dagster definitions to a dg project
dg
and Dagster Components are under active development. You may encounter feature gaps, and the APIs may change. To report issues or give feedback, please join the #dg-components channel in the Dagster Community Slack.
You can use the dg
CLI to scaffold Dagster definitions such as assets, schedules, and sensors.
All definitions added underneath the defs
directory of a project created with the create-dagster
CLI will be automatically loaded into the top-level Definitions
object.
Prerequisites
Before scaffolding definitions with dg
, you must create a project with the create-dagster CLI
and activate its virtual environment.
1. Scaffold an asset
You can use the dg scaffold defs
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 defs dagster.asset assets/my_asset.py
Creating a component at /.../my-project/src/my_project/defs/assets/my_asset.py.
Once the asset has been scaffolded, we can see that a new file has been added to defs/assets
, and view its contents:
- uv
- pip
tree
.
├── pyproject.toml
├── src
│ └── my_project
│ ├── __init__.py
│ ├── definitions.py
│ └── defs
│ ├── __init__.py
│ └── assets
│ └── my_asset.py
├── tests
│ └── __init__.py
└── uv.lock
6 directories, 7 files
tree
.
├── pyproject.toml
├── src
│ └── my_project
│ ├── __init__.py
│ └── defs
│ ├── __init__.py
│ └── assets
│ └── my_asset.py
├── tests
│ └── __init__.py
└── uv.lock
6 directories, 6 files
cat src/my_project/defs/assets/my_asset.py
import dagster as dg
@dg.asset
def my_asset(context: dg.AssetExecutionContext) -> dg.MaterializeResult: ...
You can run dg scaffold defs
from within any directory in your project and the resulting files will always be created in the <project-name>/src/<project_name>/defs/
folder.
2. Write an asset definition
In the above example, the scaffolded asset contains a basic commented-out definition. You can replace this definition with working code:
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!")
3. Check your work
Finally, you 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. │ │
│ │ └──────────┴──────────┴──────┴───────┴────────────────────────┘ │
└─────────┴─────────────────────────────────────────────────────────────────┘