Last active
November 6, 2025 16:03
-
-
Save zzstoatzz/3eb9333625d465cb74381c5116be7aee to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| """ | |
| # how to run this example on your local machine with uv (https://docs.astral.sh/uv/) | |
| # !!! be sure to run against `prefect server start` or Prefect Cloud for a UI | |
| GIST_URL="https://gist.githubusercontent.com/zzstoatzz/3eb9333625d465cb74381c5116be7aee/raw/556d8e1886e5725f383ff6e673681a42eec3f9cf/tour_of_artifacts.py" | |
| curl -L $GIST_URL -o /tmp/artifacts.py | |
| pip install -U uv | |
| uv run --python 3.12 --with 'prefect>=3.0.0rc19' /tmp/artifacts.py | |
| """ | |
| import webbrowser | |
| from typing import Any | |
| from pydantic import BaseModel | |
| from prefect import flow, task | |
| from prefect.artifacts import create_markdown_artifact | |
| from prefect.settings import PREFECT_UI_URL | |
| class Data[T](BaseModel): | |
| value: T | |
| @task(task_run_name="saving an {key} artifact") | |
| def save( | |
| key: str, | |
| model: Data, | |
| description: str | None = None, | |
| add_a_duck: bool = True, | |
| show_me_now: bool = True, | |
| ): | |
| description = description or "" | |
| if add_a_duck: | |
| description += "\n\n" | |
| uuid = create_markdown_artifact( | |
| key=key, | |
| markdown=f"```json\n{model.model_dump_json(indent=2)}\n```", | |
| description=description or "No description provided.", | |
| ) | |
| url = f"{PREFECT_UI_URL.value()}/artifacts/artifact/{uuid}" | |
| print("\n\n==========\n" f"\nSaved artifact {key!r}:\n\n{url}\n" "\n==========\n") | |
| if show_me_now: | |
| webbrowser.open(url) | |
| @task(task_run_name="some_transform on a {data.__class__.__name__}") | |
| def some_transform(data: Any) -> Any: | |
| # do something crazy | |
| return data.value | |
| @flow(log_prints=True) | |
| def main[T](data: Data[T]) -> Any: | |
| save("input", data, description="what our flow received", add_a_duck=False) | |
| results: list[T] = some_transform.map([data] * 10).result() | |
| save("output", Data[list[T]](value=results), description="what our flow produced") | |
| return results | |
| if __name__ == "__main__": | |
| print(f"finished: {main(Data[int](value=42))}") |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can render markdown or table artifacts from runtime python variables (like a
pd.DataFrameorBaseModel) and then store it under akey: str(inputandoutputin this example)