Book Image

Flask Blueprints

By : Joel Perras
Book Image

Flask Blueprints

By: Joel Perras

Overview of this book

Table of Contents (14 chapters)

Simple APIs with Flask-RESTful


One of the great joys of using Flask is the seemingly infinite extensibility and composability that it offers. As it's a rather thin layer that sits atop Werkzeug and Jinja, it does not impose much on the developer in terms of constraints.

Due to this flexibility, we have extensions such as Flask-RESTful at our disposal, which make creating JSON-based APIs a joy. First, let's install the package:

$ pip install flask-restful

Next, let's initialize the extension in our application factory in the usual fashion:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.bcrypt import Bcrypt
from flask.ext.restful import Api

# ………
api = Api()

def create_app(config=None):
    app = Flask(__name__)

    if config is not None:
        app.config.from_object(config)

    db.init_app(app)
    flask_bcrypt.init_app(app)

    api.init_app(app)

    return app

The primary building block of the Flask-RESTful extension is the concept of a resource....