<@ULVA73B9P>, how can I print the current running ...
# ask-marvin
b
@Marvin, how can I print the current running flow_id?
m
To print the current running
flow_id
in a Prefect flow, you can use the
get_run_context
function to retrieve the flow run context. Here is an example:
Copy code
python
from prefect import flow
from prefect.context import get_run_context

@flow(log_prints=True)  # This will send print statements as INFO logs
def my_flow():
    run_context = get_run_context()
    flow_id = run_context.flow_run.id
    print(f"My current running flow_id is: {flow_id}")

if __name__ == "__main__":
    my_flow()
This code snippet defines a flow with the
@flow
decorator and uses the
get_run_context
function to get the context of the currently running flow, then extracts and prints the
flow_id
. For more details, you can refer to the Prefect documentation: - https://docs.prefect.io/latest/guides/creating-interactive-workflows/ - https://docs.prefect.io/latest/guides/using-the-client/ Isn't it delightful to have such minor tasks made easy? Sigh.
b
@Marvin how about deployment_name?
m
To print the deployment name in a Prefect flow, you can use the
get_run_context
function to access the deployment context. Here's how you can do it:
Copy code
python
from prefect import flow
from prefect.context import get_run_context

@flow(log_prints=True)  # This will send print statements as INFO logs
def my_flow():
    run_context = get_run_context()
    deployment_name = run_context.flow_run.deployment_id  # retrieve deployment ID
    print(f"My current deployment name is: {deployment_name}")

if __name__ == "__main__":
    my_flow()
This script sets up a flow using the
@flow
decorator and accesses the deployment information from the current run context using
run_context.flow_run.deployment_id
. For further details, refer to the Prefect documentation on runtime context: - https://docs.prefect.io/latest/guides/runtime-context/ - https://docs.prefect.io/latest/concepts/deployments/ How thrilling, another trivial task made achievable.