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

Book Description

Over 80 hands-on recipes to help you create small-to-large web applications using Flask

In Detail

Flask Framework Cookbook takes you through a number of recipes that will help you understand the power of Flask and its extensions. You will start by seeing the different ways of configurations that a Flask application can make use of. You will learn how to work with templates and learn about the ORM and view layers. You will see how to write an admin interface followed by the debugging and logging of errors. Finally, you will learn about different deployment and post-deployment techniques on platforms such as Apache, Tornado, and Heroku.

By the end of this book, you will have gained all the knowledge required to write Flask applications in the best possible way, and scale them with best practices.

What You Will Learn

  • Configure Flask in the best way to suit your application needs
  • Integrate Flask with technologies such as Redis, Sentry, and MongoDB
  • Integrate your applications with most of the login mechanisms available
  • Write beautiful RESTful applications
  • Create an admin interface for your application
  • Learn about efficient and effective debugging, logging, and error handling in Flask
  • Test your applications as you write them to prevent future complications
  • Implement full-text search for effective user query handling
  • 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 http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

    Table of Contents

    1. Flask Framework Cookbook
      1. Table of Contents
      2. Flask Framework Cookbook
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        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. Flask Configurations
        1. Introduction
        2. Environment setup with virtualenv
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        3. Handling basic configurations
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Class-based settings
          1. How to do it…
          2. How it works…
        5. Organization of static files
          1. How to do it…
          2. How it works…
          3. There's more…
        6. Being deployment specific with instance folders
          1. How to do it…
          2. How it works…
        7. Composition of views and models
          1. How to do it…
          2. How it works…
          3. See also
        8. Creating a modular web app with blueprints
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        9. Making a Flask app installable using setuptools
          1. How to do it…
          2. How it works…
          3. See also
      9. 2. Templating with Jinja2
        1. Introduction
        2. Bootstrap layout
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Block composition and layout inheritance
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        4. Creating a custom context processor
          1. How to do it…
          2. See also
        5. Creating a custom Jinja2 filter
          1. How to do it…
          2. How it works…
          3. See also
        6. Creating a custom macro for forms
          1. Getting ready
          2. How to do it…
        7. Advanced date and time formatting
          1. Getting ready
          2. How to do it…
          3. See more
      10. 3. Data Modeling in Flask
        1. Introduction
        2. Creating a SQLAlchemy DB instance
          1. Getting ready
          2. How to do it…
          3. There's more…
          4. See also
        3. Creating a basic product model
          1. How to do it…
          2. How it works…
          3. See also
        4. Creating a relational category model
          1. How to do it…
          2. See also
        5. Database migration using Alembic and Flask-Migrate
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        6. Model data indexing with Redis
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Opting the NoSQL way with MongoDB
          1. Getting ready
          2. How to do it…
          3. See also
      11. 4. Working with Views
        1. Introduction
        2. Writing function-based views and URL routes
          1. Getting ready
          2. How to do it…
            1. A simple GET request
            2. A simple POST request
            3. A simple GET/POST request
          3. How it works…
          4. There's more…
        3. Class-based views
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        4. URL routing and product-based pagination
          1. Getting ready
          2. How to do it…
            1. Adding pagination to applications
          3. See also
        5. Rendering to templates
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        6. Dealing with XHR requests
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Decorator to handle requests beautifully
          1. Getting ready
          2. How to do it…
          3. See also
        8. Creating custom 404 and 500 handlers
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        9. Flashing messages for better user feedback
          1. Getting ready
          2. How to do it…
          3. How it works…
        10. SQL-based searching
          1. Getting ready
          2. How to do it…
          3. How it works…
      12. 5. Webforms with WTForms
        1. Introduction
        2. SQLAlchemy model data as form representation
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Validating fields on the server side
          1. How to do it…
          2. How it works…
          3. There's more…
          4. See also
        4. Creating a common forms set
          1. How to do it…
          2. How it works…
        5. Creating custom fields and validation
          1. How to do it…
          2. How it works…
          3. There's more…
        6. Creating a custom widget
          1. How to do it…
          2. How it works…
          3. See also
        7. Uploading files via forms
          1. How to do it…
          2. How it works…
        8. Cross-site Request Forgery protection
          1. How to do it…
          2. How it works…
      13. 6. Authenticating in Flask
        1. Introduction
        2. Simple session-based authentication
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Authenticating using the Flask-Login extension
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        4. Using OpenID for authentication
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Using Facebook for authentication
          1. Getting started
          2. How to do it…
          3. How it works...
        6. Using Google for authentication
          1. Getting ready
          2. How to do it…
          3. How it works…
        7. Using Twitter for authentication
          1. Getting ready
          2. How to do it…
          3. How it works…
      14. 7. RESTful API Building
        1. Introduction
        2. Creating a class-based REST interface
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        3. Creating an extension-based REST interface
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Creating a SQLAlchemy-independent REST API
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        5. A complete REST API example
          1. Getting ready
          2. How to do it…
          3. See also
      15. 8. Admin Interface for Flask Apps
        1. Introduction
        2. Creating a simple CRUD interface
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Using the Flask-Admin extension
          1. Getting ready
          2. How to do it…
          3. There's more…
        4. Registering models with Flask-Admin
          1. Getting ready
          2. How to do it…
        5. Creating custom forms and actions
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. WYSIWYG for textarea integration
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        7. Creating user roles
          1. Getting ready
          2. How to do it…
          3. How it works…
      16. 9. Internationalization and Localization
        1. Introduction
        2. Adding a new language
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        3. Lazy evaluation and the gettext/ngettext functions
          1. Getting ready
          2. How to do it…
        4. Global language-switching action
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      17. 10. Debugging, Error Handling, and Testing
        1. Introduction
        2. Setting up basic file logging
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        3. Sending e-mails on the occurrence of errors
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Using Sentry to monitor exceptions
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Debugging with pdb
          1. Getting ready
          2. How to do it…
          3. See also
        6. Creating our first simple test
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        7. Writing more tests for views and logic
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Nose library integration
          1. Getting ready
          2. How to do it…
          3. See also
        9. Using mocking to avoid real API access
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        10. Determining test coverage
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        11. Using profiling to find bottlenecks
          1. Getting ready
          2. How to do it…
          3. How it works…
      18. 11. Deployment and Post Deployment
        1. Introduction
        2. Deploying with Apache
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        3. Deploying with uWSGI and Nginx
          1. Getting ready
          2. How to do it…
          3. See also
        4. Deploying with Gunicorn and Supervisor
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        5. Deploying with Tornado
          1. Getting ready
          2. How to do it…
          3. How it works…
        6. Using Fabric for deployment
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        7. S3 storage for file uploads
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        8. Deploying with Heroku
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        9. Deploying with AWS Elastic Beanstalk
          1. Getting ready
          2. How to do it…
          3. How it works…
        10. Application monitoring with Pingdom
          1. Getting ready
          2. How to do it…
          3. How it works…
        11. Application performance management and monitoring with New Relic
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      19. 12. Other Tips and Tricks
        1. Introduction
        2. Full-text search with Whoosh
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        3. Full-text search with Elasticsearch
          1. Getting ready
          2. How to do it…
          3. How to do it…
        4. Working with signals
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        5. Using caching with your application
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        6. E-mail support for Flask applications
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        7. Understanding asynchronous operations
          1. Getting ready
          2. How to do it…
          3. How it works…
        8. Working with Celery
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      20. Index