Skip to content

Commit 2d6e95c

Browse files
authored
Dagster Essentials dg (#96)
1 parent 30e4b06 commit 2d6e95c

File tree

108 files changed

+1841
-1671
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+1841
-1671
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
FROM mcr.microsoft.com/devcontainers/python:0-3.11-bullseye
22
ENV PYTHONUNBUFFERED 1
33

4-
COPY --from=ghcr.io/astral-sh/uv:0.4.7 /uv /bin/uv
4+
COPY --from=ghcr.io/astral-sh/uv:0.6.10 /uv /bin/uv
55

66
COPY dagster_university/dagster_essentials/pyproject.toml .
77
RUN uv pip install -r pyproject.toml --system

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ celerybeat.pid
130130
*.sage.py
131131

132132
# Environments
133-
.env
134133
.venv
135134
env/
136135
venv/

course/pages/dagster-essentials/extra-credit/3-definition-metadata-asset-groups.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ For example:
3434

3535
```python
3636
import dagster as dg
37-
from dagster_essentials.assets import metrics
37+
from dagster_essentials.defs.assets import metrics
3838

3939
metric_assets = dg.load_assets_from_modules(
4040
modules=[metrics],

course/pages/dagster-essentials/extra-credit/5-materialization-metadata.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ Let’s add metadata to the `taxi_trips_file` asset to demonstrate further. This
2626
```python
2727
import dagster as dg
2828
import requests
29-
from dagster_essentials.assets import constants
30-
from dagster_essentials.partitions import monthly_partition
29+
from dagster_essentials.defs.assets import constants
30+
from dagster_essentials.defs.partitions import monthly_partition
3131

3232
@dg.asset(
3333
partitions_def=monthly_partition,

course/pages/dagster-essentials/extra-credit/6-coding-practice-metadata-taxi-zones-file.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ The metadata you built should look similar to the code contained in the **View a
1717
```python {% obfuscated="true" %}
1818
import dagster as dg
1919

20-
2120
@dg.asset(
2221
group_name="raw_files",
2322
)

course/pages/dagster-essentials/extra-credit/7-asset-metadata-as-markdown.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ In Lesson 9, you created the `adhoc_request` asset. During materialization, the
2222

2323
import matplotlib.pyplot as plt
2424

25-
from dagster_essentials.assets import constants
25+
from dagster_essentials.defs.assets import constants
2626

2727
class AdhocRequestConfig(dg.Config):
2828
filename: str
@@ -138,7 +138,7 @@ from dagster_duckdb import DuckDBResource
138138
import matplotlib.pyplot as plt
139139
import base64
140140

141-
from dagster_essentials.assets import constants
141+
from dagster_essentials.defs.assets import constants
142142

143143
class AdhocRequestConfig(dg.Config):
144144
filename: str

course/pages/dagster-essentials/lesson-2/1-set-up-local.md

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ lesson: '2'
66

77
# Set up local
88

9+
This will set up Dagster for you local machine. If you would prefer to do this course in Github Codespaces, please follow [that guide](/dagster-essentials/lesson-2/2-set-up-codespace).
10+
911
- **To install git.** Refer to the [Git documentation](https://github.com/git-guides/install-git) if you don’t have this installed.
1012
- **To have Python installed.** Dagster supports Python 3.9 - 3.12.
1113
- **To install a package manager**. To manage the python packages, we recommend [`uv`]((https://docs.astral.sh/uv/)) which Dagster uses internally.
@@ -34,46 +36,37 @@ After cloning the Dagster University project, you’ll want to navigate to speci
3436
cd dagster_university/dagster_essentials
3537
```
3638

37-
## Install the dependencies
39+
## Install uv and dg
3840

39-
**uv**
41+
Now we want to install `dg`. This is the command line interface that makes it easy to interact with Dagster. Throughout the course we will use `dg` to scaffold our project and streamline the development process.
4042

41-
To install the python dependencies with [uv](https://docs.astral.sh/uv/).
43+
In order to best use `dg` we will need the Python package manager [`uv`](https://docs.astral.sh/uv/). `uv` will allow us to install `dg` globally and more easily build our virtual environments.
4244

45+
If you do not have `uv` instead already, you can do so with:
4346
```bash
44-
uv sync
47+
brew install uv
4548
```
4649

47-
This will create a virtual environment that you can now use.
48-
50+
Now you can use `uv` to install `dg` globally:
4951
```bash
50-
source .venv/bin/activate
52+
uv tool install dagster-dg
5153
```
5254

53-
**pip**
54-
55-
Create the virtual environment.
55+
## Install the dependencies
5656

57+
With `uv` and `dg` set, we can create the virtual environment specific to this course. All of the dependencies are maintained in the `pyproject.toml` (you will not need to edit anything in that project for this course). To create the virtual environment, run:
5758
```bash
58-
python3 -m venv .venv
59+
uv sync
5960
```
6061

61-
Enter the virtual environment.
62+
This will create a virtual environment and install all the necessary dependencies. To activate this virtual environment:
6263

6364
```bash
6465
source .venv/bin/activate
6566
```
6667

67-
Install the packages.
68-
69-
```bash
70-
pip install -e ".[dev]"
71-
```
72-
73-
## Create .env file
74-
75-
You will want to make a copy of the example file `.env.example` which will be used later on.
68+
To ensure everything is working you can launch the Dagster UI.
7669

7770
```bash
78-
cp .env.example .env
71+
dg dev
7972
```

course/pages/dagster-essentials/lesson-2/2-set-up-codespace.md

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,13 @@ cd dagster_university/dagster_essentials
4141
To ensure everything is working you can launch the Dagster UI.
4242

4343
```bash
44-
dagster dev
44+
dg dev
4545
```
4646

4747
After Dagster starts running you will be prompted to open the Dagster UI within your browser. Click "Open in Browser".
4848

4949
![Codespace Launch](/images/shared/codespaces/codespaces-launch.png)
5050

51-
## Create .env file
52-
53-
You will want to make a copy of the example file `.env.example` which will be used later on.
54-
55-
```bash
56-
cp .env.example .env
57-
```
58-
5951
## Stopping your Github Codespace
6052

6153
Be sure to stop your Codespace when you are not using it. Github provides personal accounts [120 cores hours per month](https://docs.github.com/en/billing/managing-billing-for-your-products/managing-billing-for-github-codespaces/about-billing-for-github-codespaces#monthly-included-storage-and-core-hours-for-personal-accounts).

course/pages/dagster-essentials/lesson-2/3-project-files.md

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,40 @@ Let’s talk a bit about the files in the Dagster Essentials course. The `dagste
1010

1111
```bash
1212
dagster_university/dagster_essentials
13-
├── Makefile
14-
├── README.md
13+
.
1514
├── dagster_cloud.yaml
1615
├── dagster_essentials
17-
│   ├── __init__.py
18-
│   ├── assets
19-
│   │   ├── __init__.py
20-
│   │   ├── constants.py
21-
│   │   ├── metrics.py
22-
│   │   └── trips.py
2316
│   ├── completed
24-
│   │   └── ...
17+
│   │   ├── lesson_3
18+
│   │   ├── lesson_4
19+
│   │   ├── lesson_5
20+
│   │   ├── lesson_6
21+
│   │   ├── lesson_7
22+
│   │   ├── lesson_8
23+
│   │   └── lesson_9
2524
│   ├── definitions.py
26-
│   ├── jobs.py
27-
│   ├── partitions.py
28-
│   ├── resources.py
29-
│   ├── schedules.py
30-
│   └── sensors.py
25+
│   └── defs
26+
│   ├── assets
27+
│   │   ├── __init__.py
28+
│   │   ├── constants.py
29+
│   │   ├── metrics.py
30+
│   │   └── trips.py
31+
│   ├── jobs.py
32+
│   ├── partitions.py
33+
│   ├── resources.py
34+
│   ├── schedules.py
35+
│   └── sensors.py
3136
├── dagster_essentials_tests
32-
│   └── ...
3337
├── data
3438
│   ├── outputs
3539
│   ├── raw
3640
│   ├── requests
3741
│   └── staging
38-
├── env.example
42+
├── .env
43+
├── Makefile
3944
├── pyproject.toml
4045
├── pytest.ini
46+
├── README.md
4147
└── uv.lock
4248
```
4349

@@ -90,7 +96,7 @@ The columns in the following table are as follows:
9096

9197
---
9298

93-
- `.env.example`
99+
- `.env`
94100
- Python
95101
- A text file containing pre-configured environment variables. We’ll talk more about this file in Lesson 6, when we cover connecting to external services.
96102

course/pages/dagster-essentials/lesson-3/2-defining-your-first-asset.md

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,58 @@ In this course, you’ll use data from [NYC OpenData](https://opendata.cityofnew
1010

1111
Your first asset, which you’ll name `taxi_trips_file`, will retrieve the yellow taxi trip data for March 2023 and save it to a location on your local machine.
1212

13-
1. First, navigate to and open the `assets/trips.py` file in your Dagster project. This is where you’ll write your asset code.
13+
## Project structure
1414

15-
2. At the top of the `trips.py` file, add the following imports:
15+
Before we write our first asset, let's talk a little about project structures in Dagster. In the previous lesson we mentioned `dg` and how it offers a lot of helpful functionality to quickstart our project. We can use commands like `dg scaffold project` to initialize a `uv` virtual environment for us but we already took care of that when we set up the course in lesson 2.
16+
17+
However we can use `dg` to scaffold a file for our first asset. Run the following command to create the file that will contain our first asset.
18+
19+
```bash
20+
dg scaffold dagster.asset assets/trips.py
21+
```
22+
23+
This will add a `trips.py` file to our Dagster project.
24+
25+
```
26+
.
27+
└── dagster_essentials
28+
   └── defs
29+
   └── assets
30+
   ├── __init__.py
31+
   ├── constants.py # already present
32+
   └── trips.py
33+
```
34+
35+
**Note:** If we were starting a project from scratch we would use [`dg init`](https://docs.dagster.io/guides/labs/dg/scaffolding-a-project) which will handle the creation of our virtual environment. However since we already have a virtual environment defined, we can skip this step.
36+
37+
38+
Using `dg` to scaffold your project will ensure that files are placed in the correct location. We can ensure that everything is configured correctly also using `dg`.
39+
40+
```bash
41+
> dg check defs
42+
No definitions are defined for this project.
43+
```
44+
45+
This command will confirm that our project is laid out correctly. Next we can use `dg` to list all the objects in our project.
46+
47+
```bash
48+
> dg list defs
49+
No definitions are defined for this project.
50+
```
51+
52+
This makes sense because even though we created the file that will contain our asset, we have not yet included the code.
53+
54+
## Defining your first asset
55+
56+
With the files set we can now add our first asset.
57+
58+
1. Navigate and open the newly created `defs/assets/trips.py` file in your Dagster project. This is where you’ll write your asset code.
59+
60+
2. Within the `trips.py` file, remove the generated code from the scaffolding and replace it with the following imports:
1661

1762
```python
1863
import requests
19-
from dagster_essentials.assets import constants
64+
from dagster_essentials.defs.assets import constants
2065
```
2166

2267
3. Below the imports, let's define a function that takes no inputs and returns nothing (type-annoted with `None`). Add the following code to create a function to do this named `taxi_trips_file`:
@@ -47,7 +92,7 @@ Your first asset, which you’ll name `taxi_trips_file`, will retrieve the yello
4792

4893
```python
4994
import requests
50-
from dagster_essentials.assets import constants
95+
from dagster_essentials.defs.assets import constants
5196
import dagster as dg
5297

5398
@dg.asset
@@ -66,4 +111,22 @@ Your first asset, which you’ll name `taxi_trips_file`, will retrieve the yello
66111

67112
That’s it - you’ve created your first Dagster asset! Using the `@dg.asset` decorator, you can easily turn any existing Python function into a Dagster asset.
68113

114+
We can use `dg` again to check our asset:
115+
116+
```bash
117+
> dg check defs
118+
No definitions are defined for this project.
119+
```
120+
121+
And now when we run `dg list defs` our asset will register:
122+
```bash
123+
> dg list defs
124+
┏━━━━━━━━━━━━━━━━━┳━━━━━━━━━┳━━━━━━┳━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
125+
┃ Key ┃ Group ┃ Deps ┃ Kinds ┃ Description ┃
126+
┡━━━━━━━━━━━━━━━━━╇━━━━━━━━━╇━━━━━━╇━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
127+
│ taxi_trips_file │ default │ │ │ The raw parquet files for the taxi trips dataset. Sourced │
128+
│ │ │ │ │ from the NYC Open Data portal. │
129+
└─────────────────┴─────────┴──────┴───────┴──────────────────────────────────────────────────────────────┘
130+
```
131+
69132
**Questions about the `-> None` bit?** That's a Python feature called **type annotation**. In this case, it's saying that the function returns nothing. You can learn more about type annotations in the [Python documentation](https://docs.python.org/3/library/typing.html). We highly recommend using type annotations in your code to make it easier to read and understand.

course/pages/dagster-essentials/lesson-3/3-asset-materialization.md

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,43 @@ With the basics of materialization out of the way, let’s move on to actually m
4343

4444
---
4545

46+
## Materializing assets using dg
47+
48+
We can use `dg` to execute and launch assets. In order to execute an asset, use the `dg launch` command while providing the asset you wish to execute.
49+
50+
```bash
51+
dg launch --assets taxi_trips_file
52+
```
53+
54+
You will then see the logs as Dagster executes our asset:
55+
```
56+
2025-04-09 13:43:50 -0500 - dagster - DEBUG - __ASSET_JOB - 5b735e96-e6d4-4f37-80f4-641c22ef896d - 15920 - RUN_START - Started execution of run for "__ASSET_JOB".
57+
2025-04-09 13:43:50 -0500 - dagster - DEBUG - __ASSET_JOB - 5b735e96-e6d4-4f37-80f4-641c22ef896d - 15920 - ENGINE_EVENT - Executing steps using multiprocess executor: parent process (pid: 15920)
58+
2025-04-09 13:43:50 -0500 - dagster - DEBUG - __ASSET_JOB - 5b735e96-e6d4-4f37-80f4-641c22ef896d - 15920 - taxi_trips_file - STEP_WORKER_STARTING - Launching subprocess for "taxi_trips_file".
59+
2025-04-09 13:43:51 -0500 - dagster - DEBUG - __ASSET_JOB - 5b735e96-e6d4-4f37-80f4-641c22ef896d - 15925 - taxi_trips_file - STEP_WORKER_STARTED - Executing step "taxi_trips_file" in subprocess.
60+
2025-04-09 13:43:51 -0500 - dagster - DEBUG - __ASSET_JOB - 5b735e96-e6d4-4f37-80f4-641c22ef896d - 15925 - taxi_trips_file - RESOURCE_INIT_STARTED - Starting initialization of resources [io_manager].
61+
2025-04-09 13:43:51 -0500 - dagster - DEBUG - __ASSET_JOB - 5b735e96-e6d4-4f37-80f4-641c22ef896d - 15925 - taxi_trips_file - RESOURCE_INIT_SUCCESS - Finished initialization of resources [io_manager].
62+
2025-04-09 13:43:51 -0500 - dagster - DEBUG - __ASSET_JOB - 5b735e96-e6d4-4f37-80f4-641c22ef896d - 15925 - LOGS_CAPTURED - Started capturing logs in process (pid: 15925).
63+
2025-04-09 13:43:51 -0500 - dagster - DEBUG - __ASSET_JOB - 5b735e96-e6d4-4f37-80f4-641c22ef896d - 15925 - taxi_trips_file - STEP_START - Started execution of step "taxi_trips_file".
64+
2025-04-09 13:43:59 -0500 - dagster - DEBUG - __ASSET_JOB - 5b735e96-e6d4-4f37-80f4-641c22ef896d - 15925 - taxi_trips_file - STEP_OUTPUT - Yielded output "result" of type "Nothing". (Type check passed).
65+
2025-04-09 13:43:59 -0500 - dagster - DEBUG - __ASSET_JOB - 5b735e96-e6d4-4f37-80f4-641c22ef896d - 15925 - taxi_trips_file - ASSET_MATERIALIZATION - Materialized value taxi_trips_file.
66+
2025-04-09 13:43:59 -0500 - dagster - DEBUG - __ASSET_JOB - 5b735e96-e6d4-4f37-80f4-641c22ef896d - 15925 - taxi_trips_file - STEP_SUCCESS - Finished execution of step "taxi_trips_file" in 7.71s.
67+
2025-04-09 13:43:59 -0500 - dagster - DEBUG - __ASSET_JOB - 5b735e96-e6d4-4f37-80f4-641c22ef896d - 15920 - ENGINE_EVENT - Multiprocess executor: parent process exiting after 8.35s (pid: 15920)
68+
2025-04-09 13:43:59 -0500 - dagster - DEBUG - __ASSET_JOB - 5b735e96-e6d4-4f37-80f4-641c22ef896d - 15920 - RUN_SUCCESS - Finished execution of run for "__ASSET_JOB".
69+
```
70+
71+
You do not need to follow every line of the output, for now the most important thing is last line that the execution was successful.
72+
73+
```
74+
2025-04-09 13:43:59 -0500 - dagster - DEBUG - __ASSET_JOB - 5b735e96-e6d4-4f37-80f4-641c22ef896d - 15920 - RUN_SUCCESS - Finished execution of run for "__ASSET_JOB".
75+
```
76+
4677
## Materializing assets using the Dagster UI
4778

4879
If you don’t still have the Dagster UI running from Lesson 2, use the command line to run the following command in the root of your Dagster project (the top-level `dagster-university/dagster_essentials` directory):
4980

5081
```bash
51-
dagster dev
82+
dg dev
5283
```
5384

5485
Navigate to [`localhost:3000`](http://localhost:3000/) in your browser. The page should look like the following - if it doesn’t, click **Overview** in the top navigation bar:
@@ -138,3 +169,9 @@ The page is empty for now, but it’ll look more interesting shortly. Let’s ge
138169
{% /table %}
139170

140171
That’s it! You’ve successfully materialized your first asset! 🎉
172+
173+
## When to use `dg launch` vs `dg dev`
174+
175+
You now know two different ways to launch your asset. You may be wondering which one to use. Luckily there is no wrong answer. You might find it easier to execute an asset with `dg launch` when you need to quickly test something out while you may want to use `dg dev` as your Dagster project becomes more sophisticated.
176+
177+
For the majority of this course we will use `dg dev` to showcase more of the features of Dagster.

course/pages/dagster-essentials/lesson-3/5-troubleshooting-failed-runs.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ lesson: '3'
88

99
Now that you’re familiar with how assets are materialized and where to find details about their execution, let’s focus on how to troubleshoot issues. To demonstrate how to troubleshoot, you’ll intentionally cause the `taxi_trips_file` asset to fail.
1010

11-
In the `assets/trips.py` file, comment out the `from dagster_essentials.assets import constants` line so it looks like this:
11+
In the `assets/trips.py` file, comment out the `from dagster_essentials.defs.assets import constants` line so it looks like this:
1212

1313
```python
1414
import requests
15-
# from dagster_essentials.assets import constants # <---- Import commented out here
15+
# from dagster_essentials.defs.assets import constants # <---- Import commented out here
1616
import dagster as dg
1717

1818
@dg.asset
@@ -74,7 +74,7 @@ To home in on what went wrong, let’s take a closer look at the logs. We’ll u
7474

7575
At this point, you can use the stacktrace to identify and fix the cause of the error. In this case, it’s because we didn’t import `constants`, leaving it undefined.
7676

77-
To fix this, uncomment the `from dagster_essentials.assets import constants` line in the `trips.py` file and save it.
77+
To fix this, uncomment the `from dagster_essentials.defs.assets import constants` line in the `trips.py` file and save it.
7878

7979
In the Dagster UI, click **OK** to close the popover window from the run logs.
8080

0 commit comments

Comments
 (0)