<@ULVA73B9P> how do i change the flow to use a cus...
# ask-marvin
c
@Marvin how do i change the flow to use a custom name I generated, instead of an animal prefix generated from prefect, ie malachite-nuthatch
m
You can customize the name of your flow and its runtime instances by setting the
flow_run_name
parameter when defining your flow. You can specify a static string or use templated references to dynamically generate the name. Here’s a summary of how you can do this: ### Static Flow Name Assign a custom name directly to the flow using the
@flow
decorator's
name
parameter:
Copy code
python
from prefect import flow

@flow(name="My Custom Flow")
def my_flow():
    pass
### Dynamic Flow Run Name Set the
flow_run_name
to a formatted string that can include references to the flow's parameters:
Copy code
python
import datetime
from prefect import flow

@flow(flow_run_name="{name}-on-{date:%A}")
def my_flow(name: str, date: datetime.datetime):
    pass

# Creates a flow run called 'marvin-on-Thursday'
my_flow(name="marvin", date=datetime.datetime.now(datetime.timezone.utc))
### Dynamic Name Using a Function For more complex naming, you can use a function to generate the flow run name:
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():
    pass

# Creates a flow run called 'Thursday-is-a-nice-day'
my_flow()
### Accessing Flow Runtime Information If you need more detailed information about the flow at runtime, you can access the
prefect.runtime
module:
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

# Creates a flow run called 'my-flow-with-marvin-and-100'
my_flow(name="marvin")
For more detailed documentation, please refer to the Prefect Flows documentation. Issues related to custom flow names and their behavior can also be explored in the GitHub issues. Do you need help with anything else?
c
@Marvin How do I access flow_run_name in the flow itself? ie I want this to be logged in the logger.info
m
To log the
flow_run_name
within your flow, you can use the
prefect.runtime
module, which provides runtime context information for your flow runs. Here's how you can do it: 1. Import the necessary modules:
Copy code
python
    from prefect import flow
    from prefect.runtime import flow_run
    import logging
2. Configure your logger:
Copy code
python
    logging.basicConfig(level=<http://logging.INFO|logging.INFO>)
    logger = logging.getLogger(__name__)
3. Access and log the
flow_run_name
in your flow:
Copy code
python
    @flow
    def my_flow():
        flow_run_name = flow_run.name
        <http://logger.info|logger.info>(f"Running flow with name: {flow_run_name}")
        
        # Your flow logic goes here
        pass
4. Call your flow:
Copy code
python
    my_flow()
This will log the name of your flow run using the configured logger. Here's the complete example:
Copy code
python
from prefect import flow
from prefect.runtime import flow_run
import logging

# Configure logger
logging.basicConfig(level=<http://logging.INFO|logging.INFO>)
logger = logging.getLogger(__name__)

@flow
def my_flow():
    flow_run_name = flow_run.name
    <http://logger.info|logger.info>(f"Running flow with name: {flow_run_name}")
    
    # Your flow logic goes here
    pass

# Call the flow
if __name__ == "__main__":
    my_flow()
### Relevant Documentation and Links 1. Prefect Flows Documentation 2. Prefect Runtime Context Documentation By using the
prefect.runtime.flow_run
module, you can access various runtime attributes, including the flow run name, and log them as needed. This should provide you with the information you need while running your flow. Do you need help with anything else?