Learning Flask Framework
eBook - ePub

Learning Flask Framework

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

Learning Flask Framework

About this book

Build dynamic, data-driven websites and modern web applications with Flask

About This Book

  • Discover the most popular Flask resources on the web with ease
  • Familiarize yourself with third-party libraries commonly used with Flask
  • Create a fast, interactive, and secure web app with this hands-on guide

Who This Book Is For

This book is for anyone who wants to develop their knowledge of Python into something that can be used on the web. Flask follows the Python design principles and can be easily understood by anyone who knows Python, and even by those who do not.

What You Will Learn

  • Create your web pages to add modularity and flexibility to your web app using templates
  • Store and retrieve relational data using SQLAlchemy
  • Develop schema migrations with Alembic
  • Produce an admin section using flask-admin
  • Build RESTful APIs using Flask-Restless
  • Simulate requests and sessions using the Flask test client
  • Make Ajax requests from Jinja2 templates

In Detail

Flask is a small and powerful web development framework for Python. It does not presume or force a developer to use a particular tool or library. Flask supports extensions that can add application features as if they were implemented in Flask itself. Flask's main task is to build web applications quickly and with less code. With its lightweight and efficient web development framework, Flask combines rapid development and clean, simple design.

This book will take you through the basics of learning how to apply your knowledge of Python to the web.

Starting with the creation of a "Hello world" Flask app, you will be introduced to the most common Flask APIs and Flask's interactive debugger. You will learn how to store and retrieve blog posts from a relational database using an ORM and also to map URLs to views. Furthermore, you will walk through template blocks, inheritance, file uploads, and static assets.

You will learn to authenticate users, build log in/log out functionality, and add an administrative dashboard for the blog. Moving on, you will discover how to make Ajax requests from the template and see how the Mock library can simplify testing complex interactions.

Finally, you will learn to deploy Flask applications securely and in an automated, repeatable manner, and explore some of the most popular Flask resources on the web.

Style and approach

A comprehensive guide packed with real-world examples and popular use cases; starting with basic overviews and diving into the practical aspects of Flask Framework.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Learning Flask Framework


Table of Contents

Learning Flask Framework
Credits
About the Authors
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. Creating Your First Flask Application
What is Flask?
With great freedom comes great responsibility
Setting up a development environment
Supporting Python 3
Installing Python packages
Installing pip
Installing virtualenv
Why use virtualenv?
Installing virtualenv with pip
Creating your first Flask app
Installing Flask in your virtualenv
Hello, Flask!
Understanding the code
Routes and requests
Reading values from the request
Debugging Flask applications
Introducing the blog project
The spec
Creating the blog project
A barebones Flask app
Zooming out
The import flow
Summary
2. Relational Databases with SQLAlchemy
Why use a relational database?
Introducing SQLAlchemy
Installing SQLAlchemy
Using SQLAlchemy in our Flask app
Choosing a database engine
Connecting to the database
Creating the Entry model
Creating the Entry table
Working with the Entry model
Making changes to an existing entry
Deleting an entry
Retrieving blog entries
Filtering the list of entries
Special lookups
Combining expressions
Negation
Operator precedence
Building a tagging system
Adding and removing tags from entries
Using backrefs
Making changes to the schema
Adding Flask-Migrate to our project
Creating the initial migration
Adding a status column
Summary
3. Templates and Views
Introducing Jinja2
Basic template operations
Loops, control structures, and template programming
Jinja2 built-in filters
Creating a base template for the blog
Creating a URL scheme
Defining the URL routes
Building the index view
Building the detail view
Listing entries matching a given tag
Listing all the tags
Full-text search
Adding pagination links
Enhancing the blog app
Summary
4. Forms and Validation
Getting started with WTForms
Defining a form for the Entry model
A form with a view
The create.html template
Handling form submissions
Validating input and displaying error messages
Editing existing entries
The edit.html template
Deleting entries
Cleaning up
Using flash messages
Displaying flash messages in the template
Saving and modifying tags on posts
Image uploads
Processing file uploads
The image upload template
Serving static files
Summary
5. Authenticating Users
Creating a user model
Installing Flask-Login
Implementing the Flask-Login interface
Creating user objects
Login and logout views
The login template
Logging out
Accessing the current user
Restricting access to views
Storing an entry's author
Setting the author on blog entries
Protecting the edit and delete views
Displaying a user's drafts
Sessions
Summary
6. Building an Administrative Dashboard
Installing Flask-Admin
Adding Flask-Admin to our app
Exposing models through the Admin
Customizing the list views
Adding search and filtering to the list view
Customizing Admin model forms
Enhancing the User form
Generating slugs
Managing static assets via the Admin
Securing the admin website
Creating an authentication and authorization mixin
Setting up a custom index page
Flask-Admin templates
Reading more
Summary
7. AJAX and RESTful APIs
Creating a comment model
Creating a schema migration
Installing Flask-Restless
Setting up Flask-Restless
Making API requests
Creating comments using AJAX
AJAX form submissions
Validating data in the API
Preprocessors and postprocessors
Loading comments using AJAX
Retrieving the list of comments
Reading more
Summary
8. Testing Flask Apps
Unit testing
Python's unit test module
A simple math test
Flask and unit testing
Testing a page
Testing an API
Test-friendly configuration
Mocking objects
Logging and error reporting
Logging
Logging to file
Custom log messages
Levels
Error reporting
Read more
Summary
9. Excellent Extensions
SeaSurf and CSRF protection of forms
Creating Atom feeds
Syntax highlighting using Pygments
Simple editing with Markdown
Caching with Flask-Cache and Redis
Creating secure, stable versions of your site by creating static content
Commenting on a static site
Synchronizing multiple editors
Asynchronous tasks with Celery
Creating command line instructions with Flask-script
References
Summary
10. Deploying Your Application
Running Flask with a WSGI server
Apache's httpd
Serving static files
Nginx
Serving static files
Gunicorn
Securing your site with SSL
Getting your certificate
Apache httpd
Nginx
Gunicorn
Automating deployment using Ansible
Read more
Summary
Index

Learning Flask Framework

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 authors, 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: November 2015
Production reference: 1241115
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78398-336-0
www.packtpub.com
2323__perlego__ch...

Table of contents

  1. Learning Flask Framework

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
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 990+ topics, we’ve got you covered! Learn about our mission
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 about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Learning Flask Framework by Matt Copperwaite, Charles Leifer 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.