Comparison of new Python web frameworks
Python has been the go to language for building web services, right from quick-and-dirty RESTful APIs to full-fledged web applications that serve millions of users. If you have been dabbling in this area, you'd have probably used some of the most popular web frameworks already — Django, Flask, Falcon, Tornado, CherryPy, among others.
In the last few years, though, there have been many new kids on the block. These new frameworks have taken a fresh approach with focus on performance and expressiveness of the API. Here's a comparision of new web frameworks in Python that you should consider for your next side project.
You might also want to check out DeepSource's static analysis for Python, that detects 550+ bug risks, anti-patterns, and security vulnerabilities in your Python code.
1. Sanic
Sanic dubs itself as a web server and web framework that's written to go fast. It allows the usage of the `async` / `await` syntax added in Python 3.5, which makes your code non-blocking and speedy. Sanic makes use of `uvloop` and `ujson` to help with performance, but those packages are optional.
Installation
Hello world example
2. Starlette
Starlette is a lightweight [ASGI](https://asgi.readthedocs.io/en/latest/) framework which is ideal for building high performance `asyncio` services, designed to be used either as a complete framework, or as an ASGI toolkit. With some batteries included, it has support for WebSockets, GraphQL, in-process background tasks, and a test client built on [requests](https://github.com/psf/requests).
Installation
Hello world example
3. Masonite
Masonite is a developer centric Python web framework that strives for an actual batteries included developer tool with a lot of out of the box functionality with an extremely extensible architecture. It has a simple and expressive routing engine, a simple migration system that removes the "magic" and finger crossing of migrations, and a great Active Record style ORM called Orator.
Installation
4. FastAPI
FastAPI is a modern, high-performance, web framework for building APIs with Python 3.6+ based on standard Python type hints. It is built on top of Starlette, and is one of the fastest Python frameworks available. Based on, and fully compatible with, the open standards for APIs — [OpenAPI](https://github.com/OAI/OpenAPI-Specification) (previously known as Swagger) and [JSON Schema](http://json-schema.org/).
Installation
Hello world example
5. Responder
Also based on Starlette, Responder's primary concept is to bring the niceties that are brought forth from both Flask and Falcon and unify them into a single framework. It has a production static file server is built-in, automatic gzipped-responses, native GraphQL support, and a built-in testing client that uses [requests](https://github.com/psf/requests).
Installation
Hello world example
6. Molten
Molten is a minimal, extensible, fast and productive framework for building HTTP APIs with Python. Molten can automatically validate requests according to predefined schemas, ensuring that your handlers only ever run if given valid input. Molten also has support for a function-based middleware, dependency injection, and includes the `molten.contrib` package which contains various functionality commonly required by APIs in the real world such as configuration files, Prometheus metrics, request IDs, sessions, SQLAlchemy, templating, websockets and more.
Installation
Hello world example
7. Japronto
Japronto is a screaming-fast, scalable, asynchronous Python 3.5+ HTTP toolkit integrated with pipelining HTTP server based on uvloop and picohttpparser. It's targeted at speed enthusiasts, people who like plumbing and early adopters. There is no new project development happening at the moment, but it's not abandoned either.
Installation
Hello world example
8. Klein
Klein is a micro-framework for developing production-ready web services with Python. It is 'micro' in that it has an incredibly small API similar to Bottle and Flask. It is not 'micro' in that it depends on things outside the standard library. This is primarily because it is built on widely used and well tested components like Werkzeug and Twisted.
Installation
Hello world example
9. Quart
Quart is a Python ASGI web microframework. It is intended to provide the easiest way to use asyncio functionality in a web context, especially with existing Flask apps. This is possible as the Quart API is a superset of the Flask API. Quart aims to be a complete web microframework, as it supports HTTP/1.1, HTTP/2 and websockets.
Installation
Hello world example
10. BlackSheep
BlackSheep is an asynchronous web framework to build event based, non-blocking Python web applications. It is inspired by Flask and ASP.NET Core. BlackSheep supports automatic binding of values for request handlers, by type annotation or by conventions. It also supports dependency injection, and implements strategies to handle authentication and authorization using external libraries.
Installation
Hello world example
from datetime import datetime
from blacksheep.server import Application
from blacksheep.server.responses import text
app = Application()
@app.route('/')
async def home(request):
return text(f'Hello, World! {datetime.utcnow().isoformat()}')
11. Cyclone
Cyclone is a web server framework that implements the Tornado API as a Twisted protocol. The idea is to bridge Tornado's elegant and straightforward API to Twisted's Event-Loop, enabling a vast number of supported protocols. This combination provides the ground for building up hybrid servers capable of handling HTTP very efficiently while also serve or use e-mail, ssh, sip, irc, etc, all concurrently.
Installation