Copyright
Table of Contents
Preface
Who This Book Is For
How This Book Is Organized
How to Work with the Example Code
Using Code Examples
Conventions Used in This Book
Safari® Books Online
How to Contact Us
Acknowledgments
Part I. Introduction to Flask
Chapter 1. Installation
Using Virtual Environments
Installing Python Packages with pip
Chapter 2. Basic Application Structure
Initialization
Routes and View Functions
Server Startup
A Complete Application
The Request-Response Cycle
Application and Request Contexts
Request Dispatching
Request Hooks
Responses
Flask Extensions
Command-Line Options with Flask-Script
Chapter 3. Templates
The Jinja2 Template Engine
Rendering Templates
Variables
Control Structures
Twitter Bootstrap Integration with Flask-Bootstrap
Custom Error Pages
Links
Static Files
Localization of Dates and Times with Flask-Moment
Chapter 4. Web Forms
Cross-Site Request Forgery (CSRF) Protection
Form Classes
HTML Rendering of Forms
Form Handling in View Functions
Redirects and User Sessions
Message Flashing
Chapter 5. Databases
SQL Databases
NoSQL Databases
SQL or NoSQL?
Python Database Frameworks
Database Management with Flask-SQLAlchemy
Model Definition
Relationships
Database Operations
Creating the Tables
Inserting Rows
Modifying Rows
Deleting Rows
Querying Rows
Database Use in View Functions
Integration with the Python Shell
Database Migrations with Flask-Migrate
Creating a Migration Repository
Creating a Migration Script
Upgrading the Database
Chapter 6. Email
Email Support with Flask-Mail
Sending Email from the Python Shell
Integrating Emails with the Application
Sending Asynchronous Email
Chapter 7. Large Application Structure
Project Structure
Configuration Options
Application Package
Using an Application Factory
Implementing Application Functionality in a Blueprint
Launch Script
Requirements File
Unit Tests
Database Setup
Part II. Example: A Social Blogging Application
Chapter 8. User Authentication
Authentication Extensions for Flask
Password Security
Hashing Passwords with Werkzeug
Creating an Authentication Blueprint
User Authentication with Flask-Login
Preparing the User Model for Logins
Protecting Routes
Adding a Login Form
Signing Users In
Signing Users Out
Testing Logins
New User Registration
Adding a User Registration Form
Registering New Users
Account Confirmation
Generating Confirmation Tokens with itsdangerous
Sending Confirmation Emails
Account Management
Chapter 9. User Roles
Database Representation of Roles
Role Assignment
Role Verification
Chapter 10. User Profiles
Profile Information
User Profile Page
Profile Editor
User-Level Profile Editor
Administrator-Level Profile Editor
User Avatars
Chapter 11. Blog Posts
Blog Post Submission and Display
Blog Posts on Profile Pages
Paginating Long Blog Post Lists
Creating Fake Blog Post Data
Rendering Data on Pages
Adding a Pagination Widget
Rich-Text Posts with Markdown and Flask-PageDown
Using Flask-PageDown
Handling Rich Text on the Server
Permanent Links to Blog Posts
Blog Post Editor
Chapter 12. Followers
Database Relationships Revisited
Many-to-Many Relationships
Self-Referential Relationships
Advanced Many-to-Many Relationships
Followers on the Profile Page
Query Followed Posts Using a Database Join
Show Followed Posts on the Home Page
Chapter 13. User Comments
Database Representation of Comments
Comment Submission and Display
Comment Moderation
Chapter 14. Application Programming Interfaces
Introduction to REST
Resources Are Everything
Request Methods
Request and Response Bodies
Versioning
RESTful Web Services with Flask
Creating an API Blueprint
Error Handling
User Authentication with Flask-HTTPAuth
Token-Based Authentication
Serializing Resources to and from JSON
Implementing Resource Endpoints
Pagination of Large Resource Collections
Testing Web Services with HTTPie
Part III. The Last Mile
Chapter 15. Testing
Obtaining Code Coverage Reports
The Flask Test Client
Testing Web Applications
Testing Web Services
End-to-End Testing with Selenium
Is It Worth It?
Chapter 16. Performance
Logging Slow Database Performance
Source Code Profiling
Chapter 17. Deployment
Deployment Workflow
Logging of Errors During Production
Cloud Deployment
The Heroku Platform
Preparing the Application
Testing with Foreman
Enabling Secure HTTP with Flask-SSLify
Deploying with git push
Reviewing Logs
Deploying an Upgrade
Traditional Hosting
Server Setup
Importing Environment Variables
Setting Up Logging
Chapter 18. Additional Resources
Using an Integrated Development Environment (IDE)
Finding Flask Extensions
Getting Involved with Flask
Index
About the Author