Skip to main content

Fast web framework and HTTP client for Python asyncio

Project description

Build pypi versions codecov license

BlackSheep

BlackSheep is an asynchronous web framework to build event based web applications with Python. It is inspired by Flask, ASP.NET Core, and the work by Yury Selivanov.

Black Sheep

pip install blacksheep

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

Getting started

The documentation offers getting started tutorials:

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

Requirements

Python version 3.7, 3.8, or 3.9.

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:

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

$ uvicorn server:app

To run for production, refer to the documentation of the chosen ASGI server (i.e. for uvicorn).

Automatic bindings and dependency injection

BlackSheep supports automatic binding of values for request handlers by type annotation or by conventions. See more here.

from dataclasses import dataclass

from blacksheep.server.bindings import FromJson


@dataclass
class CreateCatInput:
    name: str


@app.router.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`
    ...


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


@app.router.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
    ...


@app.router.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 injection, a feature that provides a consistent and clean way to use dependencies in request handlers.

Generation of OpenAPI Documentation

Generation of OpenAPI Documentation.

Strategies to handle authentication and authorization

BlackSheep implements strategies to handle authentication and authorization. These features are documented here:

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


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


@auth("admin")
@app.router.get("/")
async def only_for_admins():
    ...


@auth()
@app.router.get("/")
async def only_for_authenticated_users():
    ...

Web framework features

Client features

BlackSheep includes an HTTP Client.

Example:

import asyncio
from blacksheep.client import ClientSession


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

        assert response is not None
        text = await response.text()
        print(text)


loop = asyncio.get_event_loop()
loop.run_until_complete(client_example(loop))

Supported platforms and runtimes

  • Python 3.7 (cpython)
  • Python 3.8 (cpython)
  • Python 3.9 (cpython)
  • Ubuntu 18.04
  • Windows 10
  • macOS

Documentation

Please refer to the documentation website.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

blacksheep-1.0.1.tar.gz (699.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

blacksheep-1.0.1-cp39-cp39-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.9Windows x86-64

blacksheep-1.0.1-cp39-cp39-manylinux1_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.9

blacksheep-1.0.1-cp39-cp39-macosx_10_14_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.9macOS 10.14+ x86-64

blacksheep-1.0.1-cp38-cp38-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.8Windows x86-64

blacksheep-1.0.1-cp38-cp38-manylinux1_x86_64.whl (2.9 MB view details)

Uploaded CPython 3.8

blacksheep-1.0.1-cp38-cp38-macosx_10_14_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.8macOS 10.14+ x86-64

blacksheep-1.0.1-cp37-cp37m-win_amd64.whl (1.1 MB view details)

Uploaded CPython 3.7mWindows x86-64

blacksheep-1.0.1-cp37-cp37m-manylinux1_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.7m

blacksheep-1.0.1-cp37-cp37m-macosx_10_14_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.7mmacOS 10.14+ x86-64

File details

Details for the file blacksheep-1.0.1.tar.gz.

File metadata

  • Download URL: blacksheep-1.0.1.tar.gz
  • Upload date:
  • Size: 699.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for blacksheep-1.0.1.tar.gz
Algorithm Hash digest
SHA256 6de7dff40b689a347b504f541647597dc59591541a214ec66ef7d47532eefc4b
MD5 67f9119401a3fc67c4bb1d7099e061b7
BLAKE2b-256 93d3205d26bc3e8813a5acb860be613227202b5d54d9ea5f2ca895c99ffabab2

See more details on using hashes here.

File details

Details for the file blacksheep-1.0.1-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: blacksheep-1.0.1-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for blacksheep-1.0.1-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 b296b9998531b37f89fc62a77c96bf4a1297eb6eedd6cd132064074a8961d0fa
MD5 594d50ddb86b16be466716436e3c7fba
BLAKE2b-256 f0d45412345c01f22624f0a2806860d5dd896f8f12a68504beca1a03d88ef2de

See more details on using hashes here.

File details

Details for the file blacksheep-1.0.1-cp39-cp39-manylinux1_x86_64.whl.

File metadata

  • Download URL: blacksheep-1.0.1-cp39-cp39-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.8 MB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for blacksheep-1.0.1-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cfd47dcc169d73db3e1d2b331f07619793171164c61264de98436012cccc991b
MD5 39b715629c12246e5c0dbcf1480fe320
BLAKE2b-256 adc1dee099744c406597ab7ac18fea211845945e2d41c3f14e9f8fb199eb2d26

See more details on using hashes here.

File details

Details for the file blacksheep-1.0.1-cp39-cp39-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: blacksheep-1.0.1-cp39-cp39-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.9, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for blacksheep-1.0.1-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 3073ac48df90cef80047f1c7b1edcc989add4d23cf106c1bddc20f7ccb2dff3f
MD5 ea0c9205c55dc690cb0000374676b868
BLAKE2b-256 3228741d933f392b5493c76015eadc6f2fda5b9e8fb26d16a4a9b9a8ded27a85

See more details on using hashes here.

File details

Details for the file blacksheep-1.0.1-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: blacksheep-1.0.1-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for blacksheep-1.0.1-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 eafcf85593feda8524f596c2becda66d69f738dc8ba624000b6930b1ebbd83c6
MD5 a87d2d2fcabb8a1a0a6b36ce79483613
BLAKE2b-256 a38bf95f1266d0090524aad4d64accc3edb26df794f9329eda681071e7208fef

See more details on using hashes here.

File details

Details for the file blacksheep-1.0.1-cp38-cp38-manylinux1_x86_64.whl.

File metadata

  • Download URL: blacksheep-1.0.1-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.9 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for blacksheep-1.0.1-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 2d37d1d33579e99066f17f0aa7ef46d610d11fb5975901b00f59b6235a69915e
MD5 7230a9b18ecbd4ad4fb9369759affd78
BLAKE2b-256 d6d7c367ee21b22b38d46c4b9a882504f8762a50267f70711adfef4668311591

See more details on using hashes here.

File details

Details for the file blacksheep-1.0.1-cp38-cp38-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: blacksheep-1.0.1-cp38-cp38-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.8, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for blacksheep-1.0.1-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 2aa4ab39a888f005b54d7c8296b3d7e948862bf7a00c5412fe5bd5f8d9be8bdd
MD5 92b4b3f2d706e4ec982e4bdef20f1727
BLAKE2b-256 28cc80170a77a3dd6e48a51bdeeedc9a4f714213e5262daf4cb74487b4e72ff7

See more details on using hashes here.

File details

Details for the file blacksheep-1.0.1-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: blacksheep-1.0.1-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for blacksheep-1.0.1-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 ff15d8f08e6d52e60d78eedd73f57c34aa618d2c26ef83d08587d6aec010ec6a
MD5 18049a1b366d2c8870aae0836338157a
BLAKE2b-256 cb357e67eaf7709a1ee023f5a6c58de21d4eee316e1b428564fc793cf5944734

See more details on using hashes here.

File details

Details for the file blacksheep-1.0.1-cp37-cp37m-manylinux1_x86_64.whl.

File metadata

  • Download URL: blacksheep-1.0.1-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.8 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for blacksheep-1.0.1-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c424d240c608a2f532ad02b732dc4178a8ebdae1d4bcb047c76f67ec3728b656
MD5 fb786f6538a1e7bd59c1642e2be60e09
BLAKE2b-256 6c76791f3a6ba947f03bfdcce66bc38483dbb3e917c9d57a6d2539e9a671b60e

See more details on using hashes here.

File details

Details for the file blacksheep-1.0.1-cp37-cp37m-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: blacksheep-1.0.1-cp37-cp37m-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: CPython 3.7m, macOS 10.14+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.2

File hashes

Hashes for blacksheep-1.0.1-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 7fd057a7eae724af31a6af16b86875446db6e66881ed231330ff7bca2e13c04d
MD5 b60961cd9ebe530f9538c4a0e94df760
BLAKE2b-256 d1b01248f0fb3cbc7d887b209ac37cb5c2b4e0aca4eb7cd625d2a7c1a295d7a8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page