docs/v3/examples/hello-world.mdx
{/*
This page is automatically generated via the generate_example_pages.py script. Any changes to this page will be overwritten.
*/}
<a href="https://github.com/PrefectHQ/prefect/blob/main/examples/hello_world.py" target="_blank">View on GitHub</a>
Welcome to your first Prefect flow. In under a minute you will:
Pro tip: change anything in this file and re-run it. Prefect hot-loads your new logic in seconds, no image builds, ever.
We start by importing the essential flow decorator from Prefect.
from prefect import flow, tags
Prefect takes your Python functions and transforms them into flows with enhanced capabilities.
Let's write a simple function that takes a name parameter and prints a greeting.
To make this function work with Prefect, we just wrap it in the @flow decorator.
@flow(log_prints=True)
def hello(name: str = "Marvin") -> None:
"""Log a friendly greeting."""
print(f"Hello, {name}!")
Now let's see different ways we can call that flow:
if __name__ == "__main__":
# run the flow with default parameters
with tags(
"test"
): # This is a tag that we can use to filter the flow runs in the UI
hello() # Logs: "Hello, Marvin!"
# run the flow with a different input
hello("Marvin") # Logs: "Hello, Marvin!"
# run the flow multiple times for different people
crew = ["Zaphod", "Trillian", "Ford"]
for name in crew:
hello(name)
When we decorated our function with @flow, the function was transformed into a Prefect flow. Each time we called it:
In short, we took a regular function and enhanced it with observability and tracking capabilities.
This simple example demonstrates Prefect's core value proposition: taking regular Python code and enhancing it with production-grade orchestration capabilities. Let's explore why this matters for real-world data workflows.
For instance, change the greeting message in the hello function to a different message and run the flow again.
You'll see your changes immediately reflected in the logs.
Add more names to the crew list or create a larger data set to process. Prefect will handle each execution and track every input and output.
The hello function is a simple example, but in its place imagine something that matters to you, like:
Prefect lets you orchestrate these operations effortlessly with automatic observability, error handling, and retries.
Remember that Prefect makes it easy to:
The @flow decorator is your gateway to enterprise-grade orchestration - no complex configuration needed!
For more information about the orchestration concepts demonstrated in this example, see the Prefect documentation.