https://prefect.io logo
Title
j

jack

02/19/2021, 4:52 PM
How do I specify a descriptive name for each card in the schematic, even though I'm using the same function name (do_something in this case) for each task?
n

nicholas

02/19/2021, 4:58 PM
Hi @jack - try this:
from prefect import Task

class DoSomething(Task):
  def run():
    # task logic
    return  

task_1 = DoSomething(name="task name")()
s

Samuel Hinton

02/19/2021, 4:58 PM
I gave something similar to this, a whole bunch of APIs to poll, and I want the name of the download function to reflect the API. If you pass a parameter into the task you can annotate it and the name will update (after hte flow runs) using the task name mapped in the annotation. Code example from my flow, with the important part being the annotation task_run_name matching the signature arg. Note that even though this changes the task names in the list view when the flow runs, it does not change the names in the schematic diagram. @nicholas - this may not be intentional/wanted behaviour EDIT: Jeremiah’s solution is better, it changes the schematic name too, Im swpping over to use that.
j

Jeremiah

02/19/2021, 4:59 PM
If you’re using the functional API you can pass
task_args
when you call the function:
@task
def my_fn():
    ...

with Flow(...):
    x = my_fn(..., task_args=dict(name="a custom name"))
    y = my_fn(..., task_args=dict(name="another custom name"))
🙌 2
j

jack

02/19/2021, 5:57 PM
We are using the functional API. @Jeremiah your example worked perfectly
👍 1
s

Samuel Hinton

02/22/2021, 12:38 PM
@Zanie - it would be good to update the docs here (https://docs.prefect.io/core/idioms/task-run-names.html) to reflect the way Jeremiah is doing it. Its not covered in the docs and it has significant advantages over the provided solutions
z

Zanie

02/22/2021, 3:58 PM
Hmm I'm a bit confused. Jeremiah's example is the creating a copy of the
my_fn
task with a new static name. The docs you've linked to takes a single task and changes its runtime name based on a dynamic value. Perhaps it is worth noting this separate pattern in that same doc though.
s

Samuel Hinton

02/22/2021, 4:31 PM
Yeah, theyre a bit different, just might be good to note the distinction at the top, because searching for “prefect task change name” or “dynamic name” takes you straight to that doc page and I didnt know there was a better way of doing what I wanted 🙂