Hi, Is there a way to append a string to a flow ru...
# prefect-server
c
Hi, Is there a way to append a string to a flow run name?
k
You can use the RenameFlowRun task and get the current one with
prefect.context.get("flow_run_name")
c
prefect.context.get("flow_run_name")
can run only inside a task, right? So I need to create a task X and inside of it return the flow name and after in the with Flow section to pass it to the RenameFlowRun task.
k
Yes and no. Yes it runs in a task, but you can use it in state handlers so you can have the rename run before the Flow begins. Code below
Copy code
from prefect import Flow, task
from prefect.tasks.prefect import RenameFlowRun

def rename_handler(obj, new_state, old_state):
    if new_state.is_running():
        RenameFlowRun().run(flow_run_name="new_name")
    return

@task
def first_task():
    return 1

with Flow("test-flow", state_handlers=[rename_handler]) as flow:
    first_task()