Flask Framework Cookbook + code
Год: 2014
Автор: Shalabh Aggarwal
Издательство: Packt Publishing
ISBN: 978-1-78398-340-7
Язык: Английский
Формат: PDF
Качество: Изначально компьютерное (eBook)
Интерактивное оглавление: Нет
Количество страниц: 258
Описание: 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.
Оглавление
Preface 1
Chapter 1: Flask Configurations 7
Introduction 7
Environment setup with virtualenv 8
Handling basic configurations 11
Class-based settings 12
Organization of static files 14
Being deployment specific with instance folders 15
Composition of views and models 16
Creating a modular web app with blueprints 19
Making a Flask app installable using setuptools 21
Chapter 2: Templating with Jinja2 23
Introduction 23
Bootstrap layout 24
Block composition and layout inheritance 26
Creating a custom context processor 32
Creating a custom Jinja2 filter 33
Creating a custom macro for forms 35
Advanced date and time formatting 36
Chapter 3: Data Modeling in Flask 39
Introduction 39
Creating a SQLAlchemy DB instance 40
Creating a basic product model 42
Creating a relational category model 46
Database migration using Alembic and Flask-Migrate 49
Model data indexing with Redis 52
Opting the NoSQL way with MongoDB 53
Table of Contents Chapter 4: Working with Views 57
Introduction 57
Writing function-based views and URL routes 58
Class-based views 60
URL routing and product-based pagination 62
Rendering to templates 64
Dealing with XHR requests 68
Decorator to handle requests beautifully 71
Creating custom 404 and 500 handlers 72
Flashing messages for better user feedback 74
SQL-based searching 77
Chapter 5: Webforms with WTForms 79
Introduction 79
SQLAlchemy model data as form representation 80
Validating fields on the server side 83
Creating a common forms set 86
Creating custom fields and validation 88
Creating a custom widget 90
uploading files via forms 92
Cross-site Request Forgery protection 96
Chapter 6: Authenticating in Flask 99
Introduction 99
Simple session-based authentication 100
Authenticating using the Flask-Login extension 107
Using OpenID for authentication 110
Using Facebook for authentication 115
Using Google for authentication 119
Using Twitter for authentication 122
Chapter 7: RESTful API Building 125
Introduction 126
Creating a class-based REST interface 126
Creating an extension-based REST interface 128
Creating a SQLAlchemy-independent REST API 131
A complete REST API example 133
Chapter 8: Admin Interface for Flask Apps 137
Introduction 137
Creating a simple CRUD interface 138
Using the Flask-Admin extension 143
Table of Contents Registering models with Flask-Admin 146
Creating custom forms and actions 148
WYSIWYG for textarea integration 151
Creating user roles 153
Chapter 9: Internationalization and Localization 159
Introduction 159
Adding a new language 160
Lazy evaluation and the gettext/ngettext functions 164
Global language-switching action 166
Chapter 10: Debugging, Error Handling, and Testing 169
Introduction 170
Setting up basic file logging 170
Sending e-mails on the occurrence of errors 173
Using Sentry to monitor exceptions 174
Debugging with pdb 178
Creating our first simple test 179
Writing more tests for views and logic 181
Nose library integration 185
Using mocking to avoid real API access 187
Determining test coverage 190
Using profiling to find bottlenecks 192
Chapter 11: Deployment and Post Deployment 195
Introduction 196
Deploying with Apache 196
Deploying with uWSGI and Nginx 199
Deploying with Gunicorn and Supervisor 202
Deploying with Tornado 204
Using Fabric for deployment 205
S3 storage for file uploads 207
Deploying with Heroku 209
Deploying with AWS Elastic Beanstalk 212
Application monitoring with Pingdom 215
Application performance management and monitoring with New Relic 217
Chapter 12: Other Tips and Tricks 221
Introduction 221
Full-text search with Whoosh 222
Full-text search with Elasticsearch 224
Working with signals 226
Table of Contents Using caching with your application 228
E-mail support for Flask applications 230
Understanding asynchronous operations 233
Working with Celery 234
Index 237