Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor code fixes for dagster uni dbt course #24757

Merged
merged 2 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ To demonstrate, we’re going to intentionally make a bug in our dbt model code,
from {{ source('raw_taxis', 'zones') }}
)
select
{{ dbt_utils.generate_surrogate_key(['zone_id']) }} as zone_id,
zone_id,
zone as zone_name,
borough,
zone_name like '%Airport' as is_airport,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ Open the `assets/dbt.py` file and do the following:

4. Now, let’s fill in the `get_asset_key` method with our own logic for defining asset keys.

1. There are two properties that we’ll want from `dbt_resource_props`: the `type` (ex., model, source, seed, snapshot) and the `name`, such as `trips` or `stg_trips`. Access both of those properties from the `dbt_resource_props` argument and store them in their own respective variables (`type` and `name`):
1. There are two properties that we’ll want from `dbt_resource_props`: the `resource_type` (ex., model, source, seed, snapshot) and the `name`, such as `trips` or `stg_trips`. Access both of those properties from the `dbt_resource_props` argument and store them in their own respective variables (`type` and `name`):

```python
def get_asset_key(self, dbt_resource_props):
type = dbt_resource_props["resource_type"]
resource_type = dbt_resource_props["resource_type"]
name = dbt_resource_props["name"]
```

Expand All @@ -63,7 +63,7 @@ Open the `assets/dbt.py` file and do the following:

```python
def get_asset_key(self, dbt_resource_props):
type = dbt_resource_props["resource_type"]
resource_type = dbt_resource_props["resource_type"]
name = dbt_resource_props["name"]

return AssetKey(f"taxi_{name}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,53 +107,52 @@ Now we’re ready to create the asset!
At this point, the `airport_trips` asset should look like this:

```python
@asset(
deps=["location_metrics"],
)
def airport_trips(database: DuckDBResource) -> MaterializeResult:
"""
A chart of where trips from the airport go
"""

query = """
select
zone,
destination_borough,
trips
from location_metrics
where from_airport
"""

with database.get_connection() as conn:
airport_trips = conn.execute(query).fetch_df()

fig = px.bar(
airport_trips,
x="zone",
y="trips",
color="destination_borough",
barmode="relative",
labels={
"zone": "Zone",
"trips": "Number of Trips",
"destination_borough": "Destination Borough"
},
)

pio.write_image(fig, constants.AIRPORT_TRIPS_FILE_PATH)

with open(constants.AIRPORT_TRIPS_FILE_PATH, 'rb') as file:
image_data = file.read()
@asset(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the difference in this block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the code block has a leading space which is a little tedious to copy over.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ty ty

deps=["location_metrics"],
)
def airport_trips(database: DuckDBResource) -> MaterializeResult:
"""
A chart of where trips from the airport go
"""

query = """
select
zone,
destination_borough,
trips
from location_metrics
where from_airport
"""
with database.get_connection() as conn:
airport_trips = conn.execute(query).fetch_df()

fig = px.bar(
airport_trips,
x="zone",
y="trips",
color="destination_borough",
barmode="relative",
labels={
"zone": "Zone",
"trips": "Number of Trips",
"destination_borough": "Destination Borough"
},
)

pio.write_image(fig, constants.AIRPORT_TRIPS_FILE_PATH)

with open(constants.AIRPORT_TRIPS_FILE_PATH, 'rb') as file:
image_data = file.read()

# Convert the image data to base64
base64_data = base64.b64encode(image_data).decode('utf-8')
md_content = f"![Image](data:image/jpeg;base64,{base64_data})"

return MaterializeResult(
metadata={
"preview": MetadataValue.md(md_content)
}
)
base64_data = base64.b64encode(image_data).decode('utf-8')
md_content = f"![Image](data:image/jpeg;base64,{base64_data})"

return MaterializeResult(
metadata={
"preview": MetadataValue.md(md_content)
}
)
```

5. Reload your code location to see the new `airport_trips` asset within the `metrics` group. Notice how the asset graph links the dependency between the `location_metrics` dbt asset and the new `airport_trips` chart asset:
Expand Down