<@ULVA73B9P> how to create dynamic create_flow_run...
# ask-marvin
l
@Marvin how to create dynamic create_flow_run names with date templating
m
thought for 51.6 seconds
In Prefect 3.x, you can create dynamic flow run names using the templating system. Here's how to do it: 1. Using a Basic Template:
Copy code
python
from prefect import flow
from datetime import datetime

@flow(
    name="my-flow-{date}",
    flow_run_name="my-flow-{date}"
)
def my_flow():
    pass
2. For More Complex Naming:
Copy code
python
from prefect import flow
from datetime import datetime

@flow
def my_flow(name_template: str = "my-flow-{date}"):
    flow_run = get_run_context().flow_run
    date_str = datetime.utcnow().strftime("%Y-%m-%d")
    flow_run.name = name_template.format(date=date_str)
3. Using Parameters in Deployment:
Copy code
python
from prefect import flow
from prefect.deployments import Deployment
from datetime import datetime

@flow
def my_flow():
    pass

deployment = Deployment.build_from_flow(
    flow=my_flow,
    name="my-deployment",
    flow_run_name="my-flow-{date}"
)
When the flow runs, the
{date}
placeholder will be automatically replaced with the current date. You can customize the date format using standard Python datetime formatting:
Copy code
python
@flow(
    flow_run_name="my-flow-{date:%Y-%m-%d-%H-%M}"  # Will result in format like "my-flow-2023-11-15-14-30"
)
def my_flow():
    pass
You can also include other dynamic elements in your naming template:
Copy code
python
@flow(
    flow_run_name="my-flow-{date:%Y-%m-%d}-{params.customer_id}"
)
def my_flow(customer_id: str):
    pass
The templating system will automatically substitute these values at runtime. This gives you flexibility in organizing and identifying your flow runs based on temporal and contextual information. For more details about templating utilities, you can refer to the Prefect templating documentation.
l
@Marvin when i start a flow via rest using create_flow_run
m
thought for 35.6 seconds
When creating a flow run via the REST API, you can set a dynamic name by including it in your API request. Here are a few ways to do this: 1. Using Python with the REST API:
Copy code
python
import requests
from datetime import datetime

# Format your desired run name
run_name = f"my-flow-{datetime.now().strftime('%Y-%m-%d-%H-%M')}"

# Create the flow run
response = <http://requests.post|requests.post>(
    "<http://your-prefect-server/api/flow_runs/>",
    json={
        "name": run_name,
        "deployment_id": "your-deployment-id"
    }
)
2. Using
curl
from the command line:
Copy code
bash
# Using date command in bash
curl -X POST "<http://your-prefect-server/api/flow_runs/>" \
    -H "Content-Type: application/json" \
    -d '{
        "name": "my-flow-'"$(date +%Y-%m-%d-%H-%M)"'",
        "deployment_id": "your-deployment-id"
    }'
3. Using the Prefect Python client:
Copy code
python
from prefect.client import get_client
from datetime import datetime

async def create_run():
    async with get_client() as client:
        flow_run = await client.create_flow_run(
            deployment_id="your-deployment-id",
            name=f"my-flow-{datetime.now().strftime('%Y-%m-%d-%H-%M')}"
        )
The name field in the request can include any string value you want, and you can incorporate: - Timestamps - Sequential numbers - Custom identifiers - Parameter values - Environment variables For example, a more complex naming pattern:
Copy code
python
import requests
from datetime import datetime
import os

run_name = (
    f"my-flow-"
    f"{os.getenv('ENVIRONMENT', 'dev')}-"
    f"{datetime.now().strftime('%Y-%m-%d-%H-%M')}-"
    f"batch-{your_batch_id}"
)

response = <http://requests.post|requests.post>(
    "<http://your-prefect-server/api/flow_runs/>",
    json={
        "name": run_name,
        "deployment_id": "your-deployment-id"
    }
)
You can find more details about the create flow run API endpoint in the REST API documentation.
l
@Marvin using {{ template }}
m
thought for 84.5 seconds
When using
create_flow_run
, you can use Jinja-style templating with double curly braces to create dynamic flow run names. Here's how to do it: 1. Basic date templating:
Copy code
python
from prefect.client import get_client

async def create_run():
    async with get_client() as client:
        flow_run = await client.create_flow_run(
            deployment_id="your-deployment-id",
            name="my-flow-{{ now.strftime('%Y-%m-%d-%H-%M') }}"
        )
2. Using parameters with templating:
Copy code
python
async def create_run():
    async with get_client() as client:
        flow_run = await client.create_flow_run(
            deployment_id="your-deployment-id",
            name="process-data-{{ params.customer_id }}-{{ now.strftime('%Y-%m-%d') }}",
            parameters={"customer_id": "12345"}
        )
Available template variables include: -
{{ now }}
- current datetime object -
{{ params.parameter_name }}
- access to flow parameters -
{{ flow_name }}
- name of the flow -
{{ deployment_name }}
- name of the deployment If you're using the REST API directly:
Copy code
python
import requests

response = <http://requests.post|requests.post>(
    "<http://your-prefect-server/api/flow_runs/>",
    json={
        "name": "my-flow-{{ now.strftime('%Y-%m-%d') }}-{{ params.batch_id }}",
        "deployment_id": "your-deployment-id",
        "parameters": {
            "batch_id": "BATCH001"
        }
    }
)
You can also combine multiple template variables:
Copy code
python
name_template = (
    "{{ flow_name }}-"
    "{{ deployment_name }}-"
    "{{ now.strftime('%Y%m%d') }}-"
    "{{ params.environment }}"
)
The templating will be evaluated when the flow run is created, substituting the variables with their actual values at runtime. Remember that the template variables must be available in the context where the flow run is created. For example, if you reference a parameter in the template, make sure to provide that parameter when creating the flow run.