Is there an easy way to access the flow’s name (no...
# prefect-community
i
Is there an easy way to access the flow’s name (not the flow run name) from a task running inside the flow in Prefect 2?
1
n
hi @Ilya Galperin do you mean the name of the function that's decorated with @flow? or the name of a deployment from which the flow run was created?
i
Heya Nate - not the name of the function itself but specifically the name of the flow defined in the decorator i.e.
@flow(name="example_flow")
we would want to find the value
example_flow
n
you could do something like
Copy code
from prefect import flow, get_run_logger, task

@task
def log_flow_name():
    logger = get_run_logger()
    <http://logger.info|logger.info>(f"I'm task that makes up a flow `{test.__name__}` with name: {test.name}")

@flow(name="example_flow")
def test():
    log_flow_name()

if __name__ == "__main__":
    test()
Copy code
❯ python flow_name.py
14:35:56.653 | INFO    | prefect.engine - Created flow run 'tourmaline-shark' for flow 'example_flow'
14:35:57.304 | INFO    | Flow run 'tourmaline-shark' - Created task run 'log_flow_name-96b8f2ed-0' for task 'log_flow_name'
14:35:57.306 | INFO    | Flow run 'tourmaline-shark' - Executing 'log_flow_name-96b8f2ed-0' immediately...
14:35:57.469 | INFO    | Task run 'log_flow_name-96b8f2ed-0' - I'm task that makes up a flow test with name: example_flow
14:35:57.556 | INFO    | Task run 'log_flow_name-96b8f2ed-0' - Finished in state Completed()
14:35:57.651 | INFO    | Flow run 'tourmaline-shark' - Finished in state Completed('All states completed.')
i
Thanks Nate. This is definitely a workable solution but since our tasks live in a different script than our flow function, I was wondering if there was a way to access the flow name directly from the flow context? Otherwise, we have to pass the flow function’s name attribute as a parameter into the task (which admittedly isn’t a huge lift if not).
n
from what I could see, the flow's name attribute doesn't live in the task run context, but yes you could totally pass it in to the tasks that need it as a parameter
i
Gotcha, thanks for confirming!