Omar Khudeira
05/03/2024, 1:30 PMMarvin
05/03/2024, 1:30 PMflow_run_name
parameter of the @flow
decorator. Below is an example snippet showing how to do this:
python
import datetime
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
# creates a flow run called 'my-flow-with-marvin-and-100'
my_flow(name="marvin")
You can read more detailed instructions and see additional examples in the Prefect documentation:
- Custom Flow naming using `flow_run_name` parameter
Remember, the generate_flow_run_name
function can access the flow's parameters and use them to construct a unique flow run name.Omar Khudeira
05/03/2024, 1:32 PMMarvin
05/03/2024, 1:33 PMflow_run_name
parameter customizes the name of the flow run, not the name of the flow itself. To modify the name of a subflow dynamically based on the parameters passed into it, you should apply a similar approach but consider that each subflow will still maintain their defined flow name while allowing their flow run names to be customized dynamically.
Here's how you can define a subflow that gets called with a dynamic flow run name based on the parameters:
python
from prefect import flow
def generate_subflow_run_name(param):
return f"subflow-with-{param}"
@flow(name="subflow")
def my_subflow(param: str, flow_run_name: str = None):
# Subflow's logic goes here
...
@flow(name="mainflow")
def my_mainflow(param: str):
# Main flow's logic goes here
# Call subflow with a dynamic run name
my_subflow(param, flow_run_name=generate_subflow_run_name(param))
# Call main flow with parameter
my_mainflow("specific-parameter")
In the code above, the my_subflow
is the subflow whose run name we want to customize. The my_mainflow
is the main flow that calls the subflow. Notice how the my_subflow
is called with a custom flow_run_name
argument, which is generated by the generate_subflow_run_name
function based on the main flow's parameters.
Please remember that the name
argument in the @flow(name="...")
decorator defines the name of the flow itself, which is not dynamic. The dynamic part here is the flow_run_name
that is created when the flow is executed.
For more details on subflows and flow runs, check the Prefect subflow documentation.Nate
05/03/2024, 2:43 PMwith_options
Marvin
05/03/2024, 2:43 PMwith_options
function in Prefect. Let me run a more targeted search to help you with this. Please hold on for a moment.
The search for with_options
in the context of Prefect did not yield any direct results about a with_options
method. However, for customizing subflows and their naming based on inputs, we can use the flow_run_name
parameter when defining the flow to dynamically set the flow run name. You can see previously referenced examples of doing this in the Prefect documentation.
Given the search didn't return particulars of with_options
, if it's a feature or method newly introduced in Prefect 2.x, I would recommend referring to the latest Prefect release notes, the Prefect Discourse community, or the Prefect Slack community for up-to-date information and examples on using this specific method.
If you have more context on the with_options
usage or if it relates to a specific Prefect component, please provide additional details, and I can assist further.Nate
05/03/2024, 2:44 PMnamed_flow = original_flow.with_options(name="whatever")
named_flow(...)