You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[daggy-u] update usage of context.add_output_metadata with MaterializeResult (#19575)
## Summary & Motivation
With Dagster University being upgraded to v1.6, we should update our
usage of `context.add_output_metadata` to returning a
`MaterializeResult` with the `metadata` defined as a parameter.
The corresponding changes to `project-dagster-university` can be found
here:
dagster-io/project-dagster-university#8
## How I Tested These Changes
---------
Co-authored-by: Erin Cochran <erin.k.cochran@gmail.com>
6. Finally, we'll return a `MaterializeResult` object with the metadata specified as a parameter:
117
114
118
-
context.add_output_metadata({
119
-
"preview": MetadataValue.md(md_content)
120
-
})
115
+
```python
116
+
return MaterializeResult(
117
+
metadata={
118
+
"preview": MetadataValue.md(md_content)
119
+
}
120
+
)
121
121
```
122
122
123
123
Let’s break down what’s happening here:
@@ -126,13 +126,13 @@ In Lesson 9, you created the `adhoc_request` asset. During materialization, the
126
126
2.`base64.b64encode` encodes the image’s binary data (`image_data`) into base64 format.
127
127
3. Next, the encoded image data is converted to a UTF-8 encoded string using the `decode` function.
128
128
4. Next, a variable named `md_content` is created. The value of this variable is a Markdown-formatted string containing a JPEG image, where the base64 representation of the image is inserted.
129
-
5.Using `context.add_output_metadata`, the image is passed in as metadata. The metadata will have a `preview` label in the Dagster UI.
129
+
5.To include the metadata on the asset, we returned a `MaterializeResult` instance with the image passed in as metadata. The metadata will have a `preview` label in the Dagster UI.
130
130
6. Using `MetadataValue.md`, the `md_content` is typed as Markdown. This ensures Dagster will correctly render the chart.
131
131
132
132
At this point, the code for the `adhoc_request` asset should look like this:
133
133
134
134
```python
135
-
from dagster import Config, asset, MetadataValue, get_dagster_logger
135
+
from dagster import Config, asset, MaterializeResult, MetadataValue, get_dagster_logger
136
136
from dagster_duckdb import DuckDBResource
137
137
138
138
import plotly.express as px
@@ -148,7 +148,7 @@ class AdhocRequestConfig(Config):
4. Next, we’ll add the metadata with the specified type:
59
59
60
60
```python
61
-
context.add_output_metadata({'Number of records':MetadataValue.int(num_rows)})
61
+
return MaterializeResult(
62
+
metadata={
63
+
'Number of records': MetadataValue.int(num_rows)
64
+
}
65
+
)
62
66
```
63
67
64
68
Let’s break down what’s happening here:
65
69
66
-
-`context.add_output_metadata`accepts a `dict`, where the key is the label or name of the metadata being passed and the value is the data itself. In this case, the key is `Number of records`. The value in this example is everything after `Number of records`.
70
+
-The `metadata` parameter accepts a `dict`, where the key is the label or name of the metadata and the value is the data itself. In this case, the key is `Number of records`. The value in this example is everything after `Number of records`.
67
71
- Using `MetadataValue.int`, the value of the `num_rows` variable is typed as an integer. This tells Dagster to render the data as an integer.
68
72
69
73
At this point, the asset should look like this:
@@ -73,25 +77,31 @@ Let’s add metadata to the `taxi_trips_file` asset to demonstrate further. This
73
77
from dagster import asset, MetadataValue
74
78
75
79
@asset(
76
-
partitions_def=monthly_partition,
77
-
group_name="raw_files",
80
+
partitions_def=monthly_partition,
81
+
group_name="raw_files",
78
82
)
79
83
deftaxi_trips_file(context):
80
-
"""
81
-
The raw parquet files for the taxi trips dataset. Sourced from the NYC Open Data portal.
82
-
"""
84
+
"""
85
+
The raw parquet files for the taxi trips dataset. Sourced from the NYC Open Data portal.
0 commit comments