Skip to main content

Adding components to your project with Python

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.

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.

  1. First, create a new subdirectory in your components/ directory to contain the component definition.
  2. 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_components import component
from dagster_components.core.context import ComponentLoadContext
from dagster_components.dagster_dbt import DbtProjectComponent
from dagster_dbt import DagsterDbtTranslator, DbtCliResource


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.