Abuzar Shakikh
01/18/2024, 11:42 AMMarvin
01/18/2024, 11:42 AMpython
from prefect import task, Flow, Parameter
@task
def say_hello(name):
print(f"Hello, {name}!")
@task
def add(x, y):
return x + y
@task
def display_result(result):
print(f"The result is: {result}")
with Flow("My Parameterized Flow") as flow:
# Define Parameters
name = Parameter("name", default="Marvin")
num1 = Parameter("num1", default=1)
num2 = Parameter("num2", default=2)
num3 = Parameter("num3", default=3)
num4 = Parameter("num4", default=4)
# Use Parameters in Tasks
greeting = say_hello(name)
sum1 = add(num1, num2)
sum2 = add(num3, num4)
final_result = add(sum1, sum2)
display_result(final_result)
# Now you can run the flow with custom parameters:
if __name__ == "__main__":
flow.run(parameters={"name": "Arthur", "num1": 10, "num2": 20, "num3": 30, "num4": 40})
In this simple example, the flow My Parameterized Flow
uses five parameters: name
, num1
, num2
, num3
, and num4
. The say_hello
task uses the name
parameter to greet someone, and the add
task is used to sum various pairs of parameters. We then display the result using the display_result
task. When running the flow, you can pass different parameter values to customize the flow's execution.
Please note that the example above might not match exactly with your use case, and the specific implementation might vary depending on the complexity of your workflow and the Prefect version you are using. The linked documentation should also provide additional context and guidance on using and overriding parameters in Prefect flows.Abuzar Shakikh
01/18/2024, 11:52 AMMarvin
01/18/2024, 11:53 AMflow
decorator to create a flow rather than a flow context manager. You would also use the @task
decorator for tasks. Here's how you might approach a similar example in Prefect 2.x, assuming it's not going to fall over for some unknown reason:
python
from prefect import flow, task
@task
def say_hello(name):
print(f"Hello, {name}!")
@task
def add(x, y):
return x + y
@task
def display_result(result):
print(f"The result is: {result}")
@flow
def my_parameterized_flow(name: str = "Marvin", num1: int = 1, num2: int = 2, num3: int = 3, num4: int = 4):
greeting = say_hello(name)
sum1 = add(num1, num2)
sum2 = add(num3, num4)
final_result = add(sum1, sum2)
display_result(final_result)
# Now you can run the flow with custom parameters:
if __name__ == "__main__":
my_parameterized_flow(name="Arthur", num1=10, num2=20, num3=30, num4=40)
When you run this script, it will execute the flow with the provided parameter values, much like in Prefect 1.x, but with the updated syntax and features that Prefect 2.x offers.
For the most comprehensive and up-to-date information on how to properly use parameters within Prefect 2.x, you should refer to the Prefect documentation or source code, as my current capacity for understanding this updated framework is somewhat....limited, to say the least.Abuzar Shakikh
01/18/2024, 11:56 AMMarvin
01/18/2024, 11:56 AMpython
from prefect import flow, task
@task
def some_task(param):
# Your task implementation
pass
@flow
def my_flow(required_param):
# Your flow logic using the required_param
some_task(required_param)
if __name__ == "__main__":
my_flow(required_param="This parameter is required")
In the my_flow
function, required_param
is a required parameter because it doesn't have a default value. When calling my_flow
, you must provide a value for required_param
, or else Python will raise a TypeError
indicating that a required positional argument is missing.
The Prefect 2.0 documentation does not explicitly state how to create required parameters because it follows the standard Python function parameter rules. If you want to ensure the parameter is also checked by the Prefect runtime, you might consider including runtime checks or using Python's type hints to guide users of your flows.