Flask Web Development, 2nd Edition

Book description

Take full creative control of your web applications with Flask, the Python-based microframework. With the second edition of this hands-on book, youâ??ll learn Flask from the ground up by developing a complete, real-world application created by author Miguel Grinberg. This refreshed edition accounts for important technology changes that have occurred in the past three years.

Explore the frameworkâ??s core functionality, and learn how to extend applications with advanced web techniques such as database migrations and an application programming interface. The first part of each chapter provides you with reference and background for the topic in question, while the second part guides you through a hands-on implementation.

If you have Python experience, youâ??re ready to take advantage of the creative freedom Flask provides. Three sections include:

  • A thorough introduction to Flask: explore web application development basics with Flask and an application structure appropriate for medium and large applications
  • Building Flasky: learn how to build an open source blogging application step-by-step by reusing templates, paginating item lists, and working with rich text
  • Going the last mile: dive into unit testing strategies, performance analysis techniques, and deployment options for your Flask application

Publisher resources

View/Submit Errata

Table of contents

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

Product information

  • Title: Flask Web Development, 2nd Edition
  • Author(s): Miguel Grinberg
  • Release date: March 2018
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781491991732