Python web frameworks

Web frameworks play an important role as the backbone of the web application and an intermediary between the web server and your custom code. Thanks to the web framework, you can write just a handful of functions and classes, and make them accessible for your clients via URLs and web interfaces; all of the internals of routing, protocols, error handling, and security precautions are taken care of by the frameworks. 

Python has a variety of web frameworks for any taste and scale. The most popular are Pyramid, Django, and Flask. Both Pyramid and Django are great options for large and complex applications and incorporate many useful features, including CMS, authentication mechanisms, and many other features, out of the box. In contrast, Flask is a smaller and simpler one and is often used for standalone APIs; it doesn't have many built-in features; hence, it's smaller and simpler to learn, but you will have to integrate those features yourself later on if you need them. All three are mostly focused on serving HTML web pages but could be used to serve RESTful APIs, if needed. In this chapter, we'll use a framework called FastAPI: it is very new, focuses 100% on APIs, and incorporates a handful of nice features for building them.

In particular, FastAPI has the following features:

  • Lightweight
  • Performant
  • Can work in asynchronous mode (we'll talk about it later in this chapter)
  • Leverages Python typing for schema/validation
  • Supports data validation with OpenAPI (previously known as Swagger) and JSON Schema off the shelf

The selection of a framework will have long-standing consequences, as you might have to maintain and expand it for a long time. As FastAPI is small and simple, it is easy to learn and to make your own opinion—and to switch it with something else, if needed. But how does it all work in practice? Let's have a look.

..................Content has been hidden....................

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