Quickstart
Convert a Python script into your first Prefect workflow
Prefect makes it easy to deploy Python scripts, run them on a schedule, make them robust to failure, and observe them in a UI.
To do this, you need to perform the following tasks:
- Install Prefect
- Connect to a Prefect API (self-hosted or Prefect Cloud)
- Add decorators to the methods in the script
Let’s start with a Python script that fetches the number of stars for a list of GitHub repositories.
Install Prefect
To install Prefect with pip, run:
See Install Prefect for more details on installation.
Connect to a Prefect API
Connect to a Prefect API:
-
Start a local API server:
-
Open the Prefect dashboard in your browser at http://localhost:4200.
Convert your script to a Prefect workflow
Decorators are the easiest way to convert a Python script into a workflow.
- Add a
@flow
decorator to the script’s entrypoint. - Add
@task
decorators to any methods called by the flow.
This will create a flow and corresponding tasks. Tasks receive metadata about upstream dependencies and the state of those dependencies before they run. Prefect records these dependencies and states as it orchestrates these tasks.
The log_prints=True
argument provided to the @flow
decorator automatically converts any print
statements within the function to INFO
level logs.
Run your flow
You can run your Prefect flow just as you would a Python script:
The output in your terminal should look similar to this:
Prefect automatically tracks the state of the flow run and logs the output, which can be viewed directly in the terminal or in the UI.
Next steps
In this tutorial, you successfully converted a Python script to a deployable workflow tracked by Prefect.
Next, get this workflow off of your laptop and run it automatically on a schedule.
Need help? Book a meeting with a Prefect Product Advocate to get your questions answered.