https://prefect.io logo
a

Alex Beatson

08/30/2023, 6:06 AM
@Marvin, how can I add tags to a flow?
m

Marvin

08/30/2023, 6:07 AM
Ah, the joy of tags. They're like little labels you stick on things to make them easier to find. It's a bit like marking your territory, but less primitive. To add tags to a flow in Prefect, you can use the
tags
parameter when defining your flow. Here's an example:
Copy code
python
from prefect import Flow

with Flow('My Flow', tags=['tag1', 'tag2']) as flow:
    pass
In this example, 'tag1' and 'tag2' are the tags added to 'My Flow'. You can then use these tags to filter your flows in the Prefect UI. You can also add tags to individual tasks within a flow, like so:
Copy code
python
from prefect import task, Flow

@task(name="hello-task", tags=["test"])
def my_task():
    print("Hello")

with Flow('My Flow') as flow:
    my_task()
In this case, the 'test' tag is added to 'hello-task'. You can use this tag to filter task runs in the UI and via the Prefect REST API, or to set concurrency limits on task runs. Isn't that just thrilling?
a

Alex Beatson

08/30/2023, 6:22 AM
@Marvin, how can i add tags to a flow run when the flow was defined using the @flow decorator?
m

Marvin

08/30/2023, 6:23 AM
Ah, the @flow decorator. A delightful little piece of syntactic sugar that makes your code look cleaner. It's like a broom for your code, sweeping away the mess. When you're using the @flow decorator, you can't add tags directly to the flow run. However, you can add tags to the flow itself, and these tags will be applied to all runs of the flow. Here's how you'd do it:
Copy code
python
from prefect import Flow

@Flow(name='My Flow', tags=['tag1', 'tag2'])
def my_flow():
    pass
In this example, 'tag1' and 'tag2' are the tags added to 'My Flow'. These tags will be applied to all runs of 'My Flow'. If you want to add tags to a specific flow run, you'll need to do it when you create the flow run. Here's an example:
Copy code
python
flow_run = flow.run(tags=['tag1', 'tag2'])
In this case, 'tag1' and 'tag2' are added to the specific flow run. Isn't that just fascinating? I can hardly contain my excitement.