Is Sanic python web framework the new Flask?

What the hell is Sanic web framework anyway?

Intro

The Facts

The Pros

Perfromance comparision between Flask and Sanic
  1. Fast learning curve since Sanic looks a lot like Flask, if you come from Flask background, it should be quite easy to work with Sanic, it even uses the same naming convention as Flask (e.g Blueprints)
  2. Sanic can handle requests Asynchronously. Flask Can’t yet.
  3. Although Sanic is fairly new, there is a lot of packages (middleware or extenstions) created to support Sanic.
  4. This is a little tip from me, for every language/framework there is a github repo as a collection of all of the awesome packages related to that language/framework. Awesome-python, awesome-django, awesome-flask .. etc. Luckily there is Awesome-Sanic and it contains all of the great extensions/middleware for sanic.
  5. Extenstions include (ORM/SQLAlchemy, Auth, Caching-Redis, Queues .. etc)
  6. It is quite easy to create a middlewares/extension for Sanic

The Cons

  1. Like any new framework, I dont know how does it perform in production, Flask is battle tested.

Lets create a simple app using Sanic

Installing sanic

main.py

from sanic import Sanic
from sanic.response import json

app = Sanic(__name__)
@app.route("/")
async def test(request):
return json({"message": "Hello_world"})

if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)

Run it

check your browser at http://localhost:8000

Now lets add more endpoints


from sanic import Sanic
from sanic import response
from sanic.response import json

app = Sanic(__name__)


@app.route("/", methods=["GET"])
async def index(request):
return json({"message": "Hello_world"})


@app.route("/post", methods=["POST"])
async def post(request):
return json({"message": request.json})


@app.route("/empty", methods=["PUT"])
async def empty_response(request):
return response.empty()


if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)

Lets see how easy to create a middleware

so if a request is coming with a camelCase JSON it will be converted to snake_case, and viseversa for response

lets create a new file calledsanic_camelcase_middleware.py

and add the following code

from json import loads, dumps
from humps import camelize, decamelize


class Camelize:
def __init__(self, app):
@app.middleware("request")
async def request_to_snake_case(request):

if request.body:
request.body = bytes(dumps(decamelize(loads(request.body))), "utf-8")

@app.middleware("response")
async def response_to_camel(request, response):

if response.body:
response.body = bytes(dumps(camelize(loads(response.body))), "utf-8")

we are using pyhumps to handle the camilization/decamelization of the body.

so we must install that

pipenv install pyhumps

now lets import this code in our main.py

# -*- coding: utf-8 -*-
"""Sample main.py used for running tests for middleware

"""
from sanic import Sanic
from sanic import response
from sanic.response import json
from sanic_camelcase_middleware import Camelize

app = Sanic(__name__)

Camelize(app)


@app.route("/", methods=["GET"])
async def index(request):
return json({"is_camelcase": True, "message": "Hello_world"})


@app.route("/post", methods=["POST"])
async def post(request):
return json({"is_camelcase": True, "message": request.json})


@app.route("/empty", methods=["PUT"])
async def empty_response(request):
return response.empty()


if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000)

that all what we need is to instantiate the Camelize class with the sanic() object.

pipenv run python main.py

{"isCamelcase": true, "message": "Hello_world"}

Camelize only deals with keys not values hence “Hello_world” remains the same.

I have already packaged this middleware So no need to create a new middleware if you need you use sanic_camelcase_middleware just use pip

pip install sanic_camelcase_middleware

Writing tests

import json
from humps import camelize
from main import app

def test_get_request():
"""test_get_request function tests get requests for middleware
The purpose of this test is to check if the middleware would work if the request
has no payload in case of "GET" requests.
However the respons should be camilized.
"""
request, response = app.test_client.get("/")
assert response.status == 200
assert response.json == {"isCamelcase": True, "message": "Hello_world"}

Running tests

Conclusion

Software Developer