Comparison of new Python web frameworks
Pick a fresh one for your next side project.
- By Sanket
- ·
- Insights
- Python
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
Installation
pip install sanic
Hello world example
from sanic import Sanic
from sanic.response import json
app = Sanic()
@app.route('/')
async def test(request):
return json({'hello': 'world'})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)
2. Starlette
Installation
pip install starlette
Hello world example
from starlette.applications import Starlette
from starlette.responses import JSONResponse
import uvicorn
app = Starlette(debug=True)
@app.route('/')
async def homepage(request):
return JSONResponse({'hello': 'world'})
if __name__ == '__main__':
uvicorn.run(app, host='0.0.0.0', port=8000)
3. Masonite
Installation
pip install masonite-cli
4. FastAPI
Installation
pip install fastapi
Hello world example
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
5. Responder
Installation
pip install responder
Hello world example
import responder
api = responder.API()
@api.route("/{greeting}")
async def greet_world(req, resp, *, greeting):
resp.text = f"{greeting}, world!"
if __name__ == '__main__':
api.run()
6. Molten
Installation
pip install molten
Hello world example
from molten import App, Route
def hello(name: str) -> str:
return f"Hello {name}!"
app = App(routes=[Route("/hello/{name}", hello)])
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
pip install japronto
Hello world example
from japronto import Application
def hello(request):
return request.Response(text='Hello world!')
app = Application()
app.router.add_route('/', hello)
app.run(debug=True)
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
pip install klein
Hello world example
from klein import run, route
@route('/')
def home(request):
return 'Hello, world!'
run("localhost", 8080)
9. Quart
Installation
pip install quart
Hello world example
rom quart import Quart, websocket
app = Quart(__name__)
@app.route('/')
async def hello():
return 'hello'
@app.websocket('/ws')
async def ws():
while True:
await websocket.send('hello')
app.run()
10. BlackSheep
Installation
pip install blacksheep
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
pip install cyclone