Creating a production ready Python REST backend with Flask-Rebar — Part 1

Emile Fugulin
4 min readMar 31, 2019

--

Choosing a framework

Python is probably my favourite language. It is dead easy to learn, yet it offers all the flexibility and features you need to tackle the most complicated problems. As I was starting my end of bachelor project (an IoT Platform) with my team, it was obvious for me that we would go with Python for our backend.

After that usually comes the choice of the framework. The Python ecosystem has become quite fragmented on this part. You have the well established Django that I personally don’t like (I generally avoid big frameworks à la Spring). You also have the many micro-frameworks (Bottle, Pyramid, CherryPy, Falcon, Hug, etc.) of which Flask is the most used. Finally, you have the new kids on the block (Sanic, Starlette, AIOHTTP, Tornado, FastApi, Quart, etc.) that make use of the relatively recent async features in Python.

Even if I believe that ASGI will eventually rule out our current WSGI servers, the current implementations are not quite mature enough for my taste. When you start out to build something that needs to be stable for many years, it is always a though sell to bet on a newer framework. A quick overview gives us:

  • Django: 40k Github stars, 190k Stackoverflow questions
  • Flask: 43k Github stars, 27k Stackoverflow questions
  • Falcon: 7k Github stars, <100 Stackoverflow questions
  • Sanic: 12k Github stars, <100 Stackoverflow questions
  • Fastapi: 2k Github stars, 0 Stackoverflow questions

We decided to go with Flask since many of us had experience with it and we knew it had a lot of plugins to help us integrate everything needed to build a real-world backend. It is not the fastest, nor the shiniest, but it is rock solid and it won’t go away anytime soon.

Choosing your extension(s)

The great thing with Flask is probably it’s numerous extensions. You actually have whole frameworks built on top of Flask. They can help you with a lot of tasks like input/output validation, OpenAPI generation, error handling, etc. Here is a quick comparison of the ones we considered:

I have used Connexion in the past and it is a very valid choice if you want to write the OpenAPI spec first and tie it to your Python functions. However, I find that this file spec file usually becomes big and confusing for larger services. There is no easy way to split it.

So if you rule that one out, you will quickly realize that most of the other choices are either abandoned or not really nice to use. If Flask-Restful (not included above) was the first tentative, Flask-RESTPlus was his successor and Flask-Rebar should become the new standard. Created by Plangrid, this framework has not received a lot of love from the community yet, but it is very good and easy to use. This is why I decided to write about it. I actually love it so much that I created the first public extension to support Auth0 authentication.

Let’s use it!

With the next parts, I plan to create a full tutorial on how to use Flask-Rebar to create a production ready application. But enough chit-chat for now, let’s see some code! To use Rebar, the first step is to initialize the extension like we would do with any other extension. We also need to create a registry which acts a bit like a Flask blueprint:

Next, we will need to define some Marshmallow schemas. As a side note, I discovered Marshmallow when I started using Flask-Rebar and I find it awesome to validate and serialize/deserialize data. Even if you are not doing web development, it is a nice library to use to maintain consistency in your Python program. Here is a simple schema to report the health of our service:

Finally, we create the route and the handler similarly as we would do in a plain Flask application. Note that the marshal_schema will be used to validate the returned dictionary when a request is made to the endpoint.

We can finally start the server and query it to get our JSON response:

Server side
Client side

You can take a look at the complete code example under the tag v1. I hope you enjoyed this post and I will see you soon in the next part of this series on Flask-Rebar!

EDIT: Part 2 of series is live!

--

--