<@ULVA73B9P> How to use tags dynamically for flows...
# ask-marvin
s
@Marvin How to use tags dynamically for flows in Prefect 3?
m
To use tags dynamically for flows in Prefect 3, you can utilize the
tags
context manager from the
prefect
module. Here's a concise example:
Copy code
python
from prefect import flow, tags
from prefect.events.schemas import DeploymentTrigger

TAG_NAME = "foobar"

@flow
def upstream():
    pass

@flow
def downstream():
    pass

if __name__ == "__main__":
    with tags(TAG_NAME):
        upstream()
        upstream()
        upstream()
    
    downstream.serve(
        name="downstream-of-tagged-upstream",
        triggers=[
            DeploymentTrigger(
                expect={"prefect.flow-run.Completed"},
                match_related=[
                    {
                        "prefect.resource.role": "tag",
                        "prefect.resource.id": f"prefect.tag.{TAG_NAME}",
                    },
                    {
                        "prefect.resource.role": "flow",
                        "prefect.resource.name": "upstream",
                    }
                ],
                threshold=3,
            )
        ]
    )
In this example, the
tags
context manager is used to apply a dynamic tag (
TAG_NAME
) to multiple runs of the
upstream
flow. The
downstream
flow is configured to trigger based on the completion of these tagged
upstream
runs. For more details, you can view the full example here.
s
@Marvin How to apply dynamic tags to task?
m
To apply dynamic tags to tasks in Prefect 3, you can use the
@task
decorator with the
tags
parameter. Here's a simple example:
Copy code
python
from prefect import task, flow

@task(tags=["dynamic-tag"])
def my_task():
    print("Task is running with dynamic tags!")

@flow
def my_flow():
    my_task()

my_flow()
In this example, the
my_task
function is decorated with
@task
and assigned a dynamic tag using the
tags
parameter. You can modify the
tags
list based on your requirements to apply different tags. For more details on the
@task
decorator and its parameters, you can refer to the Prefect documentation.