Discovering Flask

Flask was started around 2010, leveraging the Werkzeug WSGI toolkit (http://werkzeug.pocoo.org/), which provides the foundations for interacting with HTTP requests via the WSGI protocol, and various tools such as a routing system.

Werkzeug is equivalent to Paste, which provided similar features. The Pylons project (http://pylonsproject.org), which is the umbrella organization for projects like Pyramid --another web framework-- integrated Paste and its various components at some point.

Together with Bottle (http://bottlepy.org/) and a handful of other projects, they composed the Python microframeworks ecosystem.

All those projects have a similar goal--they want to offer to the Python community simple tools to build web applications faster.

However, the term microframework can be a bit misleading. It does not mean you can only create micro applications. Using those tools, you can build any application--even a large one. The prefix micro here means that the framework tries to take as few decisions as possible. It lets you freely organize your application code as you want, and use whatever libraries you want.

A microframework acts as the glue code that delivers requests to your system, and sends back responses. It does not enforce any particular paradigm on your project.

A typical example of this philosophy is when you need to interact with an SQL database. A framework like Django is batteries-included, and provides everything you need to build your web app including an Object-Relational Mapper (ORM) to bind objects with database query results. The rest of the framework tightly integrates with the ORM.

If you want to use an alternative ORM like SQLAlchemy (SA) in Django to benefit from some of its great features, you'd not be taking the easiest path, because the whole idea of Django is to provide an entire working system, and let the developer focus on building original features.

Flask, on the other hand, does not care what library you use to interact with your data. The framework will only try to make sure it has enough hooks to be extended by external libraries to provide all kinds of features. In other words, using SQLAlchemy in Flask, and making sure you're doing the right thing with SQL sessions and transactions, will mostly consist of adding a package like Flask-SQLAlchemy in your project. And if you don't like how that particular library integrates SLQAlchemy, you're free to use another one, or to build your integration.

Of course, that's not a silver bullet. Being completely free in your choices also means it's easier to make poor decisions, and build an application that relies on defective libraries or one that's not well designed.

But fear not! This chapter will make sure you know what Flask has to offer, and how to organize your code for building microservices.

This chapter covers the following topics:

  • Which Python?
  • How Flask handles requests
  • Flask built-in features
  • A microservice skeleton
The goal of this chapter is to give you all the information needed to build microservices with Flask. By doing so, it inevitably duplicates some of the information you can find in Flask's official documentation--but focuses on providing interesting details and anything relevant when building microservices. Flask has a good online documentation. Make sure you take a look at its user guide at http://flask.pocoo.org/docs, which should be a great complement to this chapter. The code base in GitHub, located at https://github.com/pallets/flask, is very well documented as well--and the source code is always the ultimate source of truth when you need to understand how something works.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset