lucian
07/14/2025, 8:41 AMMarvin
07/14/2025, 8:41 AMMarvin
07/14/2025, 8:42 AMpython
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:
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:
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:
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:
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.lucian
07/14/2025, 8:44 AMMarvin
07/14/2025, 8:44 AMMarvin
07/14/2025, 8:45 AMpython
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:
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:
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:
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.lucian
07/14/2025, 8:51 AMMarvin
07/14/2025, 8:51 AMMarvin
07/14/2025, 8:52 AMcreate_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:
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:
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:
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:
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.