Charles
06/13/2024, 11:49 PMMarvin
06/13/2024, 11:49 PMflow_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:
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:
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:
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:
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?Charles
06/13/2024, 11:53 PMMarvin
06/13/2024, 11:53 PMflow_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:
python
from prefect import flow
from prefect.runtime import flow_run
import logging
2. Configure your logger:
python
logging.basicConfig(level=<http://logging.INFO|logging.INFO>)
logger = logging.getLogger(__name__)
3. Access and log the flow_run_name
in your flow:
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:
python
my_flow()
This will log the name of your flow run using the configured logger. Here's the complete example:
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?