/home /blog 30 Mar 2020 | Get ipynb

Redbull - A meta-web framework in Python

Web frameworks are awesome! Take a look at the smallest thing I could think of with bottle.

from bottle import Bottle, request

app = Bottle()


@app.post("/say/hi")
def hi():
    name = request.json.get("name", "")
    return f"hi {name}"


app.run()

Others boast of equally small minimal examples (say Flask for example).

My work at the office often involves quickly deploying an API for someone to build a UI around. With flask/bottle/django it's usually a chore since I have to document the API, provide JSON input and output examples, deal with CORS and so on.

So I wrote Redbull. It's a framework that takes in a an app and provides some usefull wrappings around it. Take for example that last app we wrote. For someone who wants to develop an UI around that API they have to:

Usually those tasks are handled by the person who wrote the API. They have to maintain docs etc. There are very good tools for maintaining docs like Sphinx etc.

The same app with Redbull looks as such:

from bottle import Bottle
from redbull import Manager

mg = Manager(Bottle(), apiversion="1")


@mg.api
def say_hi(name: str = ""):
    "Says hi given a name"
    return f"hi {name}"


mg.finalise()
mg.app.run()

Redbull leverages the typing abilities which were recently introduced into Python and creates automatic docs at /<version>/docs. In addition it uses the type info provided to enforce input cleaning and provide useful messages when the caller messes up.

When the API is called with an OPTIONS method, it provides a docstring along with other things. Redbull does not lock you in. You could still go about using whatever framework you want in whatever way you want. What Redbull does is it makes the deployment of APIs which a UI developer can build against very quick.

The same app could be written using Aiohttp too:

from aiohttp import web
from redbull import Manager

mg = Manager(web.Application(), apiversion="1")


@mg.api
async def say_hi(name: str = ""):
    "Says hi given a name"
    return f"hi {name}"


mg.finalise()
mg.app.run()

Leave a comment/issue/question or a star on the project page at https://github.com/theSage21/redbull