Table of Contents
Mastering Flask
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers, and more
Why subscribe?
Free access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Getting Started
Version control with Git
Installing Git
Git on Windows
Git basics
Python package management with pip
Installing the pip Python package manager on Windows
Installing the pip Python package manager on Mac OS X and Linux
pip basics
Dependency sandboxing with virtualenv
virtualenv basics
The beginning of our project
Using Flask Script
Summary
2. Creating Models with SQLAlchemy
Setting up SQLAlchemy
Python packages
Flask SQLAlchemy
Our first model
Creating the user table
CRUD
Creating models
Reading models
Filtering queries
Updating models
Deleting models
Relationships between models
One-to-many
Many-to-many
The convenience of SQLAlchemy sessions
Database migrations with Alembic
Summary
3. Creating Views with Templates
Jinja's syntax
Filters
default
escape
float
int
join
length
round
safe
title
tojson
truncate
Custom filters
Comments
if statements
Loops
Macros
Flask-specific variables and functions
config
request
session
url_for()
get_flashed_messages()
Creating our views
The view function
Writing the templates and inheritance
The home page template
Writing the other templates
Flask WTForms
WTForms basics
Custom validators
Posting comments
Summary
4. Creating Controllers with Blueprints
Request setup, teardown, and application globals
Error pages
Class-based views
Method class views
Blueprints
Summary
5. Advanced Application Structure
The project as a module
Refactoring the code
Application factories
Summary
6. Securing Your App
Setting up
Updating the models
Creating the forms
Protecting your form from spam with reCAPTCHA
Creating views
Social logins
OpenID
Facebook
Twitter
Using the session
Flask Login
User roles
Summary
7. Using NoSQL with Flask
Types of NoSQL databases
Key-value stores
Document stores
Column family stores
Graph databases
RDBMS versus NoSQL
The strengths of RDBMS databases
Data safety
Speed and scale
Tools
The strengths of NoSQL databases
What database to use when
MongoDB in Flask
Installing MongoDB
Setting Up MongoEngine
Defining documents
Field types
Types of documents
The meta attribute
CRUD
Create
Write safety
Read
Filtering
Update
Delete
Relationships in NoSQL
One-to-many relationships
Many-to-many relationships
Leveraging the power of NoSQL
Summary
8. Building RESTful APIs
What is REST
Setting up a RESTful Flask API
GET requests
Output formatting
Request arguments
POST requests
Authentication
PUT requests
DELETE requests
Summary
9. Creating Asynchronous Tasks with Celery
What is Celery?
Setting up Celery and RabbitMQ
Creating tasks in Celery
Running Celery tasks
Celery workflows
Partials
Callbacks
Group
Chain
Chord
Running tasks periodically
Monitoring Celery
Web-based monitoring with Flower
Creating a reminder app
Creating a weekly digest
Summary
10. Useful Flask Extensions
Flask Script
Flask Debug Toolbar
Flask Cache
Caching views and functions
Caching functions with parameters
Caching routes with query strings
Using Redis as a cache backend
Using memcached as a cache backend
Flask Assets
Flask Admin
Creating basic admin pages
Creating database admin pages
Enhancing the post's administration
Creating file system admin pages
Securing Flask Admin
Flask Mail
Summary
11. Building Your Own Extension
Creating a YouTube Flask extension
Creating a Python package
Modifying the response with Flask extensions
Summary
12. Testing Flask Apps
What are unit tests?
How does testing work?
Unit testing the application
Testing the route functions
User interface testing
Test coverage
Test-driven development
Summary
13. Deploying Flask Apps
Deploying on your own server
Pushing code to your server with fabric
Running your web server with supervisor
Gevent
Tornado
Nginx and uWSGI
Apache and uWSGI
Deploying on Heroku
Using Heroku Postgres
Using Celery on Heroku
Deploying on Amazon web services
Using Flask on Amazon Elastic Beanstalk
Using Amazon Relational Database Service
Using Celery with Amazon Simple Queue Service
Summary
Index
Copyright © 2015 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: September 2015
Production reference: 1250915
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78439-365-6
www.packtpub.com
Author
Jack Stouffer
Reviewers
Nidal Alhariri
Pedro Alejandro Baumann
Ben Chaimberg
Ayun Park
Rotem Yaari
Commissioning Editor
Julian Ursell
Acquisition Editor
Harsha Bharwani
Content Development Editor
Riddhi Tuljapurkar
Technical Editor
Gaurav Suri
Copy Editor
Dipti Mankame
Project Coordinator
Sanchita Mandal
Proofreader
Safis Editing
Indexer
Priya Sane
Production Coordinator
Nitesh Thakur
Cover Work
Nitesh Thakur
2323__perlego__cha...