chris
12/27/2024, 7:35 PMMarvin
12/27/2024, 7:35 PMcreate_progress_artifact()
and update them with update_progress_artifact()
2. The progress is represented as a float between 0 and 100
3. To make the progress visible in the Artifacts page of the UI, you need to provide a key
parameter
Here's a basic example:
python
from prefect import flow
from prefect.artifacts import create_progress_artifact, update_progress_artifact
@flow
async def my_flow():
# Create initial progress
artifact_id = await create_progress_artifact(
progress=0,
key="my-progress", # Required for UI visibility
description="Starting process"
)
# Update progress later
await update_progress_artifact(
artifact_id=artifact_id,
progress=50,
description="Halfway there..."
)
The progress will be visible in:
1. The Artifacts tab of the specific flow run
2. The main Artifacts page in the UI (only if you provided a key
)
For more details, check out the Artifacts documentation.
mutters under breath Of course, tracking progress is just another way to measure our inevitable march towards entropy, but at least you'll have a nice UI to watch it happen...chris
12/27/2024, 7:49 PMcreate_progress_artifact
and update_progress_artifact
calls to my code, what happens when I'm running that code locally (i.e., not in a flow run) ? are they no-ops?Marvin
12/27/2024, 7:51 PMRuntimeError
. This is because artifacts are intrinsically tied to flow runs or task runs - they need that context to know where to store and associate the progress data.
Here's what would happen if you tried to use these functions outside a flow run:
python
# This will raise a RuntimeError
await create_progress_artifact(progress=50, key="my-progress")
If you want to make your code work both inside and outside flow runs, you'll need to wrap these calls in a check for the runtime context. mutters something about the futility of context-dependent code Here's a slightly less depressing way to handle it:
python
from prefect.context import get_run_context
from prefect.artifacts import create_progress_artifact
async def update_progress(progress: float):
try:
ctx = get_run_context()
await create_progress_artifact(
progress=progress,
key="my-progress"
)
except RuntimeError:
# We're not in a flow run, handle accordingly
print(f"Progress: {progress}%") # or whatever fallback you prefer
The relevant documentation can be found in the Artifacts section, though it doesn't explicitly mention this behavior. mumbles something about documentation being like life - never quite complete
Would you like me to check if there are any specific GitHub issues related to this behavior? It might reveal more edge cases or common patterns for handling this.