Mastering Flask

Book description

Gain expertise in Flask to create dynamic and powerful web applications

About This Book

  • Work with scalable Flask application structures to create complex web apps
  • Discover the most powerful Flask extensions and learn how to create one
  • Deploy your application to real-world platforms using this step-by-step guide

Who This Book Is For

If you are a Flask user who knows the basics of the library and how to create basic web pages with HTML and CSS, and you want to take your applications to the next level, this is the book for you. Harnessing the full power of Flask will allow you to create complex web applications with ease.

What You Will Learn

  • Set up a best practices Python environment
  • Use SQLAlchemy to programmatically query a database
  • Develop templates in Jinja
  • Set up an MVC environment for Flask
  • Discover NoSQL, when to use it, when not to, and how to use it
  • Develop a custom Flask extension
  • Use Celery to create asynchronous tasks
  • Use py.test to create unit tests

In Detail

Flask is a library that allows programmers to create web applications in Python. Flask is a micro-framework that boasts a low learning curve, a large community, and the power to create complex web apps. However, Flask is easy to learn but difficult to master.

Starting from a simple Flask app, this book will walk through advanced topics while providing practical examples of the lessons learned. After building a simple Flask app, a proper app structure is demonstrated by transforming the app to use a Model-View-Controller (MVC) architecture. With a scalable structure in hand, the next chapters use Flask extensions to provide extra functionality to the app, including user login and registration, NoSQL querying, a REST API, an admin interface, and more. Next, you'll discover how to use unit testing to take the guesswork away from making sure the code is performing as it should. The book closes with a discussion of the different platforms that are available to deploy a Flask app on, the pros and cons of each one, and how to deploy on each one.

Style and approach

With plenty of useful examples, this guide introduces new concepts and then shows you how those concepts can be used in a real-world environment. Most sections are based around a single example app that is developed throughout the book.

Table of contents

  1. Mastering Flask
    1. Table of Contents
    2. Mastering Flask
    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. Getting Started
      1. Version control with Git
        1. Installing Git
          1. Git on Windows
        2. Git basics
      2. Python package management with pip
        1. Installing the pip Python package manager on Windows
        2. Installing the pip Python package manager on Mac OS X and Linux
        3. pip basics
      3. Dependency sandboxing with virtualenv
        1. virtualenv basics
      4. The beginning of our project
        1. Using Flask Script
      5. Summary
    9. 2. Creating Models with SQLAlchemy
      1. Setting up SQLAlchemy
        1. Python packages
        2. Flask SQLAlchemy
      2. Our first model
        1. Creating the user table
      3. CRUD
        1. Creating models
        2. Reading models
          1. Filtering queries
        3. Updating models
        4. Deleting models
      4. Relationships between models
        1. One-to-many
        2. Many-to-many
      5. The convenience of SQLAlchemy sessions
      6. Database migrations with Alembic
      7. Summary
    10. 3. Creating Views with Templates
      1. Jinja's syntax
        1. Filters
          1. default
          2. escape
          3. float
          4. int
          5. join
          6. length
          7. round
          8. safe
          9. title
          10. tojson
          11. truncate
          12. Custom filters
        2. Comments
        3. if statements
        4. Loops
        5. Macros
        6. Flask-specific variables and functions
          1. config
          2. request
          3. session
          4. url_for()
          5. get_flashed_messages()
      2. Creating our views
        1. The view function
        2. Writing the templates and inheritance
          1. The home page template
          2. Writing the other templates
      3. Flask WTForms
        1. WTForms basics
        2. Custom validators
        3. Posting comments
      4. Summary
    11. 4. Creating Controllers with Blueprints
      1. Request setup, teardown, and application globals
      2. Error pages
      3. Class-based views
        1. Method class views
      4. Blueprints
      5. Summary
    12. 5. Advanced Application Structure
      1. The project as a module
        1. Refactoring the code
      2. Application factories
      3. Summary
    13. 6. Securing Your App
      1. Setting up
        1. Updating the models
        2. Creating the forms
          1. Protecting your form from spam with reCAPTCHA
        3. Creating views
        4. Social logins
          1. OpenID
          2. Facebook
          3. Twitter
      2. Using the session
      3. Flask Login
        1. User roles
      4. Summary
    14. 7. Using NoSQL with Flask
      1. Types of NoSQL databases
        1. Key-value stores
        2. Document stores
        3. Column family stores
        4. Graph databases
      2. RDBMS versus NoSQL
        1. The strengths of RDBMS databases
          1. Data safety
          2. Speed and scale
          3. Tools
        2. The strengths of NoSQL databases
        3. What database to use when
      3. MongoDB in Flask
        1. Installing MongoDB
        2. Setting Up MongoEngine
        3. Defining documents
          1. Field types
          2. Types of documents
          3. The meta attribute
        4. CRUD
          1. Create
            1. Write safety
          2. Read
            1. Filtering
          3. Update
          4. Delete
        5. Relationships in NoSQL
          1. One-to-many relationships
          2. Many-to-many relationships
      4. Leveraging the power of NoSQL
      5. Summary
    15. 8. Building RESTful APIs
      1. What is REST
      2. Setting up a RESTful Flask API
      3. GET requests
        1. Output formatting
        2. Request arguments
      4. POST requests
        1. Authentication
      5. PUT requests
      6. DELETE requests
      7. Summary
    16. 9. Creating Asynchronous Tasks with Celery
      1. What is Celery?
      2. Setting up Celery and RabbitMQ
      3. Creating tasks in Celery
      4. Running Celery tasks
        1. Celery workflows
          1. Partials
          2. Callbacks
          3. Group
          4. Chain
          5. Chord
          6. Running tasks periodically
      5. Monitoring Celery
        1. Web-based monitoring with Flower
      6. Creating a reminder app
      7. Creating a weekly digest
      8. Summary
    17. 10. Useful Flask Extensions
      1. Flask Script
      2. Flask Debug Toolbar
      3. Flask Cache
        1. Caching views and functions
        2. Caching functions with parameters
        3. Caching routes with query strings
        4. Using Redis as a cache backend
        5. Using memcached as a cache backend
      4. Flask Assets
      5. Flask Admin
        1. Creating basic admin pages
        2. Creating database admin pages
        3. Enhancing the post's administration
        4. Creating file system admin pages
        5. Securing Flask Admin
      6. Flask Mail
      7. Summary
    18. 11. Building Your Own Extension
      1. Creating a YouTube Flask extension
        1. Creating a Python package
      2. Modifying the response with Flask extensions
      3. Summary
    19. 12. Testing Flask Apps
      1. What are unit tests?
      2. How does testing work?
      3. Unit testing the application
        1. Testing the route functions
      4. User interface testing
      5. Test coverage
      6. Test-driven development
      7. Summary
    20. 13. Deploying Flask Apps
      1. Deploying on your own server
        1. Pushing code to your server with fabric
        2. Running your web server with supervisor
        3. Gevent
        4. Tornado
        5. Nginx and uWSGI
        6. Apache and uWSGI
      2. Deploying on Heroku
        1. Using Heroku Postgres
        2. Using Celery on Heroku
      3. Deploying on Amazon web services
        1. Using Flask on Amazon Elastic Beanstalk
        2. Using Amazon Relational Database Service
        3. Using Celery with Amazon Simple Queue Service
      4. Summary
    21. Index

Product information

  • Title: Mastering Flask
  • Author(s): Jack Stouffer
  • Release date: September 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781784393656