Flask Web Development

Book description

Take full creative control of your web applications with Flask, the Python-based microframework. With this hands-on book, you’ll learn Flask from the ground up by developing a complete social blogging application step-by-step. Author Miguel Grinberg walks you through the framework’s core functionality, and shows you how to extend applications with advanced web techniques such as database migration and web service communication.

Table of contents

  1. Dedication
  2. 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. Safari® Books Online
    7. How to Contact Us
    8. Acknowledgments
  3. I. Introduction to Flask
    1. 1. Installation
      1. Using Virtual Environments
      2. Installing Python Packages with pip
    2. 2. Basic Application Structure
      1. Initialization
      2. Routes and View Functions
      3. Server Startup
      4. A Complete Application
      5. The Request-Response Cycle
        1. Application and Request Contexts
        2. Request Dispatching
        3. Request Hooks
        4. Responses
      6. Flask Extensions
        1. Command-Line Options with Flask-Script
    3. 3. Templates
      1. The Jinja2 Template Engine
        1. Rendering Templates
        2. Variables
        3. Control Structures
      2. Twitter Bootstrap Integration with Flask-Bootstrap
      3. Custom Error Pages
      4. Links
      5. Static Files
      6. Localization of Dates and Times with Flask-Moment
    4. 4. Web Forms
      1. Cross-Site Request Forgery (CSRF) Protection
      2. Form Classes
      3. HTML Rendering of Forms
      4. Form Handling in View Functions
      5. Redirects and User Sessions
      6. Message Flashing
    5. 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
    6. 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
    7. 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. Launch Script
      5. Requirements File
      6. Unit Tests
      7. Database Setup
  4. II. Example: A Social Blogging Application
    1. 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. 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
    2. 9. User Roles
      1. Database Representation of Roles
      2. Role Assignment
      3. Role Verification
    3. 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
    4. 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 Data on 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
    5. 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. Query Followed Posts Using a Database Join
      4. Show Followed Posts on the Home Page
    6. 13. User Comments
      1. Database Representation of Comments
      2. Comment Submission and Display
      3. Comment Moderation
    7. 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
  5. III. The Last Mile
    1. 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?
    2. 16. Performance
      1. Logging Slow Database Performance
      2. Source Code Profiling
    3. 17. Deployment
      1. Deployment Workflow
      2. Logging of Errors During Production
      3. Cloud Deployment
      4. The Heroku Platform
        1. Preparing the Application
          1. Creating a Heroku account
          2. Installing the Heroku Toolbelt
          3. Creating an application
          4. Provisioning a database
          5. Configuring logging
          6. Configuring email
          7. Running a production web server
          8. Adding a requirements file
          9. Adding a Procfile
        2. Testing with Foreman
        3. Enabling Secure HTTP with Flask-SSLify
        4. Deploying with git push
        5. Reviewing Logs
        6. Deploying an Upgrade
      5. Traditional Hosting
        1. Server Setup
        2. Importing Environment Variables
        3. Setting Up Logging
    4. 18. Additional Resources
      1. Using an Integrated Development Environment (IDE)
      2. Finding Flask Extensions
      3. Getting Involved with Flask
    5. Index
  6. Colophon
  7. Copyright

Product information

  • Title: Flask Web Development
  • Author(s):
  • Release date: May 2014
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449372620