<@ULVA73B9P> is it possible to set dynamically the...
# ask-marvin
f
@Marvin is it possible to set dynamically the flow name over a function with Prefect 2.X ?
m
Yes, it is possible to set the flow name dynamically in Prefect 2.x using a function. You can define a function that returns a string to be used as the flow run name. Here's a brief example from the documentation:
Copy code
python
import datetime
from prefect import flow

def generate_flow_run_name():
    date = datetime.datetime.now(datetime.timezone.utc)
    return f"{date:%A}-is-a-nice-day"

@flow(flow_run_name=generate_flow_run_name)
def my_flow(name: str):
    pass

# This will create a flow run called 'Thursday-is-a-nice-day'
if __name__ == "__main__":
    my_flow(name="marvin")
Additionally, if you need to use information from the flow such as parameters, you can utilize the
prefect.runtime
module. Here's another example demonstrating this:
Copy code
python
from prefect import flow
from prefect.runtime import flow_run

def generate_flow_run_name():
    flow_name = flow_run.flow_name
    parameters = flow_run.parameters
    name = parameters["name"]
    limit = parameters["limit"]
    return f"{flow_name}-with-{name}-and-{limit}"

@flow(flow_run_name=generate_flow_run_name)
def my_flow(name: str, limit: int = 100):
    pass

# This creates a flow run called 'my-flow-with-marvin-and-100'
if __name__ == "__main__":
    my_flow(name="marvin")
You can find more information in the Prefect 2.x documentation here: Prefect 2.x Documentation Ah, naming things and cache invalidation, the two hard problems of computer science... and naming things dynamically at runtime totally counts! 😊
f
@Marvin I want to set the flow name, not the flow run name, from a flow parameter
m
In Prefect 2.x, setting the flow name directly from a flow parameter is not supported. The flow name is typically defined statically at the time of flow creation, and you can dynamically set the flow run name instead. The examples provided in the documentation focus on dynamically setting the flow run name, using parameters from the flow. If your need to dynamically adapt the flow name based on certain criteria is crucial, you might need to consider a workflow redesign or manage it externally in the system that triggers your flows. For more details, you can check out the Prefect 2.x documentation here: Prefect 2.x Documentation. Ah, the persistence of static flow names... it's one of those fixed points in the universe, like tax day. Let's just learn to live with it. 😊