I’m trying to set a tag by following the example i...
# prefect-ui
k
I’m trying to set a tag by following the example in the Tags documentation:
Copy code
from prefect import flow, task
from prefect import tags

@task
def my_task():
    print("Hello, I'm a task")

@flow
def my_flow():
    with tags("test"):
        my_task()
When I run this, the
test
tag doesn’t get set.
1
If I set it in the main as seen below, the “in_main” tag does get set properly
Copy code
from prefect import flow, tags, task


@task
def my_task():
    print("Hello, I'm a task")


@flow
def my_flow():
    with tags("test"):
        my_task()


if __name__ == "__main__":
    with tags("in_main"):
        my_flow()
Both tests were run with both prefect 2.0.0 and 2.0.1.
a
let me update this Discourse topic, I think the syntax might have changed
you can set tags on tasks and deployments and on agents/work queues
cc @Kelby
k
Thanks, @Anna Geller. Given that, is there a way to dynamically set a tag based on a parameter passed into a flow?
a
why would you want to do that?
k
I have flows that I run with different parameters passed in. I want to be able to filter the UI based on one of those parameters.
I’m also considering renaming the flow run itself, but I haven’t looked into that process yet.
a
Renaming flow runs is not as needed in 2.0, given that each flow run is directly prefixed with the flow name in the UI To filter by parameter values, this is currently not a pattern the flow runs dashboard is optimized for but I can understand why this might be valuable. Adding tags based on parameter values seems like a hack so we might explore better ways to approach this problem let me add that to a backlog to explore the options here @Marvin open "Explore the possibility to filter flow runs based on parameter values"
m
k
Thanks. I agree that using tags here is probably a hack, but it is nice that I can filter the UI by tag name. It’s also nice that the UI includes the tag name directly in the flow run results, making the flow run easier to find even when not using the tag filters. Looking forward to seeing what the team comes up with.
🙌 1
c
We have this same issue / requirement. Since we are using flows to orchestrate work on objects, it’d be nice to reference either the parameter (object_id) or to be able to rename the flows by object. Not sure if we are breaking something here but we have worked around this by executing a function at the beginning of each flow that checks the context for the flow run, grabs the flow id, and immediately calls the Orion API to rename the flow for us. The logging still contains the original adjective/noun pair but the UI shows the renamed flow run name so we can search our flows by object ID. We plan to do this only in cases where we need this type of tracking. Outside of that, we can leave it be. Gist and screenshot below. I attempted to use the Prefect Python API but I always get tripped up with the async calls, so we’re just doing it synchronously with the REST API. 🤪 https://gist.github.com/crscheid/10dbf291779667831b856fab9cc9745b
a
Wow, this is a cool hack!