Skip to content

Commit ba5d017

Browse files
authored
DOC-878 Reorganize and clean up Components docs (#27848)
## Summary & Motivation Reorganize according to [proposed new IA](https://www.notion.so/dagster/Components-outline-v2-19918b92e46280b69264df356461bda4) and copyedit existing content. ## How I Tested These Changes ## Changelog > Insert changelog entry or delete this section. --------- Signed-off-by: nikki everett <[email protected]>
1 parent 618a12c commit ba5d017

20 files changed

+668
-835
lines changed

docs/docs-beta/src/code-examples-content.js

Lines changed: 0 additions & 387 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: 'Adding components to your project with Python'
3+
sidebar_position: 300
4+
---
5+
6+
:::info
7+
8+
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.
9+
10+
:::
11+
12+
In some cases, you may want to add a component to your project with Python rather than a `component.yaml` file.
13+
14+
:::note Prerequisites
15+
16+
Before adding a component with Python, you must either [create a project with components](/guides/labs/components/building-pipelines-with-components/creating-a-code-location-with-components) or [migrate an existing code location to components](/guides/labs/components/incrementally-adopting-components/existing-code-location).
17+
18+
:::
19+
20+
1. First, create a new subdirectory in your `components/` directory to contain the component definition.
21+
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:
22+
23+
<CodeExample path="docs_beta_snippets/docs_beta_snippets/guides/components/python-components/component.py" language="python" />
24+
25+
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.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: "Adding components to your project"
3+
sidebar_position: 200
4+
---
5+
6+
:::info
7+
8+
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.
9+
10+
:::
11+
12+
To add components to your project, you can instantiate them from the command line, which will create a new directory inside your `components/` folder that contains a `component.yaml` file.
13+
14+
If you want to use Python to add components to your project instead, see "[Adding components to your project with Python](adding-components-python)".
15+
16+
:::note Prerequisites
17+
18+
Before adding a component with Python, you must either [create a project with components](/guides/labs/components/building-pipelines-with-components/creating-a-code-location-with-components) or [migrate an existing code location to components](/guides/labs/components/incrementally-adopting-components/existing-code-location).
19+
20+
:::
21+
22+
## Finding a component
23+
24+
You can view the available component types in your environment by running the following command:
25+
26+
```bash
27+
dg component-type list
28+
```
29+
30+
This will display a list of all the component types that are available in your project. To see more information about a specific component type, you can run:
31+
32+
```bash
33+
dg component-type docs <component-name>
34+
```
35+
36+
This will display a webpage containing documentation for the specified component type.
37+
38+
## Instantiating a component
39+
40+
Once you've decided on the component type that you'd like to use, you can instantiate it by running:
41+
42+
```bash
43+
dg component generate <component-type> <component-name>
44+
```
45+
46+
This will create a new directory inside your `components/` folder that contains a `component.yaml` file. Some component types may also generate additional files as needed.
47+
48+
## Configuration
49+
50+
### Basic configuration
51+
52+
The `component.yaml` is the primary configuration file for a component. It contains two top-level fields:
53+
54+
- `type`: The type of the component defined in this directory
55+
- `params`: A dictionary of parameters that are specific to this component type. The schema for these parameters is defined by the `get_schema` method on the component class.
56+
57+
To see a sample `component.yaml` file for your specific component, you can run:
58+
59+
```bash
60+
dg component-type docs <component-name>
61+
```
62+
63+
### Component templating
64+
65+
Each `component.yaml` file supports a rich templating syntax, powered by `jinja2`.
66+
67+
#### Templating environment variables
68+
69+
A common use case for templating is to avoid exposing environment variables (particularly secrets) in your YAML files. The Jinja scope for a `component.yaml` file contains an `env` function that can be used to insert environment variables into the template:
70+
71+
```yaml
72+
component_type: my_snowflake_component
73+
74+
params:
75+
account: {{ env('SNOWFLAKE_ACCOUNT') }}
76+
password: {{ env('SNOWFLAKE_PASSWORD') }}
77+
```
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: 'Creating a code location with components'
3+
sidebar_position: 100
4+
---
5+
6+
:::info
7+
8+
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.
9+
10+
:::
11+
12+
:::note Prerequisites
13+
14+
Before creating a project with components, you must follow the [steps to install `uv` and `dg`](/guides/labs/components/index.md#installation).
15+
16+
:::
17+
18+
After [installing dependencies](/guides/labs/components/index.md#installation), you can scaffold a components-ready code location for your project. In the example below, we scaffold a code location called `jaffle-platform`:
19+
20+
<CliInvocationExample path="docs_beta_snippets/docs_beta_snippets/guides/components/index/2-scaffold.txt" />
21+
22+
This command builds a code location and initializes a new Python virtual environment inside of it. When using `dg`'s default environment management behavior, you won't need to worry about activating this virtual environment yourself.
23+
24+
## Overview of files and directories
25+
26+
Let's have a look at the scaffolded files:
27+
28+
<CliInvocationExample path="docs_beta_snippets/docs_beta_snippets/guides/components/index/3-tree.txt" />
29+
30+
You can see that we have a fairly standard Python project structure. The following files and directories are included:
31+
32+
- A Python package `jaffle_platform`-- the name is an underscored inflection of the
33+
project root directory (`jaffle_platform`).
34+
- An (empty) `jaffle_platform_tests` test package
35+
- A `uv.lock` file
36+
- A `pyproject.toml` file
37+
38+
### pyproject.toml
39+
40+
The `pyproject.toml` contains a `tool.dagster` and `tool.dg` section that look like
41+
this:
42+
43+
<CodeExample path="docs_beta_snippets/docs_beta_snippets/guides/components/index/4-pyproject.toml" language="TOML" title="jaffle-platform/pyproject.toml" />
44+
45+
#### tool.dagster section
46+
47+
The `tool.dagster` section of `pyproject.toml` is not `dg`-specific. This section specifies that a set of definitions can be loaded from the `jaffle_platform.definitions` module.
48+
49+
#### tool.dg section
50+
51+
The `tool.dg` section contains two settings requiring more explanation: `is_code_location` and `is_component_lib`.
52+
53+
##### is_code_location setting
54+
55+
`is_code_location = true` specifies that this project is a `dg`-managed Dagster code location. Code locations created with components are regular Dagster code locations with a particular structure.
56+
57+
To understand the structure, let's look at the content of `jaffle_platform/definitions.py`:
58+
59+
<CodeExample path="docs_beta_snippets/docs_beta_snippets/guides/components/index/5-definitions.py" language="Python" title="jaffle-platform/jaffle_platform/definitions.py" />
60+
61+
This call to `build_component_defs` will:
62+
63+
- discover the set of components defined in the project
64+
- compute a set of `Definitions` from each component
65+
- merge the component-specific definitions into a single `Definitions` object
66+
67+
`is_code_location` is telling `dg` that the project is structured in this way and therefore contains component instances. In the current project, component instances will be placed in the default location at `jaffle_platform/components`.
68+
69+
##### is_component_lib setting
70+
71+
`is_component_lib = true` specifies that the project is a component library. This means that the project may contain component types that can be referenced when generating component instances.
72+
73+
In a typical code location, most components are likely to be instances of types defined in external libraries (e.g. `dagster-components`), but you can also define custom component types scoped to your project. That is why `is_component_lib` is set to `true` by default. Any scaffolded component types in `jaffle_platform` will be placed in the default location at `jaffle_platform/lib`.
74+
75+
You can also see that this module is registered under the `dagster.components` entry point in `pyproject.toml`. This is what makes the components discoverable to `dg`:
76+
77+
<CodeExample path="docs_beta_snippets/docs_beta_snippets/guides/components/index/6-pyproject.toml" language="TOML" title="jaffle-platform/pyproject.toml" />
78+
79+
## Next steps
80+
81+
After scaffolding your code location with components, you can [add more components](adding-components) to complete your pipeline.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: 'Customizing components'
3+
sidebar_position: 400
4+
---
5+
6+
:::info
7+
8+
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.
9+
10+
:::
11+
12+
You can customize the behavior of a component beyond what is available in the `component.yaml` file.
13+
14+
To do so, you can create a subclass of your desired component in a file named `component.py` in the same directory as your `component.yaml` file. This subclass should be annotated with the `@component_type` decorator, which will define a local name for this component:
15+
16+
<CodeExample path="docs_beta_snippets/docs_beta_snippets/guides/components/custom-subclass/basic-subclass.py" language="python" />
17+
18+
You can then update the `type:` field in your `component.yaml` file to reference this new component type. The new type name will be `.<component-name>`, where the leading `.` indicates that this is a local component type:
19+
20+
```yaml
21+
type: .custom_subclass
22+
23+
params:
24+
...
25+
```
26+
27+
## Customizing execution
28+
29+
By convention, most library components have an `execute()` method that defines the core runtime behavior of the component. This can be overridden by subclasses of the component to customize this behavior.
30+
31+
For example, we can create a subclass of the `SlingReplicationCollectioncomponent` that adds a debug log message during execution:
32+
33+
<CodeExample path="docs_beta_snippets/docs_beta_snippets/guides/components/custom-subclass/debug-mode.py" language="python" />
34+
35+
## Adding component-level templating scope
36+
37+
By default, the scopes available for use in the template are:
38+
39+
- `env`: A function that allows you to access environment variables.
40+
- `automation_condition`: A scope allowing you to access all static constructors of the `AutomationCondition` class.
41+
42+
However, it can be useful to add additional scope options to your component type. For example, you may have a custom automation condition that you'd like to use in your component.
43+
44+
To do so, you can define a function that returns an `AutomationCondition` and define a `get_additional_scope` method on your subclass:
45+
46+
<CodeExample path="docs_beta_snippets/docs_beta_snippets/guides/components/custom-subclass/custom-scope.py" language="python" />
47+
48+
This can then be used in your `component.yaml` file:
49+
50+
```yaml
51+
component_type: .custom_subclass
52+
53+
params:
54+
...
55+
transforms:
56+
- attributes:
57+
automation_condition: "{{ custom_cron('@daily') }}"
58+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: 'Building pipelines with components'
3+
sidebar_position: 20
4+
---
5+
6+
:::info
7+
8+
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.
9+
10+
:::
11+
12+
import DocCardList from '@theme/DocCardList';
13+
14+
<DocCardList />
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
title: 'Troubleshooting Components'
3+
sidebar_position: 500
4+
unlisted: true
5+
---
6+
7+
:::info
8+
9+
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.
10+
11+
:::

0 commit comments

Comments
 (0)