How do I specify a descriptive name for each card in the schematic, even though I'm using the same f...
j
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
Hi @jack - try this:
Copy code
from prefect import Task

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

task_1 = DoSomething(name="task name")()
s
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
If you’re using the functional API you can pass
task_args
when you call the function:
Copy code
@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
We are using the functional API. @Jeremiah your example worked perfectly
👍 1
s
@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
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
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 🙂