Skip to main content

Fast web framework for Python asyncio

Project description

Build pypi versions codecov license Join the chat at https://gitter.im/Neoteroi/BlackSheep documentation

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

Important. This branch contains the code of the version 1 of the web framework. The main branch contains the code for the currently developed version.


from datetime import datetime
from blacksheep import Application


app = Application()

@app.route("/")
async def home():
    return 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, 3.9, or 3.10.

BlackSheep belongs to the category of ASGI web frameworks, so it requires an ASGI HTTP server to run, such as uvicorn, 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 import Application, FromJSON, FromQuery


app = Application()


@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(culture_code, area):
    # in this example, both parameters are obtained from routes with
    # matching names
    return 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():
    ...

Since version 1.2.1, BlackSheep implements:

Meaning that it is extremely easy to integrate with services such as:

Refer to the documentation for more details and examples.

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)
  • Python 3.10 (cpython)
  • Ubuntu 18.04
  • Windows 10
  • macOS

Documentation

Please refer to the documentation website.

Communication

BlackSheep community in Gitter.

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.2.14.tar.gz (851.1 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.2.14-cp311-cp311-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.11Windows x86-64

blacksheep-1.2.14-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

blacksheep-1.2.14-cp311-cp311-macosx_10_9_universal2.whl (1.8 MB view details)

Uploaded CPython 3.11macOS 10.9+ universal2 (ARM64, x86-64)

blacksheep-1.2.14-cp310-cp310-win_amd64.whl (1.2 MB view details)

Uploaded CPython 3.10Windows x86-64

blacksheep-1.2.14-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.5 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

blacksheep-1.2.14-cp310-cp310-macosx_11_0_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.10macOS 11.0+ x86-64

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

Uploaded CPython 3.9Windows x86-64

blacksheep-1.2.14-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.5 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

blacksheep-1.2.14-cp39-cp39-macosx_11_0_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.9macOS 11.0+ x86-64

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

Uploaded CPython 3.8Windows x86-64

blacksheep-1.2.14-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.7 MB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

blacksheep-1.2.14-cp38-cp38-macosx_10_15_x86_64.whl (1.3 MB view details)

Uploaded CPython 3.8macOS 10.15+ x86-64

File details

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

File metadata

  • Download URL: blacksheep-1.2.14.tar.gz
  • Upload date:
  • Size: 851.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for blacksheep-1.2.14.tar.gz
Algorithm Hash digest
SHA256 dab86d54e75446aa7e610642bc6c60b7f3f0a839b52be01696cfd6e662a1b5a1
MD5 a41a7b1ea7bfe28fb402d22b0df5062b
BLAKE2b-256 3ec6c9b9f763259ba701fff0965ee09f61cd5d1313caf4b086bf91dd66d3f391

See more details on using hashes here.

File details

Details for the file blacksheep-1.2.14-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for blacksheep-1.2.14-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 e913fc130f4ca435de4a84b5abde4927e9952e6af481f19a6062b2c39b81a856
MD5 aef667fb8b7ede2a1ad868a51d5a9271
BLAKE2b-256 9b4e5e2b165766cdf6951b9317de7e81d46612c8623130355ba5367a3f7a7b2a

See more details on using hashes here.

File details

Details for the file blacksheep-1.2.14-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for blacksheep-1.2.14-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b22ab0c71420b99b164ab7396ac243adcb29c60412608ac4f6ae061bf13ceb98
MD5 dff6153e64cf67a8ee6de7a0dd059412
BLAKE2b-256 2875acb65f959215f8b9344e1749c68ea5ece8789ea242102691ee790e553031

See more details on using hashes here.

File details

Details for the file blacksheep-1.2.14-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for blacksheep-1.2.14-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 63c3c25d63ae5dc15367e922fc18b5d167d97a544b7a0ce72f7182c61115b086
MD5 bd5b0fc2326d6d92d932092d273ae8a8
BLAKE2b-256 62dbcd69a702c5ba343c616cfa50c8fab23e060ea98bead4648ee8e20f23163a

See more details on using hashes here.

File details

Details for the file blacksheep-1.2.14-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for blacksheep-1.2.14-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 21eb544f7eaaf29d669c24c34ca8689b85b2d499d481111d749b4ebdd5b34299
MD5 0ee795ec39d1d87723262b7ea71049da
BLAKE2b-256 fc3cc88c9db0ddd7ed281012f0ca56b0cbfd6800a182a047b1bcbd9f11008a96

See more details on using hashes here.

File details

Details for the file blacksheep-1.2.14-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for blacksheep-1.2.14-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e6e4549cb9e1bfc0f073e161ef2b3f1860a0efa2f6ca41a089bf2e630bc6c3f3
MD5 2b5aa4d9a746f8974551abeee3cdfddc
BLAKE2b-256 d366b7750e9f8ca0807e86278be7d40e5af7c134a89e48f1e5d73af489f5f0d4

See more details on using hashes here.

File details

Details for the file blacksheep-1.2.14-cp310-cp310-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for blacksheep-1.2.14-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 4ae69c98318ec060ac0ff097ca09db9f29033bddd143b6ac4c2fc14baa645c4b
MD5 724616f2082cf01ab82f7b6414577c65
BLAKE2b-256 1815c790784238d955974257b68bd7cbc71351591082bf7c89c342f82732760b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blacksheep-1.2.14-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/4.0.2 CPython/3.11.3

File hashes

Hashes for blacksheep-1.2.14-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 560f248270cd309e3c6465cd0d762e50fdb7a6266fd78b6adac1721107656b7e
MD5 f81b46da0971f9497352961e5f90bec7
BLAKE2b-256 9737d1bf84f4387c08a9cee0d4489a5b31644a55db5e47b525a30566137f44e4

See more details on using hashes here.

File details

Details for the file blacksheep-1.2.14-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for blacksheep-1.2.14-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0b695c0fe1a3a36fe8a416e3c10834042e8e736eae31dec281a484e02840d8d2
MD5 2d8eb15e7b981cd0f5921540f775c361
BLAKE2b-256 d44cd41894a74c3d42ea010f9ccf8e8d744446dfef4b49ee805c7ad790d98560

See more details on using hashes here.

File details

Details for the file blacksheep-1.2.14-cp39-cp39-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for blacksheep-1.2.14-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 35a2bd38ebc01a9b5fb53b8e7523cbe10b5a433a2705747e878e84d0decd2d35
MD5 840994f230eab30f97c0f190fb9ccde6
BLAKE2b-256 3f0b31a49002558fe9a1e1e42f1c26d1cd4d00034303ea156447aafe9622a5f6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: blacksheep-1.2.14-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/4.0.2 CPython/3.11.3

File hashes

Hashes for blacksheep-1.2.14-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 20c4287719ab0987526a7a41fe3e1518a507186b057115b3d15b0795a87781a6
MD5 4da56c41f199dde6ae75b43a933b5a47
BLAKE2b-256 64d944eeac193ea9ae84a7ae6bd360305cfb0722a0135a022f38d57f509af3ec

See more details on using hashes here.

File details

Details for the file blacksheep-1.2.14-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for blacksheep-1.2.14-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9dbee648578e11d573706dde5c068bb16b4ef0c3b5387e15976db5b0a07b800f
MD5 24b75d25ba31db31201a5aeccc04f40e
BLAKE2b-256 3f6749d8cf2fb35aaa63783ec8afb3518e8078986c26bc89e3957d2570847801

See more details on using hashes here.

File details

Details for the file blacksheep-1.2.14-cp38-cp38-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for blacksheep-1.2.14-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 ba8fa34195abe560a853b1fa0371c5429bd3c373d9bdddd06fc3e6e916eb5a47
MD5 a765807a877607d53683912d8bf112a3
BLAKE2b-256 c11df9a149c1f5e60eda4aff5a025bc2f6cb428b593671fbc985ba7a006dbb43

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