<https://docs.prefect.io/core/idioms/task-run-name...
# prefect-community
d
https://docs.prefect.io/core/idioms/task-run-names.html I am having trouble refactoring the example in the documentation above for my needs. I am trying to name tasks based on the mapped children in Prefect Cloud, but I am not mapping a list. I am mapping a dictionary, where the desired
task_run_name
would be a value based on a specific key for that mapped dict index. Anyone have experience naming tasks based on a mapped dictionary?
k
Hi @David Michael Carter, you can check out templating for that
d
hi Kevin, this directs me to the link I already posted, which I wasn’t able to use for my needs
k
I re-read the first post. How do you map over a dictionary?
This won’t work right?
Copy code
from prefect import Flow, task

@task
def mytask(x):
    return x+1

with Flow("test") as flow:
    mytask.map({"a": 1, "b": 2})

flow.run()
d
sorry I was wrong. I am mapping a list, but each element of the list is a dictionary.
k
Ah ok I think we can make it work. Let me make an example
d
Copy code
task_run_name="{myDict['name']}"
results in key error for ‘name’… trying various iterations on that idea
k
I see, but you can use the callable name generator too
Copy code
from prefect import Flow, task

def generate_task_run_name(dict_val, **kwargs):
    return "something" + str(dict_val['a'])

@task(task_run_name=generate_task_run_name)
def mytask(dict_val):
    return dict_val["b"]+1

with Flow("test") as flow:
    mytask.map([{"a": 1, "b": 2}, {"a": 3, "b": 4}])

flow.register("general_assembly")
Note this will only work with a backend, not with flow.run
d
success!
k
Nice!
d
the callable name generator did the trick. Thank you