Table of Contents
Preface xi
Part I Introduction to Flask
1 Installation 1
Creating the Application Directory 2
Virtual Environments 2
Creating a Virtual Environment with Python 3 3
Creating a Virtual Environment with Python 2 3
Working with a Virtual Environment 4
Installing Python Packages with pip 5
2 Basic Application Structure 7
Initialization 7
Routes and View Functions 8
A Complete Application 9
Development Web Server 10
Dynamic Routes 12
Debug Mode 13
Command-Line Options 15
The Request-Response Cycle 17
Application and Request Contexts 17
Request Dispatching 18
The Request Object 19
Request Hooks 20
Responses 21
Flask Extensions 23
3 Templates 25
The Jinja2 Template Engine 26
Rendering Templates 26
Variables 27
Control Structures 28
Bootstrap Integration with Flask-Bootstrap 30
Custom Error Pages 33
Links 36
Static Files 37
Localization of Dates and Times with Flask-Moment 38
4 Web Forms 43
Configuration 44
Form Classes 44
HTML Rendering of Forms 47
Form Handling in View Functions 48
Redirects and User Sessions 51
Message Flashing 53
5 Databases 57
SQL Databases 57
No SQL Databases 58
SQL or NoSQL? 59
Python Database Frameworks 59
Database Management with Flask-SQLAlchemy 61
Model Definition 62
Relationships 64
Database Operations 66
Creating the Tables 66
Inserting Rows 66
Modifying Rows 68
Deleting Rows 68
Querying Rows 68
Database Use in View Functions 71
Integration with the Python Shell 72
Database Migrations with Flask-Migrate 73
Creating a Migration Repository 73
Creating a Migration Script 74
Upgrading the Database 75
Adding More Migrations 76
6 Email 79
Email Support with Flask-Mail 79
Sending Email from the Python Shell 81
Integrating Emails with the Application 81
Sending Asynchronous Email 83
7 Large Application Structure 85
Project Structure 85
Configuration Options 86
Application Package 88
Using an Application Factory 88
Implementing Application Functionality in a Blueprint 90
Application Script 93
Requirements File 93
Unit Tests 94
Database Setup 96
Running the Application 97
Part II Example: A Social Slogging Application
8 User Authentication 101
Authentication Extensions for Flask 101
Password Security 102
Hashing Passwords with Werkzeug 102
Creating an Authentication Blueprint 105
User Authentication with Flask-Login 107
Preparing the User Model for Logins 107
Protecting Routes 108
Adding a Login Form 109
Signing Users In 111
Signing Users Out 112
Understanding How Flask-Login Works 113
Testing Logins 114
New User Registration 115
Adding a User Registration Form 115
Registering New Users 117
Account Confirmation 118
Generating Confirmation Tokens with itsdangerous 118
Sending Confirmation Emails 120
Account Management 125
9 User Roles 127
Database Representation of Roles 127
Role Assignment 131
Role Verification 132
10 User Profiles 137
Profile Information 137
User Profile Page 138
Profile Editor 141
User-Level Profile Editor 141
Administrator-Level Profile Editor 143
User Avatars 146
11 Blog Posts 151
Blog Post Submission and Display 151
Blog Posts on Profile Pages 154
Paginating Long Blog Post Lists 155
Creating Fake Blog Post Data 155
Rendering in Pages 157
Adding a Pagination Widget 158
Rich-Text Posts with Markdown and Flask-PageDown 161
Using Flask-PageDown 162
Handling Rich Text on the Server 164
Permanent Links to Blog Posts 165
Blog Post Editor 167
12 Followers 171
Database Relationships Revisited 171
Many-to-Many Relationships 172
Self-Referential Relationships 174
Advanced Many-to-Many Relationships 174
Followers on the Profile Page 178
Querying Followed Posts Using a Database Join 181
Showing Followed Posts on the Home Page 183
13 User Comments 189
Database Representation of Comments 189
Comment Submission and Display 191
Comment Moderation 193
14 Application Programming Interfaces 199
Introduction to REST 199
Resources Are Everything 200
Request Methods 201
Request and Response Bodies 201
Versioning 202
RESTful Web Services with Flask 203
Creating an API Blueprint 203
Error Handling 204
User Authentication with Flask-HTTPAuth 206
Token-Based Authentication 208
Serializing Resources to and from JSON 210
Implementing Resource Endpoints 213
Pagination of Large Resource Collections 216
Testing Web Services with HTTPie 217
Part III The Last Mile
15 Testing 221
Obtaining Code Coverage Reports 221
The Flask Test Client 224
Testing Web Applications 225
Testing Web Services 228
End-to-End Testing with Selenium 230
Is It Worth It? 234
16 Performance 237
Logging Slow Database Performance 237
Source Code Profiling 239
17 Deployment 241
Deployment Workflow 241
Logging of Errors During Production 242
Cloud Deployment 243
The Heroku Platform 244
Preparing the Application 244
Testing with Heroku Local 253
Deploying with git push 254
Deploying an Upgrade 255
Docker Containers 256
Installing Docker 256
Building a Container Image 257
Running a Container 261
Inspecting a Running Container 262
Pushing Your Container Image to an External Registry 263
Using an External Database 264
Container Orchestration with Docker Compose 265
Cleaning Up Old Containers and Images 269
Using Docker in Production 270
Traditional Deployments 270
Server Setup 271
Importing Environment Variables 271
Setting Up Logging 272
18 Additional Resources 275
Using an Integrated Development" Environment (IDE) 275
Finding Flask Extensions 276
Getting Help 276
Getting Involved with Flask 277
Index 279