Hi! Quick question about "default" parameters. I ...
# ask-community
j
Hi! Quick question about "default" parameters. I have a flow that takes a date as a parameter. It runs on a schedule and should default to whatever is
date.today()
when the flow runs. Currently I solve this by defaulting the date-parameter to
None
and replacing it with
date.today()
inside the flow if this is the case. This works fine, but I would really like a way to populate the parameters tab in the UI with the actual parameters used in the flow. Is there a better way to do dynamic parameters like this?
n
hi @Jahn Thomas Fidje - i think what you settled on is the simplest way to do it which sounds good to me can you explain more what you're envisioning here?
This works fine, but I would really like a way to populate the parameters tab in the UI with the actual parameters used in the flow.
i.e. what's your flow signature and whats the ideal UX for running your flow from the UI?
j
@Nate Thanks for chiming in šŸ™‚ I realize that my wording was a bit confusing. Below are two screenshots from the UI. The first is from a scheduled run, the second is from a manual run: The first one failed, and therefore I had to re-run the flow manually with parameters set. I would really like it if it showed me in the first picture what value
end_date
was set to during the scheduled run
n
hmm - yeah that panel will only show what is actually provided to the flow run, so if you just want to see that value, there's a couple ways to go about this • log the value in your flow • use
run_deployment
to programmatically schedule your runs instead of using the schedule, where you can provide the
end_date
explicitly and it will show up
j
I see šŸ™‚ Are there better ways to do dynamic parameters? I tried using a Pydantic model with a
default_factory
defined with a
Field()
, but that did not work as expected either šŸ˜ž
n
> do dynamic parameters hmm sorry, this could mean a lot of things
Copy code
from datetime import timedelta
from typing import Literal

from pydantic import BaseModel, Field
from pydantic_extra_types.pendulum_dt import DateTime

from prefect import flow


class NiceDateParams(BaseModel):
    area: Literal["sand", "snow", "ice"]
    currency: Literal["usd", "eur", "jpy"]
    start_date: DateTime = Field(
        default_factory=lambda: DateTime.now() - timedelta(days=1)
    )
    end_date: DateTime = Field(default_factory=lambda: DateTime.now())


@flow(log_prints=True)
def f(params: NiceDateParams):
    print(params)


if __name__ == "__main__":
    f.serve()
pydantic is the right tool to get a nice form, but that doesn't really address the fact that you're not passing the value you want to see into the API (ie as logs or as parameters via
run_deployment
or manual run)
j
Thank you for your help šŸ™‚