Skip to main content

Fast web framework and HTTP client for Python asyncio

Project description

Build pypi versions codecov

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-0.3.0.tar.gz (652.8 kB view details)

Uploaded Source

Built Distributions

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

blacksheep-0.3.0-cp39-cp39-win_amd64.whl (1.1 MB view details)

Uploaded CPython 3.9Windows x86-64

blacksheep-0.3.0-cp39-cp39-manylinux1_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.9

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

Uploaded CPython 3.9macOS 10.14+ x86-64

blacksheep-0.3.0-cp38-cp38-win_amd64.whl (1.1 MB view details)

Uploaded CPython 3.8Windows x86-64

blacksheep-0.3.0-cp38-cp38-manylinux1_x86_64.whl (2.8 MB view details)

Uploaded CPython 3.8

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

Uploaded CPython 3.8macOS 10.14+ x86-64

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

Uploaded CPython 3.7mWindows x86-64

blacksheep-0.3.0-cp37-cp37m-manylinux1_x86_64.whl (2.6 MB view details)

Uploaded CPython 3.7m

blacksheep-0.3.0-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-0.3.0.tar.gz.

File metadata

  • Download URL: blacksheep-0.3.0.tar.gz
  • Upload date:
  • Size: 652.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for blacksheep-0.3.0.tar.gz
Algorithm Hash digest
SHA256 95822f15887fb22d863b1707f17e49752581ba77c0e805656b95479c4e960ba5
MD5 9c35babc39ce027d89fb208f84c91d20
BLAKE2b-256 2896eb06865142289cbef436ab515855b48d1a027513e45bafdc761ad1cd2b3f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blacksheep-0.3.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for blacksheep-0.3.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 ff7155c594374cfe5acfc87c000665ff41f333fd7cc1dea2a97efed942caa308
MD5 1d31a44e46b5542a27f0af248a3847e6
BLAKE2b-256 0d48ab347b7dfaf249a8a553c3f6c6208c4fe89e3939e1294f0332c8593210b8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blacksheep-0.3.0-cp39-cp39-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.7 MB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for blacksheep-0.3.0-cp39-cp39-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 54679b7855c8f144afd8c407dde66c968689df6ae391158261050fbf857ebef4
MD5 6a2e82fc268d349ec8cd7ede8d105e32
BLAKE2b-256 3441d1ee9489e01b38c86d33049d7e98eefb2aa0aa85a4cebc2adebef01882f9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blacksheep-0.3.0-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.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for blacksheep-0.3.0-cp39-cp39-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 456fea2e5e25593633524c00fb2a132eb5dc4a4770c34950812313c412d321c3
MD5 a06631a7edaa1669e5971cd8e9513718
BLAKE2b-256 250a2f1bb7b18e93a8257f06b33fd7cd65de4401275fe2be6fb8100c4dcf65b7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blacksheep-0.3.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 1.1 MB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for blacksheep-0.3.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 0a70989fa280c6c60dc0798afb9ad2d44008b793ee63803949f97fdc752a8c62
MD5 98978b7b5102115b15ee50000137f209
BLAKE2b-256 2492bc5f55fa551563e05212e9dddb1c4fee00c98430f6c475199191f0f64bc1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blacksheep-0.3.0-cp38-cp38-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.8 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for blacksheep-0.3.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 0fef149f8c25ad948fdef8b848f54862511f3bc0b0745fc9e54d48edda1cd3dd
MD5 742408fe31e574249c5693d9ea1a8fd6
BLAKE2b-256 fba3149585c90a59bfef25097b9c2d2a5e6aef57b54176d1f0ce9a2341d210f1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blacksheep-0.3.0-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.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for blacksheep-0.3.0-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 1a81cbfd4a200a3403e45ab9155538dadddd6cfd7ab51e5edd79ca714a20c436
MD5 ee2b3b52987a44c7963adc216e0a7382
BLAKE2b-256 b7b7115a62cfbfea5cc87861ea36fd2eeab623a64b5ec52cf03b33798bc22189

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blacksheep-0.3.0-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.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for blacksheep-0.3.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 f0dea3d15a17381eb65aaf4f166721b6046e515f35295b86497260f558afb465
MD5 4b0c342ecb145cbcca6b4bc7a55c93ac
BLAKE2b-256 b6b67b0c16a7f4e9de40b8fdebd61375c554d698d8a3e826a05dedca8a6d9013

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blacksheep-0.3.0-cp37-cp37m-manylinux1_x86_64.whl
  • Upload date:
  • Size: 2.6 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for blacksheep-0.3.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 d9e081e2d62cf22c6f1692ef6bb9b80e775843d6fb395de407e3cc235d62f039
MD5 fbcbea3b524fdcee8346231ccedef21f
BLAKE2b-256 1610e24a6c46f72434c8f403d21b1dee155fabb745a8b3dc1614e9fb00719383

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blacksheep-0.3.0-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.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.6

File hashes

Hashes for blacksheep-0.3.0-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 f9c3d4a808ab5d6667a5d85bb20693a6838678c5f1ee79501b153fba4e3f4d7a
MD5 acb6b6204b2407ed4b7b3dcdf22e471b
BLAKE2b-256 6cd96e3f9dc032b68d53d091908f677a5fdd3c5ef2f81f9e36271a85631b6b54

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