-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
83 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 0 additions & 46 deletions
46
examples/docs_snippets/docs_snippets/getting-started/quickstart.py
This file was deleted.
Oops, something went wrong.
46 changes: 46 additions & 0 deletions
46
examples/docs_snippets/docs_snippets/getting-started/quickstart/assets.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import json | ||
|
||
import pandas as pd | ||
import requests | ||
from dagster_quickstart.configurations import HNStoriesConfig | ||
|
||
from dagster import ( | ||
MaterializeResult, | ||
MetadataValue, | ||
asset, | ||
) | ||
|
||
|
||
@asset | ||
def hackernews_top_story_ids(config: HNStoriesConfig): | ||
"""Get top stories from the HackerNews top stories endpoint.""" | ||
top_story_ids = requests.get( | ||
"https://hacker-news.firebaseio.com/v0/topstories.json" | ||
).json() | ||
|
||
with open(config.hn_top_story_ids_path, "w") as f: | ||
json.dump(top_story_ids[: config.top_stories_limit], f) | ||
|
||
|
||
@asset(deps=[hackernews_top_story_ids]) | ||
def hackernews_top_stories(config: HNStoriesConfig) -> MaterializeResult: | ||
"""Get items based on story ids from the HackerNews items endpoint.""" | ||
with open(config.hn_top_story_ids_path, "r") as f: | ||
hackernews_top_story_ids = json.load(f) | ||
|
||
results = [] | ||
for item_id in hackernews_top_story_ids: | ||
item = requests.get( | ||
f"https://hacker-news.firebaseio.com/v0/item/{item_id}.json" | ||
).json() | ||
results.append(item) | ||
|
||
df = pd.DataFrame(results) | ||
df.to_csv(config.hn_top_stories_path) | ||
|
||
return MaterializeResult( | ||
metadata={ | ||
"num_records": len(df), | ||
"preview": MetadataValue.md(str(df[["title", "by", "url"]].to_markdown())), | ||
} | ||
) |