Adding components to your project with Python
info
This feature is still in development and might change in patch releases. It’s not production ready, and the documentation may also evolve. Stay tuned for updates.
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 components.
- 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_components import ComponentLoadContext, component
from dagster_components.lib 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.