William Jamir
05/28/2024, 9:35 AMMarvin
05/28/2024, 9:36 AMruntime
context to access flow run parameters and use Prefect's client to add tags to a flow run.
Here's how you can approach it:
1. Use the prefect.runtime.context
to access the flow run parameters.
2. Interact with the Prefect API to update the flow run with the appropriate tags.
Here's an example of how you could implement this:
### Code Example
python
from prefect import flow, task
from prefect.runtime import flow_run
from prefect.client import get_client
@task
def add_tags(id: str):
client = get_client()
current_flow_run_id = flow_run.id
current_tags = flow_run.tags
new_tags = current_tags | {id} # Add new tag to existing tags
# Update flow run tags
client.update_flow_run(current_flow_run_id, tags=list(new_tags))
@flow
def my_flow(id: str):
add_tags(id)
# Your flow logic here
# Example usage
my_flow("unique-flow-id")
In this example:
1. Retrieve the Flow Run ID: This is necessary to identify which flow run to update.
2. Retrieve Existing Tags: Use flow_run.tags
to get the current tags.
3. Update Tags: Add the new ID to the existing set of tags.
4. Update the Flow Run: Use the Prefect client’s update_flow_run
function to apply the new tags.
References:
- Prefect Runtime Context
- Prefect Client - Flow Run Management
This should allow you to dynamically set a tag to your flow during runtime based on the provided ID parameter.