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 traditional Dagster definitions such as schedules, sensors, and assets. When using 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 new definitions to be easily added to a 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.
dg scaffold
1. Scaffold an asset
You can use the dg scaffold
command to scaffold a new asset underneath the defs
folder. For this example, we'll scaffold an asset named my_asset.py
, and write it to the defs/assets
directory.
dg scaffold asset assets/my_asset.py
Using /.../my-project/.venv/bin/dagster-components
Creating a Dagster component instance folder at /.../my-project/my_project/defs/assets/my_asset.py.
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: ...
2. Write a definition
As seen in the above example, the scaffolded asset contains a basic commented-out definition. We'll want to 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!")
3. Checking our work
Finally, if we run dg list defs
, we can confirm that our 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. │
└──────────┴──────────┴──────┴───────┴────────────────────────┘