<@ULVA73B9P> I want to set up custom flow_run_name...
# ask-marvin
z
@Marvin I want to set up custom flow_run_name. How could I pass the function?
m
To set up a custom
flow_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:
Copy code
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.
z
@Marvin how to access variables from inside flow function?
m
To access variables inside a flow function in Prefect 3.x, you simply define them as parameters in your flow function. These parameters can then be accessed and used within the function body. Here is a basic example:
Copy code
python
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.
z
@Marvin I still use prefect v2.19
m
In Prefect 2.19, accessing variables inside a flow function is straightforward. You define them as parameters in your flow function, and they can be accessed within the function body. Here's an example:
Copy code
python
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.
z
@Marvin how can I use variables created in flow function for custome flow_run_name?
m
In Prefect 2.19, you can use variables created in a flow function for a custom
flow_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:
Copy code
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.
z
@Marvin I get NameError: name 'flow_run' is not defined
m
The import statement
from 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:
Copy code
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.