<@ULVA73B9P> I want to give my task run a specific...
# ask-marvin
k
@Marvin I want to give my task run a specific name. Can I assign a variable inside my task and use that as the task run name? Or must the task run name always be passed on in the task parameters?
m
You can assign a specific name to your task run in Prefect 3.x using either a static string or a function that returns a string. This can be done by providing a
task_run_name
argument when defining your task. Python's standard string formatting syntax applies, allowing you to dynamically generate the name using variables or context information. Here's a basic example:
Copy code
python
from prefect import task

@task(name="My Task")
def my_task():
    return "Hello, world!"

# You can also use a function to set the task run name
@task(task_run_name=lambda: f"My Task Run: {prefect.runtime.task_run_id}")
def my_dynamic_task():
    return "Hello, dynamic world!"
In this example,
task_run_name
can be a static string or a lambda function that returns a string. You can access runtime information using
prefect.runtime
to make the name more dynamic. For more details, you can refer to the Prefect task writing documentation.