In this Python article iam going to have Introduction in Python New Web Frameworks, if you are a python developer you will be familiar with Django and Flask, the most popular web frameworks for python programming language.
but in this video we are not going to talk about django or flask, we are going to talk about the new web frameworks for python.
1: Starlette
Starlette is a lightweight ASGI framework/toolkit, which is ideal for building high performance asyncio services.
It is production-ready, and gives you the following:
- Seriously impressive performance.
- WebSocket support.
- GraphQL support.
- In-process background tasks.
- Startup and shutdown events.
- Test client built on
requests
. - CORS, GZip, Static Files, Streaming responses.
- Session and Cookie support.
- 100% test coverage.
- 100% type annotated codebase.
- Zero hard dependencies.
Requirements
Python 3.6+
Installation
1 |
pip3 install starlette |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from starlette.applications import Starlette from starlette.responses import JSONResponse from starlette.routing import Route async def homepage(request): return JSONResponse({'hello': 'world'}) routes = [ Route("/", endpoint=homepage) ] app = Starlette(debug=True, routes=routes) |
Then run the application using Uvicorn:
1 |
$ uvicorn example:app |
2: Blacksheep
BlackSheep is an asynchronous web framework to build event based, non-blocking Python web applications. It is inspired by Flask, ASP.NET Core, and the work by Yury Selivanov.
Installation
1 |
pip install blacksheep |
Example:
1 2 3 4 5 6 7 8 9 10 |
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()}') |
Requirements
BlackSheep belongs to the category of ASGI web frameworks, so it requires an ASGI HTTP server to run, such as uvicorn, daphne, or hypercorn. For example, to use it with uvicorn:
1 |
pip install uvicorn |
To run an application like in the example above, use the methods provided by the ASGI HTTP Server: server is the name of our python file.
1 |
$ uvicorn server:app |
3: Sanic
Sanic is a Python 3.6+ 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.
The project is maintained by the community, for the community. Contributions are welcome!
The goal of the project is to provide a simple way to get up and running a highly performant HTTP server that is easy to build, to expand, and ultimately to scale.
Installation
1 |
pip3 install sanic |
Example:
1 2 3 4 5 6 7 8 9 10 11 |
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) |
4: FastAPI
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.
The key features are:
- Fast: Very high performance, on par with NodeJS and Go (thanks to Starlette and Pydantic). One of the fastest Python frameworks available.
- Fast to code: Increase the speed to develop features by about 200% to 300% *.
- Fewer bugs: Reduce about 40% of human (developer) induced errors. *
- Intuitive: Great editor support. Completion everywhere. Less time debugging.
- Easy: Designed to be easy to use and learn. Less time reading docs.
- Short: Minimize code duplication. Multiple features from each parameter declaration. Fewer bugs.
- Robust: Get production-ready code. With automatic interactive documentation.
- Standards-based: Based on (and fully compatible with) the open standards for APIs: OpenAPI (previously known as Swagger) and JSON Schema.
* estimation based on tests on an internal development team, building production applications.
Installation
1 |
pip install fastapi |
You will also need an ASGI server, for production such as Uvicorn or Hypercorn.
1 |
pip install uvicorn |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} @app.get("/items/{item_id}") def read_item(item_id: int, q: str = None): return {"item_id": item_id, "q": q} |
Run the server with:
1 |
uvicorn main:app --reload |
5: 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.
A Klein bottle is an example of a non-orientable surface, and a glass Klein bottle looks like a twisted bottle or twisted flask. This, of course, made it too good of a pun to pass up.
Klein’s documentation can be found at Read The Docs.
Installation
1 |
pip install klein |
Example:
1 2 3 4 5 6 7 |
from klein import run, route @route('/') def home(request): return 'Hello, world!' run("localhost", 8080) |
6: 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. Quart is very extendable and has a number of known extensions and works with many of the Flask extensions.
Installation
Quart can be installed via pipenv or pip,
1 2 |
pipenv install quart pip install quart |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from 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() |
if the above is in a file called app.py
it can be run as,
1 |
$ <span class="pl-s1">python app.py</span> |
Also you can watch the video for this article
Subscribe and Get Free Video Courses & Articles in your Email