https://prefect.io logo
Title
j

Jen Evenson

02/16/2023, 9:50 PM
@Marvin How do we get
prefect.context.flow_name
in Prefect 2?
m

Marvin

02/16/2023, 9:51 PM
Ah, I see, the
prefect.context.flow_name
in Prefect 2 becomes set when the flow is run and can be customized using the
flow_run_name
keyword argument, as per the RELEASE-NOTES.md at main · PrefectHQ/prefect · GitHub. You can provide a custom static name as the
task_run_name
and a custom but a fixed name as the
flow_run_name
in the flow. For example, you can use the following code:
from datetime import datetime
from prefect import flow, task
@task(task_run_name="custom-static-name")
def my_task(name):
print(f"hi {name}")
@flow(flow_run_name="custom-but-fixed-name")
def my_flow(name: str, date: datetime):
return my_task(name)
my_flow()
I hope this answer is satisfactory. Have a great day!
j

Jen Evenson

02/16/2023, 9:53 PM
@Marvin How do I retrieve the flow_name in Prefect 2?
m

Marvin

02/16/2023, 9:53 PM
Ah, so you want to know how to retrieve the flow_name in Prefect 2? According to the [Flows page](https://docs.prefect.io/ui/flows/) in the Prefect UI, you can simply select the name of the flow in order to retrieve it. Alternatively, you can also use the
prefect deployment inspect
command, as per the instructions on the [Flow deployments page](https://docs.prefect.io/tutorials/deployments/) of the Prefect 2 documentation. Additionally, if you are using Prefect Cloud, you can use the [Prefect Cloud Quickstart guide](https://docs.prefect.io/ui/cloud-quickstart/) to quickly get up and running with Prefect. I hope this is sufficient information to answer your question.
n

Nate

02/16/2023, 11:52 PM
Hi @Jen Evenson it looks like marvin didn't give a good answer here (bad @Marvin) here is how you can get the flow name from the context in prefect 2
In [6]: from prefect import flow

In [7]: from prefect.context import get_run_context

In [8]: @flow(log_prints=True)
   ...: def prints_flow_name():
   ...:     print(f"Hello from {get_run_context().flow.name}!")
   ...:

In [9]: prints_flow_name()
17:49:34.257 | INFO    | prefect.engine - Created flow run 'marigold-goldfish' for flow 'prints-flow-name'
17:49:35.258 | INFO    | Flow run 'marigold-goldfish' - Hello from 'prints-flow-name'!
17:49:35.411 | INFO    | Flow run 'marigold-goldfish' - Finished in state Completed()
and in case you meant
flow_run
name, you get that the same way with
{get_run_context().flow_run.name}
:thank-you: 1
m

Marvin

02/16/2023, 11:52 PM
I apologize for not giving a satisfactory response and I appreciate your feedback on the matter. It appears that if you want to access the
flow_name
in Prefect 2, you can use the
get_run_context().flow.name
command to get the flow name and
get_run_context().flow_run.name
to retrieve the flow_run name. You can see an example of how to use this code in the code block you provided. I hope this is the information you are looking for.