You are previewing Learning Flask Framework.
O'Reilly logo
Learning Flask Framework

Book Description

Build dynamic, data-driven websites and modern web applications with Flask

About This Book

  • Discover the most popular Flask resources on the web with ease

  • Familiarize yourself with third-party libraries commonly used with Flask

  • Create a fast, interactive, and secure web app with this hands-on guide

  • Who This Book Is For

    This book is for anyone who wants to develop their knowledge of Python into something that can be used on the web. Flask follows the Python design principles and can be easily understood by anyone who knows Python, and even by those who do not.

    What You Will Learn

  • Create your web pages to add modularity and flexibility to your web app using templates

  • Store and retrieve relational data using SQLAlchemy

  • Develop schema migrations with Alembic

  • Produce an admin section using flask-admin

  • Build RESTful APIs using Flask-Restless

  • Simulate requests and sessions using the Flask test client

  • Make Ajax requests from Jinja2 templates

  • In Detail

    Flask is a small and powerful web development framework for Python. It does not presume or force a developer to use a particular tool or library. Flask supports extensions that can add application features as if they were implemented in Flask itself. Flask’s main task is to build web applications quickly and with less code. With its lightweight and efficient web development framework, Flask combines rapid development and clean, simple design.

    This book will take you through the basics of learning how to apply your knowledge of Python to the web.

    Starting with the creation of a “Hello world” Flask app, you will be introduced to the most common Flask APIs and Flask’s interactive debugger. You will learn how to store and retrieve blog posts from a relational database using an ORM and also to map URLs to views. Furthermore, you will walk through template blocks, inheritance, file uploads, and static assets.

    You will learn to authenticate users, build log in/log out functionality, and add an administrative dashboard for the blog. Moving on, you will discover how to make Ajax requests from the template and see how the Mock library can simplify testing complex interactions.

    Finally, you will learn to deploy Flask applications securely and in an automated, repeatable manner, and explore some of the most popular Flask resources on the web.

    Style and approach

    A comprehensive guide packed with real-world examples and popular use cases; starting with basic overviews and diving into the practical aspects of Flask Framework.

    Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the code file.

    Table of Contents

    1. Learning Flask Framework
      1. Table of Contents
      2. Learning Flask Framework
      3. Credits
      4. About the Authors
      5. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Creating Your First Flask Application
        1. What is Flask?
          1. With great freedom comes great responsibility
        2. Setting up a development environment
          1. Supporting Python 3
        3. Installing Python packages
          1. Installing pip
          2. Installing virtualenv
            1. Why use virtualenv?
            2. Installing virtualenv with pip
        4. Creating your first Flask app
          1. Installing Flask in your virtualenv
          2. Hello, Flask!
          3. Understanding the code
          4. Routes and requests
            1. Reading values from the request
          5. Debugging Flask applications
        5. Introducing the blog project
          1. The spec
          2. Creating the blog project
            1. A barebones Flask app
            2. Zooming out
            3. The import flow
        6. Summary
      9. 2. Relational Databases with SQLAlchemy
        1. Why use a relational database?
        2. Introducing SQLAlchemy
          1. Installing SQLAlchemy
          2. Using SQLAlchemy in our Flask app
          3. Choosing a database engine
          4. Connecting to the database
        3. Creating the Entry model
          1. Creating the Entry table
          2. Working with the Entry model
          3. Making changes to an existing entry
          4. Deleting an entry
        4. Retrieving blog entries
          1. Filtering the list of entries
          2. Special lookups
          3. Combining expressions
            1. Negation
            2. Operator precedence
        5. Building a tagging system
          1. Adding and removing tags from entries
          2. Using backrefs
        6. Making changes to the schema
          1. Adding Flask-Migrate to our project
          2. Creating the initial migration
          3. Adding a status column
        7. Summary
      10. 3. Templates and Views
        1. Introducing Jinja2
          1. Basic template operations
          2. Loops, control structures, and template programming
          3. Jinja2 built-in filters
        2. Creating a base template for the blog
        3. Creating a URL scheme
          1. Defining the URL routes
          2. Building the index view
          3. Building the detail view
          4. Listing entries matching a given tag
          5. Listing all the tags
          6. Full-text search
        4. Adding pagination links
        5. Enhancing the blog app
        6. Summary
      11. 4. Forms and Validation
        1. Getting started with WTForms
          1. Defining a form for the Entry model
          2. A form with a view
          3. The create.html template
          4. Handling form submissions
          5. Validating input and displaying error messages
          6. Editing existing entries
            1. The edit.html template
          7. Deleting entries
          8. Cleaning up
        2. Using flash messages
          1. Displaying flash messages in the template
        3. Saving and modifying tags on posts
          1. Image uploads
          2. Processing file uploads
            1. The image upload template
        4. Serving static files
        5. Summary
      12. 5. Authenticating Users
        1. Creating a user model
        2. Installing Flask-Login
          1. Implementing the Flask-Login interface
        3. Creating user objects
        4. Login and logout views
          1. The login template
          2. Logging out
        5. Accessing the current user
        6. Restricting access to views
          1. Storing an entry's author
          2. Setting the author on blog entries
          3. Protecting the edit and delete views
          4. Displaying a user's drafts
        7. Sessions
        8. Summary
      13. 6. Building an Administrative Dashboard
        1. Installing Flask-Admin
          1. Adding Flask-Admin to our app
        2. Exposing models through the Admin
          1. Customizing the list views
          2. Adding search and filtering to the list view
          3. Customizing Admin model forms
          4. Enhancing the User form
          5. Generating slugs
        3. Managing static assets via the Admin
        4. Securing the admin website
          1. Creating an authentication and authorization mixin
          2. Setting up a custom index page
          3. Flask-Admin templates
        5. Reading more
        6. Summary
      14. 7. AJAX and RESTful APIs
        1. Creating a comment model
          1. Creating a schema migration
        2. Installing Flask-Restless
          1. Setting up Flask-Restless
          2. Making API requests
        3. Creating comments using AJAX
          1. AJAX form submissions
          2. Validating data in the API
          3. Preprocessors and postprocessors
        4. Loading comments using AJAX
          1. Retrieving the list of comments
        5. Reading more
        6. Summary
      15. 8. Testing Flask Apps
        1. Unit testing
          1. Python's unit test module
          2. A simple math test
        2. Flask and unit testing
          1. Testing a page
          2. Testing an API
        3. Test-friendly configuration
        4. Mocking objects
        5. Logging and error reporting
          1. Logging
            1. Logging to file
            2. Custom log messages
            3. Levels
          2. Error reporting
        6. Read more
        7. Summary
      16. 9. Excellent Extensions
        1. SeaSurf and CSRF protection of forms
        2. Creating Atom feeds
        3. Syntax highlighting using Pygments
        4. Simple editing with Markdown
        5. Caching with Flask-Cache and Redis
        6. Creating secure, stable versions of your site by creating static content
          1. Commenting on a static site
          2. Synchronizing multiple editors
        7. Asynchronous tasks with Celery
        8. Creating command line instructions with Flask-script
        9. References
        10. Summary
      17. 10. Deploying Your Application
        1. Running Flask with a WSGI server
          1. Apache's httpd
            1. Serving static files
          2. Nginx
            1. Serving static files
          3. Gunicorn
        2. Securing your site with SSL
          1. Getting your certificate
          2. Apache httpd
          3. Nginx
          4. Gunicorn
        3. Automating deployment using Ansible
        4. Read more
        5. Summary
      18. Index