Mastering Flask
eBook - ePub

Mastering Flask

  1. 288 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Mastering Flask

Book details
Book preview
Table of contents
Citations

About This Book

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

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.

Frequently asked questions

Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes, you can access Mastering Flask by Jack Stouffer in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Python. We have over one million books available in our catalogue for you to explore.

Information

Year
2015
ISBN
9781784393656
Edition
1

Mastering Flask


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

Mastering Flask

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

Credits

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...

Table of contents

  1. Mastering Flask