Chapter 13. RESTful Data with Flask
In Chapter 12 we saw how to begin building a basic RESTful web server with Flask, limited to GET requests. This allowed retrieval of a dataset and some of its subsets. For most visualizations, the constraint that data be passively consumed, not altered, is acceptable,1 but even allowing for this, a lot of fairly basic stuff was missing (e.g., the ability to paginate retrieved data, allowing you to control the size of responses from the server). In this chapter, we’ll see two Flask RESTful plugins that add this functionality and a whole lot more for the price of a few lines of Python. I think you’ll be impressed by how much web API can be rolled with so little.
We’ll deal with the two major use cases: serving data from SQL and NoSQL (in the shape of MongoDB) databases with Flask-Restless and Flask Eve, respectively.
Although there are some Flask RESTful plugins that can be adapted to work with both SQL and NoSQL database backends (e.g., Flask-RESTful), in my experience adapting them is a little awkward and, given the shallow learning curve and limited boilerplate code involved, it’s easier to stick with the specialist solutions like the two we’re about to put through their paces. First off we’ll examine Python Eve, a fairly recent Flask RESTful library that is impressively full-featured and succinct.
A RESTful, MongoDB API with Eve
Python Eve is a plugin for the Flask framework that makes implementing a MongoDB-based RESTful API a relatively ...