Task Manager REST API — Flask & FastAPI
starter.py/solution.py
convention on purpose: there are two parallel pairs
of files — starter_flask.py/solution_flask.py
and starter_fastapi.py/solution_fastapi.py.
Both build the exact same task manager, so the contrast between the
two frameworks stays concrete and side by side.
A Task Manager REST API — CRUD on an in-memory list of tasks — built once in Flask, once in FastAPI. The Flask version is a small JSON API plus one server-rendered HTML page at /, showing Flask's classic idiom clearly. The FastAPI version is a pure, typed JSON API using Pydantic models for validation, with no HTML page at all — matching FastAPI's idiom and this chapter's point that a JSON API is meant to be consumed by something else.
Example run
=== Task Manager REST API -- Flask version ===
Demonstration (no live server, calling the same logic the routes use):
GET /api/tasks -> [{'id': 1, 'title': 'Write the report', 'done': False}, {'id': 2, 'title': 'Review pull request', 'done': True}]
GET /api/tasks/1 -> {'id': 1, 'title': 'Write the report', 'done': False}
POST /api/tasks -> {'id': 3, 'title': 'Water the plants', 'done': False}
PATCH /api/tasks/1 -> {'id': 1, 'title': 'Write the report', 'done': True}
DELETE /api/tasks/2 -> True
GET /api/tasks (after changes) -> [{'id': 1, 'title': 'Write the report', 'done': True}, {'id': 3, 'title': 'Water the plants', 'done': False}]
BaseModel, a commented-out app.run()/uvicorn.run()
call), but each route is a thin wrapper around a plain Python function
holding the actual logic — those functions are what run directly
in this course, with the server call left for you to uncomment and
run for real, locally, once Flask or FastAPI+uvicorn is installed.
starter_flask.py and/or starter_fastapi.py
below into an empty folder, then run python3 starter_flask.py
(or starter_fastapi.py).
The pieces
- Flask version:
@app.route("/")serves a server-rendered HTML page viarender_template_string();@app.route("/api/tasks", ...)routes serve JSON (Flask auto-converts a returned dict);request.args/request.jsonread query and body data. - FastAPI version:
TaskCreate/TaskUpdate/TaskPydantic models describe and validate every request/response shape;@app.get("/tasks/{task_id}")uses a path parameter;@app.get("/tasks")'sdone: bool | None = Noneargument is a query parameter. - Both versions: five CRUD operations (list, get one, create, update, delete) built as thin route wrappers around plain
_list_tasks_data()/_add_task_data()/etc. functions, testable without any live server.
Ideas to make it your own
- Persist tasks to Chapter 21's mock DB layer instead of a plain in-memory dict.
- Add a
PUT /tasks/{id}route replacing a task's title AND done status together, contrasted withPATCH's partial update. - Add validation rejecting an empty or whitespace-only title.
Why this project matters
Nearly every mobile app backend and modern web application is, at its core, exactly this pattern: CRUD operations exposed over HTTP as a small set of routes. Building the same feature twice — once in Flask's simpler, HTML-first style and once in FastAPI's typed, JSON-first, auto-documented style — makes the Flask-vs-FastAPI tradeoff concrete instead of abstract.
More project ideas
From Chapter 7 onward, you get a genuine choice of what to build. The Task Manager REST API above is fully built out with starter and reference solution files for both frameworks — the four ideas below are not. Each is a real, grounded use case solvable with only what's been taught through Chapter 22. No starter or solution files are provided on purpose — building one of these unassisted is the point.
1 A Simple Blog API
Problem: A blogging platform needs a backend API for creating, listing, and reading blog posts.
What it should do: Build routes for listing posts, getting one post by ID, and creating a new post (title + body), using a Pydantic model (or Flask's request.json) for the create route. Store posts in an in-memory list.
Suggested approach: Reuse this project's exact CRUD-route structure, swapping "task" for "post" and adding a body field.
2 A URL Shortener API
Problem: A URL shortener needs an API that accepts a long URL and returns a short code, then looks that code back up to the original URL.
What it should do: Build a POST route generating a random short code (Chapter 11's random module) for a submitted URL, and a GET route /{short_code} looking it back up.
Suggested approach: A great fit for FastAPI's path parameters — /{short_code} is exactly Sub-topic 4's pattern.
3 A Bookmark Manager with Tags
Problem: A bookmark manager needs to store URLs tagged with categories, and support filtering by tag.
What it should do: Build CRUD routes for bookmarks (url, title, tags: list[str]), plus a filtering query parameter GET /bookmarks?tag=python.
Suggested approach: A Pydantic field can be typed tags: list[str] directly — FastAPI validates it the same way it validates any other typed field.
4 A Personal Expense Tracker API
Problem: Someone wants a small API to log personal expenses and see a running total.
What it should do: Build routes to add an expense (amount, category, optional note), list expenses (optionally filtered by category), and a GET /expenses/total route summing all amounts.
Suggested approach: Build this one in whichever framework you found more comfortable, going deeper in just one instead of splitting effort across both again.