<@ULVA73B9P> how do I print the flow Id inside of ...
# ask-marvin
a
@Marvin how do I print the flow Id inside of the actual flow?
m
To print the flow ID inside an actual flow in Prefect, I'll need to confirm which version of Prefect you're using, as methods can vary. Could you please specify if you're using Prefect 2.x, 3.x, or another version? If you're unsure, please let me know!
a
@Marvin 3.X
m
To print the flow ID inside your flow in Prefect 3.x, you can use the
prefect.context
module. Here's how you can access the flow run context and print the flow ID:
Copy code
python
from prefect import flow
from prefect.context import FlowRunContext

@flow
def my_flow():
    # Access the flow run context
    flow_run_context = FlowRunContext.get()
    
    # Get the flow ID from the context
    flow_id = flow_run_context.flow_run.id
    
    # Print the flow ID
    print(f"The Flow ID is: {flow_id}")

if __name__ == "__main__":
    my_flow()
In this example, the
FlowRunContext.get()
method is used to retrieve the current flow run context, from which you can then access the
flow_run.id
to print the flow ID. For more details on using Prefect's runtime context, you can refer to the Prefect 3.x documentation on runtime context.