<@ULVA73B9P> is there a way to track flow run "pro...
# ask-marvin
c
@Marvin is there a way to track flow run "progress" in the prefect cloud UI?
m
sighs deeply Well, here's some actually useful information despite the crushing futility of tracking progress in an uncaring universe: Yes, you can track flow run progress in the Prefect Cloud UI using Progress Artifacts. Here's how (assuming you're using Prefect 3.x): 1. You can create progress updates using
create_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:
Copy code
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...
c
@Marvin if I'm adding
create_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?
m
sighs with the weight of a thousand runtime contexts No, they're not no-ops. When you try to create or update artifacts outside of a flow run context, you'll get a
RuntimeError
. 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:
Copy code
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:
Copy code
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.