Install Prefect
Install Prefect.If needed, see Install Prefect for other options.
Connect to Prefect’s API
Connect to the Prefect API, either Prefect Cloud hosted by us, or a local Prefect server instance:- Head to https://app.prefect.cloud/ and sign in or create a forever-free Prefect Cloud account.
-
Log in to Prefect Cloud from your development environment:
- Choose Log in with a web browser and click the Authorize button in the browser window that opens.
Convert your script to a Prefect flow
The easiest way to convert a Python script into a workflow is to add a@flow
decorator to the script’s entrypoint, the Python function that runs first.
Flows are containers for workflow logic as code.
They’re the core observable, orchestrated, deployable units of work in Prefect.
Adding @task
decorators to the functions called by the flow converts them to tasks.
Tasks receive metadata about upstream dependencies and the state of those dependencies before they run.
Prefect will record these dependencies and states as it orchestrates these tasks when they run.
my_gh_workflow.py
The
log_prints=True
argument provided to the @flow
decorator logs output from print
statements within the function.Run your flow
Run your Prefect flow just as you would a Python script:Create a work pool
Running a flow locally is a good start, but you should use a remote destination for production flows. A work pool is the most common way to do this.Deploy your flow to Cloud using a managed work pool.
-
Create a managed work pool:
- View your new work pool on the Work Pools page of the UI.
Deploy and schedule your flow
A deployment is used to determine when, where, and how a flow function should run. Deployments elevate flows to remotely configurable entities that have their own API.-
Create a deployment script:
create_deployment.pyYou can store your flow code in nearly any location as long as Prefect can access it. See Where to store your flow code for more details.
-
Run the script to create the deployment:
Check the logs to ensure your deployment was created:
-
Schedule a run for the deployment:
After a minute or so, you should see the flow run graph and logs on the Flow Run page in the UI.
- When you’re done, click the More > Delete button on the Deployments page to stop further scheduled runs.
Next steps
You’ve seen how to move from a Python script to a scheduled, observable, remotely orchestrated workflow with Prefect. Now considering reading:- Write flows
- Write tasks
- Self-host your Prefect server
Need help? Book a meeting with a Prefect Product Advocate to get your questions answered.