Chapter 17 Project Practice

Welcome to the Python Project Practice chapter! After mastering the first 16 chapters, you’ve acquired core skills including Python basics, advanced features, network programming, and data science. This chapter will help you integrate your knowledge through two complete practical projects, experiencing the real project development process.

As a senior developer once said: “Theory is the foundation, practice is the bridge, and projects are the touchstone to test learning outcomes.” By the end of this chapter, you will be able to:

  1. Master the complete process of project planning and design
  2. Independently develop a fully functional web application
  3. Build professional-level data analysis platforms
  4. Learn basic skills for project deployment and operations

Let’s embark on this exciting project practice journey!

17.1 Project Planning and Design

Requirement Analysis: The Foundation of Project Success

The first step in project development is requirement analysis, which determines the project’s direction and success. It involves not just listing features but deeply understanding user needs and business goals.

Functional Requirement Analysis

Functional requirements describe what the system should do, detailing core functionalities. Taking a personal blog system as an example:

Core Functional Requirements:
- User management: registration, login, profile management
- Article management: create, edit, delete, publish articles
- Content display: article list, detail page, paginated browsing
- Interactive features: comments, likes, favorites
- Search function: search by title, content, tags

User Story Example:

As a blog author,
I want to easily publish and manage my articles,
so that I can share my knowledge and ideas.

Acceptance Criteria:
- I can create new articles with title, content, and tags
- I can save drafts and continue editing later
- Published articles appear on the homepage
- Markdown format is supported for writing

Non-Functional Requirement Analysis

Non-functional requirements focus on system quality attributes, equally important:

Performance Requirements:
- Page load time < 2 seconds
- Support 100 concurrent users
- Database query response time < 500ms

Security Requirements:
- Passwords encrypted at rest
- Protection against SQL injection
- Protection against cross-site scripting (XSS)

Usability Requirements:
- System availability > 99%
- Responsive design for mobile devices
- Intuitive user interface

System Design: Building a Solid Architecture

Architecture Design

We’ll use the classic MVC (Model-View-Controller) architecture:

┌─────────────────┐    ┌─────────────────┐    ┌─────────────────┐
│   Frontend (View)│    │  Backend (Controller)│    │  Database (Model) │
│                 │    │                 │    │                 │
│ - HTML/CSS/JS   │◄──►│ - Flask App     │◄──►│ - SQLite/MySQL  │
│ - Responsive layout│    │ - Route handling│    │ - Data models   │
│ - User interaction│    │ - Business logic│    │ - Data persistence│
└─────────────────┘    └─────────────────┘    └─────────────────┘

Module Division

Divide the system into independent modules for easier development and maintenance:

blog_system/
├── app.py              # Main application entry
├── models/             # Data models
   ├── __init__.py
   ├── user.py        # User model
   └── post.py        # Article model
├── views/              # View layer
   ├── __init__.py
   ├── auth.py        # Authentication views
   └── blog.py        # Blog-related views
├── templates/          # HTML templates
├── static/            # Static files
└── requirements.txt   # Dependencies

Database Design

Designing a clear database structure is key to project success:

Users Table (users):

CREATE TABLE users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    username VARCHAR(80) UNIQUE NOT NULL,
    email VARCHAR(120) UNIQUE NOT NULL,
    password_hash VARCHAR(120) NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

Articles Table (posts):

CREATE TABLE posts (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    title VARCHAR(100) NOT NULL,
    content TEXT NOT NULL,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    user_id INTEGER NOT NULL,
    FOREIGN KEY (user_id) REFERENCES users (id)
);

Technology Selection: Choosing the Right Tools

Framework Selection

For web development, we choose Flask for these reasons:

Advantages of Flask:
- Lightweight with a gentle learning curve
- High flexibility with component choices
- Rich ecosystem of extensions
- Ideal for rapid development of small-to-medium projects

Core Dependencies:

Flask==2.3.3              # Web framework
Flask-SQLAlchemy==3.0.5    # ORM database
Flask-Login==0.6.3         # User authentication
Werkzeug==2.3.7           # WSGI utility library

Database Selection

Use SQLite during development, upgrade to MySQL or PostgreSQL in production:

Advantages of SQLite:
- No installation or configuration needed
- File-based database for easy development testing
- Full SQL standard support
- Simple automatic backups

Project Management: Ensuring Smooth Project Execution

Development Plan

Break the project into manageable phases:

Phase 1 (1-2 weeks): Basic Framework Setup
- Environment setup and dependency installation
- Database model design and creation
- Basic routing and template structure

Phase 2 (2-3 weeks): Core Function Development
- User registration and login
- Article creation and management
- Basic frontend interface

Phase 3 (1-2 weeks): Advanced Features and Optimization
- Implement search functionality
- UI beautification and responsive design
- Performance optimization and security hardening

Phase 4 (1 week): Testing and Deployment
- Functional testing and bug fixing
- Deployment configuration and launch

17.2 Project 1: Personal Blog System

Project Overview

A personal blog system is a classic web application project covering core modern web development technologies. This project will teach you how to build a fully functional web application from scratch.

Functional Features

Core Features:
- User registration and login system
- Article creation, editing, deletion, and publishing
- Article list display and pagination
- Tag system and category management
- Full-text search functionality
- RESTful API interface

Technical Highlights:
- Responsive design for multi-device access
- Secure user authentication mechanism
- Efficient database query optimization
- Modern frontend interaction experience

Technology Stack

Backend Technologies:
- Python 3.8+
- Flask 2.3.3 (Web framework)
- SQLAlchemy (ORM)
- Flask-Login (user session management)
- Werkzeug (password encryption)

Frontend Technologies:
- HTML5/CSS3
- JavaScript (native/jQuery)
- Bootstrap (responsive framework)
- Jinja2 (template engine)

Database:
- SQLite (development environment)
- MySQL/PostgreSQL (production environment)

Environment Setup

First, create the project directory and virtual environment:

# Create project directory
mkdir blog_system
cd blog_system

# Create virtual environment
python -m venv venv

# Activate virtual environment (Windows)
venv\Scripts\activate

# Activate virtual environment (Linux/Mac)
source venv/bin/activate

# Install dependencies
pip install -r requirements.txt

Dependency file requirements.txt:

Flask==2.3.3
Flask-SQLAlchemy==3.0.5
Flask-Login==0.6.3
Werkzeug==2.3.7
Jinja2==3.1.2
SQLAlchemy==2.0.21

Backend Development

Flask Application Setup

Start with the main application file:

"""
Personal Blog System - Main Flask application file
Author: Python from Beginner to Pro
"""

from flask import Flask, render_template, request, redirect, url_for, flash, jsonify
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
from werkzeug.security import generate_password_hash, check_password_hash
from datetime import datetime
import os

# Create Flask application instance
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your-secret-key-here'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///blog.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

# Initialize extensions
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'

This code sets up the basic Flask application configuration, including secret key, database connection, and login management.

Database Model Design

Define data models, the foundation of application data:

# User model
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password_hash = db.Column(db.String(120), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    posts = db.relationship('Post', backref='author', lazy=True)

    def set_password(self, password):
        """Set password with automatic hashing"""
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        """Verify password"""
        return check_password_hash(self.password_hash, password)

# Article model
class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    content = db.Column(db.Text, nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    tags = db.relationship('Tag', secondary='post_tags', backref='posts')

# Tag model
class Tag(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), unique=True, nullable=False)

# Article-Tag association table
post_tags = db.Table('post_tags',
    db.Column('post_id', db.Integer, db.ForeignKey('post.id'), primary_key=True),
    db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True)
)

Model Design Explanation:

  1. User Model: Inherits UserMixin for login functionality, storing user info with password encryption
  2. Post Model: Article model with foreign key to User, supporting auto-updating timestamps
  3. Tag Model: Tags for article categorization
  4. post_tags Table: Junction table for many-to-many relationship between articles and tags

User Authentication System

Implement complete user registration and login:

@login_manager.user_loader
def load_user(user_id):
    """User loader callback for session management"""
    return User.query.get(int(user_id))

@app.route('/register', methods=['GET', 'POST'])
def register():
    """User registration"""
    if request.method == 'POST':
        username = request.form['username']
        email = request.form['email']
        password = request.form['password']

        # Check for existing username/email
        if User.query.filter_by(username=username).first():
            flash('Username already exists', 'error')
            return redirect(url_for('register'))

        if User.query.filter_by(email=email).first():
            flash('Email already registered', 'error')
            return redirect(url_for('register'))

        # Create new user
        user = User(username=username, email=email)
        user.set_password(password)
        db.session.add(user)
        db.session.commit()

        flash('Registration successful! Please log in', 'success')
        return redirect(url_for('login'))

    return render_template('register.html')

@app.route('/login', methods=['GET', 'POST'])
def login():
    """User login"""
    if request.method == 'POST':
        username = request.form['username']
        password = request.form['password']
        user = User.query.filter_by(username=username).first()

        if user and user.check_password(password):
            login_user(user)
            flash('Login successful!', 'success')
            return redirect(url_for('index'))
        else:
            flash('Username or password incorrect', 'error')

    return render_template('login.html')

Security Features:
- Password encryption using Werkzeug’s generate_password_hash
- User authentication with username/password verification
- Session management via Flask-Login
- Duplicate checks during registration

Article Management Functionality

Implement CRUD operations for articles:

@app.route('/create_post', methods=['GET', 'POST'])
@login_required
def create_post():
    """Create new article"""
    if request.method == 'POST':
        title = request.form['title']
        content = request.form['content']
        tags_str = request.form.get('tags', '')

        # Create article
        post = Post(title=title, content=content, user_id=current_user.id)

        # Process tags
        if tags_str:
            tag_names = [tag.strip() for tag in tags_str.split(',')]
            for tag_name in tag_names:
                tag = Tag.query.filter_by(name=tag_name).first()
                if not tag:
                    tag = Tag(name=tag_name)
                    db.session.add(tag)
                post.tags.append(tag)

        db.session.add(post)
        db.session.commit()

        flash('Article published successfully!', 'success')
        return redirect(url_for('index'))

    return render_template('create_post.html')

@app.route('/post/<int:id>')
def view_post(id):
    """View article details"""
    post = Post.query.get_or_404(id)
    return render_template('post_detail.html', post=post)

@app.route('/search')
def search():
    """Search functionality"""
    query = request.args.get('q', '')
    if query:
        posts = Post.query.filter(
            Post.title.contains(query) | Post.content.contains(query)
        ).order_by(Post.created_at.desc()).all()
    else:
        posts = []
    return render_template('search.html', posts=posts, query=query)

RESTful API Design

To support frontend separation and mobile apps, we provide RESTful APIs:

@app.route('/api/posts')
def api_posts():
    """Get articles list API"""
    posts = Post.query.order_by(Post.created_at.desc()).all()
    return jsonify([{
        'id': post.id,
        'title': post.title,
        'content': post.content[:100] + '...' if len(post.content) > 100 else post.content,
        'author': post.author.username,
        'created_at': post.created_at.isoformat(),
        'tags': [tag.name for tag in post.tags]
    } for post in posts])

Application Startup and Testing

Add startup code at the end of the main file:

if __name__ == '__main__':
    with app.app_context():
        db.create_all()
        # Create default admin account
        if not User.query.filter_by(username='admin').first():
            admin = User(username='admin', email='admin@example.com')
            admin.set_password('admin123')
            db.session.add(admin)
            db.session.commit()
            print("Default admin account created: admin/admin123")

    app.run(debug=True, host='0.0.0.0', port=5000)

Run the application:

python app.py

Sample Output:

Default admin account created: admin/admin123
 * Serving Flask app 'app'
 * Debug mode: on
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://192.168.1.100:5000
 * Press CTRL+C to quit
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 123-456-789

Access http://localhost:5000 in your browser to view the blog system!

17.3 Project 2: Data Analysis Platform

Project Overview

A data analysis platform is essential for modern enterprises. This project teaches building a professional data analysis system covering data collection, cleaning, analysis, and visualization.

Business Requirements

Core Objectives:
- Provide intuitive data analysis tools for business users
- Support multiple data source integration and processing
- Offer rich visualizations and dashboards
- Include data prediction and trend analysis capabilities

Application Scenarios:
- Sales data analysis and prediction
- User behavior analysis
- Business KPI monitoring
- Market trend research

Technology Stack

Data Processing:
- Pandas (data manipulation and analysis)
- NumPy (numerical computing)
- Scikit-learn (machine learning)

Visualization:
- Matplotlib (static charts)
- Seaborn (statistical charts)
- Plotly (interactive charts)

Running the Data Analysis Platform

Execute the analysis program:

python data_processor.py

Sample Output:
```
Sample data generated and saved as ‘sample_data.csv’

=== Step 1: Load Data ===
Data loaded successfully! Data shape: (

Xiaoye