My parameter value shows the same date every day - how can I set Parameter value dynamically?

My simplified flow example:

from datetime import date
from prefect import task, Flow, Parameter


@task(log_stdout=True)
def do_something(x):
    print(x)


with Flow("example") as flow:
    start_date = Parameter("start_date", default=date.today().strftime("%Y-%m-%d"))
    do_something(start_date)

Why do I get the same date every day instead of this date being evaluated dynamically every time I run my flow?

The default Parameter value is evaluated during registration time, so the start_date in your example will be fixed to the registration date.

To make it dynamic, you would need to move it into a task and pass this date as data dependency. This way, you can defer the execution of date.today() so that it’s evaluated at runtime rather than at registration time.

Here is the same example but slightly modified:

from datetime import date
from prefect import task, Flow, Parameter

@task(log_stdout=True)
def do_something(x):
    print(x)

@task
def get_date(day):
    if day is None:
        return date.today().strftime("%Y-%m-%d")
    else:
        return day

with Flow("example") as flow:
    start_date = Parameter("start_date", default=None)
    new_date = get_date(start_date)
    do_something(new_date)
1 Like