Hey, I know you can set a task or flow run name us...
# ask-community
n
Hey, I know you can set a task or flow run name using a string variable. But is it possible to do within a dict? like this:
@task(task_run_name="reset-indy-config-{device['name']}")
or is it only possible to do
{device}
?
j
You can use an f-string:
Copy code
@task(task_run_name=f"reset-indy-config-{device['name']}")
n
ooh thank you was missing that!! appreciate it
n
just to note, an f-string is often not what you want here, because an f-string is rendered immediately (with whatever the value of
foo
is at function definition time) as opposed to a template string (without the f) which is rendered after the task is called, and therefore can be named according to the params it received
Copy code
In [1]: from prefect import task

In [2]: foo = dict(x=42)

In [3]: @task
   ...: def t(foo: dict): pass

In [4]: t.with_options(task_run_name=f"{foo['x']}")(dict(x=43))
15:48:32.451 | INFO    | Task run '42' - Created task run '42' for task 't'
15:48:32.460 | INFO    | Task run '42' - Finished in state Completed()

In [5]: t.with_options(task_run_name="{foo[x]}")(dict(x=43))
15:48:43.996 | INFO    | Task run '43' - Created task run '43' for task 't'
15:48:44.001 | INFO    | Task run '43' - Finished in state Completed()
n
the way i'm calling my tasks is immediately appending them to an array as i use .submit on them. so would it be t.with_options(blah).submit()?
n
you don't need to use
with_options
if you don't need to template it differently each time, I just did because I wanted to show the examples side by side with the same underlying task object if you're always going to template the same way, you'd be free to do this
Copy code
@task(task_run_name="reset-indy-config-{device[name]}")
n
oh wow it really was a matter of removing the quotes lol
thank you!!!
n
catjam