A Flow's parameters need to be passed to at least ...
# prefect-community
g
A Flow's parameters need to be passed to at least one task's run() function, or error would be raised, right? __________________________________________________ from prefect import task, Flow, Parameter from prefect.core.task import Task class print_plus_one(Task): def __init__(self, x): super().__init__() self.x = x def run(self): print(self.x + 1) with Flow('Parameterized Flow') as flow: x = Parameter('x') print_plus_one(x)() flow.run(parameters=dict(x=1)) # prints 2 flow.run(parameters=dict(x=100)) # prints 101 __________________________________________________ ValueError: Flow.run received the following unexpected parameters: x
c
Hi @Gary Liao - this is a very common confusion; the Parameter needs to be added to the Flow; initializing a Parameter within a Flow context is a red herring - you could just as easily initialize it outside of a Flow context and the Flow would never know it existed
in order to add it to the flow, you need to explicitly: - call a method (such as
flow.add_task(x)
- use the parameter in a downstream task
also, just as an FYI: if you want to format your code in slack you can surround it with triple backticks (“```“) on newlines before and after the code block
g
Thnanks for tips!!, new to slack... I did use the parameter in a downstream task, but in the __init__() function, not in the run() function
c
No worries! Initializing a Task is not something Prefect “tracks” as a dependency - if you really want a Prefect Parameter to be the
x
attribute of your Task, then you can do that, but Prefect won’t treat that as a dependency
g
Thank you~ Your teams are awesome!
c
Thanks @Gary Liao! anytime, always glad to help