This tutorial will guide you through building a simple ToDo application using Flask for the web API and Temporal for orchestrating workflows. This app enables users to add tasks, retrieve all tasks, fetch a task by its ID, and toggle a task's completion status.
- Have Python installed.
- Install Flask, SQLAlchemy, and Temporal Python SDK.
- Familiarity with basic Flask concepts and SQLAlchemy.
The activities.py
file defines the Task
model and database-related operations.
- Define the Task Model: This is the structure of the task with fields such as id, title, and completed status.
- Setup SQLite Database: For this tutorial, we are using SQLite. However, you can easily switch to another database.
Temporal activities are individual pieces of logic or operations in the workflow.
- The
add_task
activity allows us to add a new task. - The
get_task_by_id
fetches a specific task. - The
get_all_tasks
fetches all tasks. - The
toggle_task
toggles the completed status of a task.
Each of these activities interacts with the database, and they are asynchronous.
In the app.py
file, the Flask routes are defined.
- The
main
route displays the web page. - Other routes are API endpoints that execute corresponding workflows in Temporal.
- The
get_temporal_client
method creates a connection to the Temporal server.
The workflows.py
file defines the workflows, which are a series of steps or operations.
Each workflow corresponds to an API endpoint. The workflow uses the defined activities and sets a timeout for each operation.
Temporal requires a worker to poll for tasks and execute them. The run_worker.py
file runs a Temporal worker that listens for tasks and executes the defined workflows and activities.
- First, start the Temporal worker using the
run_worker.py
script. - Next, start the Flask app using the
app.py
script.
With Flask handling web requests and Temporal managing workflows, we've created a robust ToDo app. Flask serves the API, while Temporal ensures that operations like adding or toggling tasks are handled reliably. This separation of concerns allows for better scalability and fault tolerance.
Remember, Temporal is suitable for applications that require reliability, as it provides features like retries, timeouts, and distributed tracing out of the box.
By following this tutorial, you've learned how to combine Flask and Temporal to build a resilient and scalable application. Now, you can expand upon this foundation by adding more features, integrating user authentication, or switching to a more scalable database.
To start the app, run the following command:
# terminal 1
poetry run python run_worker.py
# terminal 2
poetry run python app.py
Now you can interact with the app using the provided API. Choose one of the following options:
You can visit the web app at http://127.0.0.1:5000 and interact with the app.
The provided code showcases a Flask API integrated with Temporal.
Here's how you would interact with this API using curl
commands:
-
Add a Task:
You can use this
curl
command to add a task. This is a POST request that takes atitle
for the task.curl -X POST http://127.0.0.1:5000/add_task -H "Content-Type: application/json" -d '{"title": "Hello World!"}'
-
Get All Tasks:
You can fetch all tasks with this command. This is a simple GET request.
curl -X GET http://127.0.0.1:5000/get_all_tasks
-
Get a Specific Task by ID:
For retrieving a specific task using its
task_id
, you'd use thiscurl
command:curl -X GET http://127.0.0.1:5000/get_task/1
Replace
1
with the actual task ID you wish to retrieve. -
Complete a Task:
To mark a task as completed, use this
curl
command:curl -X POST http://127.0.0.1:5000/toggle_task/1
Replace
1
with the actual task ID you wish to complete.