Build an Application - Introduction
Now that you have hopefully digested the Python Fundamentals section you have a solid grounding of a majority of Python's language features and how to do TDD.
This next section will involve building an application.
Each chapter will iterate on the previous one, expanding the application's functionality as our product owner dictates.
New concepts will be introduced to help facilitate writing great code but most of the new material will be learning what can be accomplished from Python's standard library and popular frameworks.
What we're building
We'll build a Task Management API - a RESTful web service for managing tasks and projects. This is a practical application that demonstrates:
- HTTP endpoints and REST principles
- JSON request/response handling
- Database integration
- Command-line interfaces
- Real-world TDD practices
Architecture overview
The chapters ahead
1. HTTP Server with Flask
We'll create our first HTTP endpoint and learn:
- Setting up a Flask application
- Handling HTTP requests and responses
- Testing HTTP handlers
- The basics of REST APIs
2. JSON and Routing
We'll expand our API to handle JSON:
- Returning JSON responses
- Parsing JSON request bodies
- URL routing and parameters
- Error handling in APIs
3. Database Integration
We'll persist our data:
- Introduction to SQLAlchemy
- Database models and migrations
- CRUD operations
- Testing with databases
4. Command Line Applications
We'll build a CLI for our API:
- Using argparse and click
- Reading user input
- Formatting output
- Testing CLI applications
Project setup
Let's create our project structure:
mkdir taskapi
cd taskapi
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
Create initial files:
Install dependencies:
pip install flask pytest
pip freeze > requirements.txt
TDD approach
Throughout this section, we'll follow the TDD cycle:
- Red: Write a failing test that describes the feature
- Green: Write the minimum code to make it pass
- Refactor: Improve the code while keeping tests green
Each chapter will start with user requirements, translate them into tests, and then implement the functionality.
Why build a web application?
Web applications are ubiquitous. Learning to build them with TDD teaches you:
- How to test code with external dependencies
- How to structure applications for testability
- Real-world patterns like dependency injection
- How to work with frameworks while maintaining testability
Prerequisites
Make sure you have:
- Python 3.8 or later installed
- A code editor (VS Code, PyCharm, etc.)
- Basic understanding of HTTP (GET, POST, etc.)
- Familiarity with JSON
Let's begin building our application in the next chapter: HTTP Server with Flask.