https://prefect.io logo
#best-practices
Title
# best-practices
s

Stefan

02/17/2023, 9:41 AM
Copy code
def main(date: DateModel = None) -> None:
How can I use the current date for flow_run_name? Unless doing data for another day, I don't call the flow with any arguments.
n

Nate

02/17/2023, 9:30 PM
hi @Stefan - if you're using prefect >= 2.7.12, you can use the new templating feature to name your flow runs like
Copy code
from datetime import datetime
from prefect import flow, task

@task(task_run_name="{name}")
def my_task(name):
  print(f"hi {name}")

@flow(flow_run_name="{name}-on-{date:%m-%d-%Y}")
def my_flow(name: str, date: datetime):
  return my_task(name)

my_flow("Nate", datetime.now())
s

Stefan

02/18/2023, 7:13 AM
But since I schedule flow runs in the UI I wouldn't have that last line calling "my_flow"
n

Nate

02/19/2023, 10:39 PM
ahh yes, sorry i missed that bit from your first message. im not sure theres a direct way to give custom names to flow runs created by the scheduler, but whats your use case? perhaps we can achieve it slightly differently
s

Stefan

02/23/2023, 9:16 AM
Hi again Nate! So I have a lot of scripts that run on the daily. Before prefect, we'd call them using argparse if we had to perform the job for previous dates. So for a day-to-day basis, I will not call my flow with any parameter, as it will do the job for -todays date-. Maybe there could be a config option to generate a flow run name from todays date in a specified string?
n

Nate

02/23/2023, 3:53 PM
hi @Stefan
Maybe there could be a config option to generate a flow run name from todays date in a specified string?
that sounds reasonable to me, let me raise it to the team
s

Stefan

02/24/2023, 11:43 AM
Thanks @Nate! I did an experiment: From the looks of it, the flow run is generated with "vagabond-impala" but quickly changes to my specified one with a dynamic date. This is my def:
Copy code
def main(year_month: str = (datetime.datetime.today() - relativedelta(months=1)).strftime('%Y%m')):
and so in the decorator:
Copy code
flow_run_name="azure2aisyrisk for {year_month}",
Update @Nate. This only works when calling a Flow from the command line. The parameters is successfully parsed as input to the flow_run_name. When calling from the UI, I get a KeyError as it is not defined.
2 Views