In this video, Introduce & Install Python’s new Web Framework. The tutorial will also show you the installation process with examples. If you’re a python developer, you’ll be familiar with Django and Flask, the most popular web frameworks for the 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.

These are the Python Web Frameworks that we are going to talk

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

pip3 install starlette

Example:

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:

$ uvicorn example:app

Starlette on GitHub

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

pip install blacksheep

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()}')

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:

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.

$ uvicorn server:app

BlackSheep on GitHub

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

pip3 install sanic

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)

Sanic on GitHub

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

pip install fastapi

You will also need an ASGI server, for production such as Uvicorn or Hypercorn.

pip install uvicorn

Example:

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:

uvicorn main:app --reload

FastAPI on GitHub

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

pip install klein

Example:

from klein import run, route

@route('/')
def home(request):
    return 'Hello, world!'

run("localhost", 8080)

Klein on GitHub

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,

pipenv install quart
pip install quart

Example:

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,

$ <span class="pl-s1">python app.py</span>

Quart on GitHub

Thanks for reading !

Originally published by Parwiz at codeloop

#python #programming

Introduce & Install the new Web Framework for Python
24.55 GEEK