In this Python article we want to introduce Python Top 6 Web Development Frameworks,
if you are working on Web Development projects using Python Programming Language,
than you will have information about Django and Flask Web Development libraries in Python,
these two are the most popular web frameworks for Python, and there are a lot of big companies
that they are using Django as backend, we are not going to talk about these two web frameworks,
but we are talking about some new web frameworks that you will not be familiar with that.
1: Starlette
Starlette is a lightweight ASGI framework/toolkit, which is ideal for building high performance
asyncio services. It is production-ready and give you different features like Web Socket support,
GraphQL support, In-process background tasks, Startup and shutdown events, Test client built
on requests, Session and Cookie support and Zero hard dependencies.
Installation
1 |
pip3 install starlette |
Creating example in starlette
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({'Geekscoders.com': 'Starlette Application'}) routes = [ Route("/", endpoint=homepage) ] app = Starlette(debug=True, routes=routes) |
For runing Starlette application you need to install uivcorn.
1 |
pip install uvicorn |
Now run this command, myexample is our file name.
1 |
uvicorn myexample:app |
And this is the result.
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 |
Creating example in BlackSheep
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'GeeksCoders, BlackSheep! {datetime.utcnow().isoformat()}') |
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,
myexample is the file name.
1 |
uvicorn myexample:app |
This is the result.
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. 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 |
Creating example in sanic
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({'Geekscoders.com': 'Sanic Application'}) if __name__ == '__main__': app.run(host='0.0.0.0', port=8000) |
Run the code and this is the result
4: FastAPI
FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints. it Increase the speed to develop features by about 200% to 300%, it Reduce about 40% of human (developer) induced errors, it is Designed to be easy to use and learn, Less time reading docs, it has Great editor support, Completion everywhere, less time debugging, and it minimize code duplication, multiple features from each parameter declaration, Fewer bugs.
Installation
1 |
pip install fastapi |
Creating example in FastAPI
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 {"Geekscoders.com": "FastAPI"} @app.get("/items/{item_id}") def read_item(item_id: int, q: str = None): return {"item_id": item_id, "q": q} |
You will also need an ASGI server, for production such as Uvicorn or Hypercorn. myexample us our file name.
1 |
uvicorn myexample:app --reload |
This is the result.
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.
Installation
1 |
pip install klein |
Creating example in Klein
1 2 3 4 5 6 7 |
from klein import run, route @route('/') def home(request): return '<h1>Geekscoders.com - Klein Application</h1>' run("localhost", 8080) |
Run the code this is the example
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.
Installation
Quart can be installed via pipenv or pip.
1 2 |
pipenv install quart pip install quart |
Creating example in Quart Application
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 '<h1>Geekscoders.com - Quart Application</h1>' @app.websocket('/ws') async def ws(): while True: await websocket.send('hello') app.run() |
Run the code and this is the result.
Join Our Free Courses