Neoteroi/BlackSheep: Fast ASGI web framework for Python
December 19, 2024

Neoteroi/BlackSheep: Fast ASGI web framework for Python





BlackSheep is an asynchronous web framework for building event-based web applications using Python. it is inspired by
flask, ASP.NET Coreand work Yuri Selivanov.


from datetime import datetime

from blacksheep import Application, get


app = Application()

@get("https://github.com/")
async def home():
    return f"Hello, World! {datetime.utcnow().isoformat()}"

Get started with the CLI ✨

BlackSheep provides a CLI to quickly start new projects. To try it, first install blacksheep-cli pack:

pip install blacksheep-cli

and then use blacksheep create Command bootstraps the project using one of the supported templates.

The CLI includes help and supports custom templates, using Cookiecutter.

Get started with documentation

This document provides an introductory tutorial:

These project templates can be used to launch new applications faster:

Python: Any version listed in the project’s classifier. The current list is:

BlackSheep falls into the following categories
ASGI web framework, so it requires an ASGI HTTP server to run, e.g. unicornor
super corn. For example, use it with uvicorn:

To execute an application like the example above, use the methods provided by ASGI HTTP Server:

# if the BlackSheep app is defined in a file `server.py`

$ uvicorn server:app

To run a production environment, refer to the documentation for your chosen ASGI server (i.e. unicorn).

Automatic binding and dependency injection

BlackSheep supports automatic binding of request handler values ​​via type annotations or conventions. look More here.

from dataclasses import dataclass

from blacksheep import Application, FromJSON, FromQuery, get, post


app = Application()


@dataclass
class CreateCatInput:
    name: str


@post("/api/cats")
async def example(data: FromJSON[CreateCatInput]):
    # in this example, data is bound automatically reading the JSON
    # payload and creating an instance of `CreateCatInput`
    ...


@get("/:culture_code/:area")
async def home(culture_code, area):
    # in this example, both parameters are obtained from routes with
    # matching names
    return f"Request for: {culture_code} {area}"


@get("/api/products")
def get_products(
    page: int = 1,
    size: int = 30,
    search: str = "",
):
    # this example illustrates support for implicit query parameters with
    # default values
    # since the source of page, size, and search is not specified and no
    # route parameter matches their name, they are obtained from query string
    ...


@get("/api/products2")
def get_products2(
    page: FromQuery[int] = FromQuery(1),
    size: FromQuery[int] = FromQuery(30),
    search: FromQuery[str] = FromQuery(""),
):
    # this example illustrates support for explicit query parameters with
    # default values
    # in this case, parameters are explicitly read from query string
    ...

It also supports dependency injectiona feature that provides a consistent and clean way of using dependencies in request handlers.

OpenAPI file generation

OpenAPI file generation.

Strategies for handling authentication and authorization

BlackSheep implements principles to handle authentication and authorization. These functions are documented here:

app.use_authentication()\
    .add(ExampleAuthenticationHandler())


app.use_authorization()\
    .add(AdminsPolicy())


@auth("admin")
@get("https://github.com/")
async def only_for_admins():
    ...


@auth()
@get("https://github.com/")
async def only_for_authenticated_users():
    ...

since version 1.2.1BlackSheep implementation:

This means easy integration with:

Please refer to the document and BlackSheep-example
Learn more details and examples.

BlackSheep includes an HTTP client.

example:

import asyncio

from blacksheep.client import ClientSession


async def client_example():
    async with ClientSession() as client:
        response = await client.get("https://docs.python.org/3/")
        text = await response.text()
        print(text)


asyncio.run(client_example())

Supported platforms and runtimes

  • Python: Build all versions included in matrix
  • Ubuntu
  • Windows 10
  • macOS

See File website.

BlackSheep Community on Gitter.

this main The branch contains the currently developed version, version 2. v1 The branch contains version 1 of the web framework and is used for bug fixes and maintenance.

2024-12-18 16:58:03

Leave a Reply

Your email address will not be published. Required fields are marked *