Adding components to your project with Python
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.
In some cases, you may want to add a component to your project with Python rather than a component.yaml
file.
Prerequisites
Before adding a component with Python, you must either create a project with components or migrate an existing project to dg
.
- First, create a new subdirectory in your
components/
directory to contain the component definition. - In the subdirectory, create a
component.py
file to define your component instance. In this file, you will define a single@component
-decorated function that instantiates the component type that you're interested in:
from dagster_dbt import DagsterDbtTranslator, DbtCliResource, DbtProjectComponent
from dagster.components import ComponentLoadContext, component
class MyTranslator(DagsterDbtTranslator): ...
@component
def my_dbt_component(context: ComponentLoadContext) -> DbtProjectComponent:
return DbtProjectComponent(
dbt=DbtCliResource(project_dir="."),
translator=MyTranslator(),
)
This function needs to return an instance of your desired component type. In the example above, we've used this functionality to customize the translator
argument of the DbtProjectcomponent
class.