Flask Blueprints

Book description

Dive into the world of the Flask microframework to develop an array of web applications

About This Book

  • Structure, compose, and build powerful Flask HTML-based applications and JSON/XML-based APIs using advanced application design patterns
  • Integrate third-party Flask extensions for tasks such as social authentication, sending emails, and interacting with databases and cache layers
  • Build a series of Flask applications of increasing complexity

Who This Book Is For

If you are a Python web developer who has developed basic Flask applications and now wants to build a series of more complex web applications, then this is the book for you.

What You Will Learn

  • Use the virtualenv Python package to effectively isolate your development environments
  • Convert a simple one-file Flask application into a more full-fledged multi-package application
  • Integrate Flask-Login for simple user authentication, Flask-WTF for forms, and Flask-SQLAlchemy for database interactions
  • Explore URL routing and dispatching in a blueprint structured application
  • Create your own signals and consume them within your application
  • Learn to leverage Werkzeug, the WSGI library that powers much of Flask
  • Implement custom exceptions for handling non-20x response codes
  • Write your own CLI tools for administrative and development tasks of your Flask application using Flask-Script/Click
  • Build your Flask extensions to encapsulate reusable behaviors across your applications
  • Integrate your application with open source JavaScript-based graphing libraries to create simple data visualizations

In Detail

Flask is a small but powerful web development framework for Python. Though Flask is termed a micro-framework, it is no way lacking in functionality; there are many extensions available to Flask which helps it to function at the same level as other large frameworks such as Django and Ruby on Rails.

This book will demonstrate how to develop a series of web application projects with the Python web micro-framework, and leverage extensions and external Python libraries and APIs to extend the development of a variety of larger and more complex web applications.

The book will start by explaining Python's Virtualenv library and how to create and switch between multiple virtual environments. You'll first build an SQL database-backed application, which will use Flask-WTF, Flask-SQLAlchemy, Jinja templates, and other methods. Next you'll move on to a timeline application, built using concepts including pytest-Flask, the Blinker package, data modelling for user timelines, exception handling, and creating and organizing CLI tools.

Moving on, you'll discover how to implement a photo timeline application where you'll explore topics such as writing and running celery tasks, API error handling and testing, and Werkzeug middlewares.

Finally, the book walks you through creating an application which fetches data from GitHub and stores it locally. You will also learn how to install and configure Flask-Click extension.

Style and approach

This book covers how to effectively use the Flask micro-framework to develop a series of web applications. Each chapter focusses on the development of an application increasing in complexity with easy steps to follow.

Table of contents

  1. Flask Blueprints
    1. Table of Contents
    2. Flask Blueprints
    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. Starting on the Right Foot – Using Virtualenv
      1. Setuptools and pip
      2. Avoiding dependency hell, the Python way
      3. Working with virtualenv
        1. Creating a new virtual environment
        2. Activating and deactivating virtual environments
        3. Adding packages to an existing environment
        4. Uninstalling packages from an existing environment
      4. Simplifying common operations – using the virtualenvwrapper tool
      5. Summary
    9. 2. Small to Big – Growing the Flask Application Structure
      1. Your first Flask application structure
      2. From module to package
      3. From package to blueprint
        1. Our first blueprint
      4. Summary
    10. 3. Snap – the Code Snippet Sharing Application
      1. Getting started
        1. Flask-SQLAlchemy
          1. Configuring Flask-SQLAlchemy
          2. SQLAlchemy basics
            1. Declarative mapping and Flask-SQLAlchemy
          3. Snap data models
        2. Flask-Login and Flask-Bcrypt for authentication
        3. Flask-WTF – form validation and rendering
          1. Hashing user passwords
          2. Configure an application SECRET_KEY
          3. Hook up the blueprint
        4. Let's run this thing
        5. The data model for snaps
          1. Better defaults with content-sensitive default functions
        6. Snap view handlers
      2. Summary
    11. 4. Socializer – the Testable Timeline
      1. Starting off
      2. Application factories
        1. The application context
          1. Instantiating an app object
      3. Unit and functional testing
      4. Social features – friends and followers
      5. Functional and integration testing
      6. Publish/subscribe events with Blinker
        1. Signals from Flask and extensions
        2. Creating custom signals
      7. Graceful handling of exceptions
      8. Functional testing
      9. Your newsfeed
      10. Summary
    12. 5. Shutterbug, the Photo Stream API
      1. Starting off
      2. The application factory
      3. Interlude – Werkzeug
      4. Simple APIs with Flask-RESTful
        1. Improved password handling with hybrid attributes
        2. API authentication
          1. Authentication protocols
        3. Getting users
          1. Creating new users
            1. Input validation
          2. API testing
        4. Interlude – Werkzeug middlewares
          1. Back to Shutterbug – uploading photos
            1. File uploads in distributed systems
          2. Testing the photo uploads
        5. Fetching the user's photos
      5. Summary
    13. 6. Hublot – Flask CLI Tools
      1. Starting off
        1. The manage.py file
          1. The built-in default commands
        2. The Flask-Script commands across Blueprints
          1. Submanagers
          2. The required and optional arguments
        3. Flask extensions – the basics
          1. When should an extension be used?
          2. Our extension – GitHubber
      2. Summary
    14. 7. Dinnerly – Recipe Sharing
      1. First OAuth
        1. Why use OAuth?
        2. Terminology
        3. So what's wrong with OAuth 1.0?
        4. Three-legged authorization
      2. Setting up the application
        1. Declaring our models
        2. Handling OAuth in our views
        3. Creating recipes
        4. Posting recipes to Twitter and Facebook
          1. SQLAlchemy events
        5. Finding common friends
      3. Interlude – database migrations
        1. Alembic
      4. Summary
    15. Index

Product information

  • Title: Flask Blueprints
  • Author(s): Joël Perras
  • Release date: November 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781784394783