Zivile
03/07/2025, 8:15 AMMarvin
03/07/2025, 8:26 AMflow_run_name
in Prefect 3.x, you can specify this when defining your flow using the @flow
decorator. You can provide a string template with the flow's parameters as variables, or a function that returns a string. Here's a brief example:
python
from prefect import flow
@flow(flow_run_name="custom-run-{param}")
def my_flow(param: str):
print(f"Running with parameter: {param}")
# Or using a function
def generate_name():
return "dynamic-run-name"
@flow(flow_run_name=generate_name)
def another_flow():
print("Another flow running")
In this example, flow_run_name
is set to a template string in my_flow
, which uses the flow's parameter param
. Alternatively, another_flow
uses a function generate_name
to set the flow_run_name
.
For further details, you can refer to the Prefect flows documentation.Zivile
03/07/2025, 8:27 AMMarvin
03/07/2025, 8:28 AMpython
from prefect import flow
@flow
def my_flow(param1: int, param2: str):
# Accessing and using the variables inside the flow
result = param1 * 2
print(f"The result is {result} and the message is {param2}")
# Calling the flow
my_flow(10, "Hello, Prefect!")
In this example, param1
and param2
are parameters of the flow my_flow
. Inside the function, you can perform operations using these parameters just like any other Python function.
For more advanced configurations, such as overriding job variables on a deployment, you can refer to the Prefect documentation.Zivile
03/07/2025, 8:29 AMMarvin
03/07/2025, 8:29 AMpython
from prefect import flow
@flow
def my_flow(param1: int, param2: str):
# Use the variables inside the flow
result = param1 * 2
print(f"The result is {result} and the message is {param2}")
# Calling the flow
my_flow(10, "Hello, Prefect!")
In this example, param1
and param2
are parameters of the my_flow
function. You can use these parameters within the function like any other Python function.
For more advanced use cases, such as overriding job variables on a flow run, you can refer to the Prefect documentation on overriding job variables.Zivile
03/07/2025, 8:30 AMMarvin
03/07/2025, 8:31 AMflow_run_name
by defining a function that generates the name using those variables. You can then pass this function to the flow_run_name
parameter of the @flow
decorator. Here's an example:
python
import datetime
from prefect import flow
from prefect.runtime import flow_run
def generate_flow_run_name():
# Access flow information and parameters
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):
# Your flow logic here
pass
if __name__ == "__main__":
my_flow(name="marvin")
In this example, the generate_flow_run_name
function accesses the flow's name and parameters using prefect.runtime.flow_run
. It then constructs a custom flow run name using these values. When my_flow
is called, it will generate a flow run name like 'my-flow-with-marvin-and-100'
.
For more information, refer to the Prefect 2.x flow concepts documentation.Zivile
03/07/2025, 8:35 AMMarvin
03/07/2025, 8:35 AMfrom prefect.runtime import flow_run
is valid in Prefect 2.19. If you're encountering a NameError
, ensure that this import statement is included at the top of your script. Here's a quick checklist to resolve the issue:
1. Ensure that the import statement is placed before any usage of flow_run
.
2. Double-check that you are using Prefect 2.19 and that your environment is correctly set up to use this version.
3. Make sure there are no typos in the import statement or in the usage of flow_run
.
Here's a complete example for clarity:
python
import datetime
from prefect import flow
from prefect.runtime import flow_run
def generate_flow_run_name():
# Access flow information and parameters
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):
# Your flow logic here
pass
if __name__ == "__main__":
my_flow(name="marvin")
If everything is set up correctly and you're still facing issues, consider checking the environment where the script is being executed to ensure it has access to the Prefect library.