Fast web framework for Python asyncio
Project description
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.
pip install blacksheep
from datetime import datetime
from blacksheep import Application, get
app = Application()
@get("/")
async def home():
return f"Hello, World! {datetime.utcnow().isoformat()}"
Getting started using the CLI ✨
BlackSheep offers a CLI to bootstrap new projects rapidly.
To try it, first install the blacksheep-cli
package:
pip install blacksheep-cli
Then use the blacksheep create
command to bootstrap a project
using one of the supported templates.
The CLI includes a help, and supports custom templates, using the
same sources supported by Cookiecutter
.
Getting started with the documentation
The documentation offers getting started tutorials:
These project templates can be used to start new applications faster:
Requirements
Python: any version listed in the project's classifiers. The current list is:
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, 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 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")
@get("/")
async def only_for_admins():
...
@auth()
@get("/")
async def only_for_authenticated_users():
...
Since version 1.2.1
, BlackSheep implements:
Meaning that it is easy to integrate with services such as:
Refer to the documentation and to BlackSheep-Examples for more details and examples.
Web framework features
- ASGI compatibility
- Routing
- Request handlers can be defined as functions, or class methods
- Middlewares
- WebSocket
- Server-Sent Events (SSE)
- Built-in support for dependency injection
- Support for automatic binding of route and query parameters to request handlers methods calls
- Strategy to handle exceptions
- Strategy to handle authentication and authorization
- Built-in support for OpenID Connect authentication using OIDC discovery
- Built-in support for JWT Bearer authentication using OIDC discovery and other sources of JWKS
- Handlers normalization
- Serving static files
- Integration with Jinja2
- Support for serving SPAs that use HTML5 History API for client side routing
- Support for automatic generation of OpenAPI Documentation
- Strategy to handle CORS settings
- Sessions
- Support for automatic binding of
dataclasses
andpydantic
models to handle the request body payload expected by request handlers TestClient
class to simplify testing of applications- Anti Forgery validation to protect against Cross-Site Request Forgery (XSRF/CSRF) attacks
Client features
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: all versions included in the build matrix
- Ubuntu
- Windows 10
- macOS
Documentation
Please refer to the documentation website.
Communication
BlackSheep community in Gitter.
Branches
The main branch contains the currently developed version, which is version 2. The v1 branch contains version 1 of the web framework, for bugs fixes and maintenance.
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
Built Distributions
File details
Details for the file blacksheep-2.0.7.tar.gz
.
File metadata
- Download URL: blacksheep-2.0.7.tar.gz
- Upload date:
- Size: 1.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae192809c1e42de5a0d6230f1238d027641a8d889a4a9fb5349b7980f74afd88 |
|
MD5 | db44ba3e464392c6d78a01cf9254a86c |
|
BLAKE2b-256 | 3383d96bbc907a8c34272b1ae052b6e95fd3f38329198fc8e6a9a3a790091cf5 |
File details
Details for the file blacksheep-2.0.7-cp312-cp312-win_amd64.whl
.
File metadata
- Download URL: blacksheep-2.0.7-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb2594c101241d4a983085667898a6f43024024d9722f8a3df16150f4bdd5d39 |
|
MD5 | 864b67c5d82522a30be720280142218b |
|
BLAKE2b-256 | e79ee8b55a70b30b576397d9a48fceadf492c7a98c0dc171048be03976be7ecd |
File details
Details for the file blacksheep-2.0.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: blacksheep-2.0.7-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 5.1 MB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d451f4c18d7af1852ee6cc52ab7a0bb0c435d1d6cd6c22d4238d229400c25b8 |
|
MD5 | a0a87b5b9f036fef9c5edfc792f361f5 |
|
BLAKE2b-256 | 1c3b721bf625b7ccb3360b6fd95fc333cf044f01e3c6980fbce6022519bc59b5 |
File details
Details for the file blacksheep-2.0.7-cp312-cp312-macosx_10_9_universal2.whl
.
File metadata
- Download URL: blacksheep-2.0.7-cp312-cp312-macosx_10_9_universal2.whl
- Upload date:
- Size: 2.3 MB
- Tags: CPython 3.12, macOS 10.9+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 36f5c7cd30b36bc076aed972ba68645acde0af59e6c2a5b6ab301bc27ec7a01e |
|
MD5 | 1e477e1953cf36353f7a8f10acdeaf3d |
|
BLAKE2b-256 | 8e6f05b6ac2820626d8124c5d055242c678252f630ea1755ee021e9f348415f2 |
File details
Details for the file blacksheep-2.0.7-cp311-cp311-win_amd64.whl
.
File metadata
- Download URL: blacksheep-2.0.7-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | aff0e2503c5fc7237475533d788153c01acaf4d00bb7a0a62d7fcb526ed6ec62 |
|
MD5 | 15a2f3c6e8664b86fa762ceec9754c26 |
|
BLAKE2b-256 | c7cf7b61f88592c9793746f603de0fe55b3364fc0ca35edf9ce4e77912e836a7 |
File details
Details for the file blacksheep-2.0.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: blacksheep-2.0.7-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 4.7 MB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bcee81f0238ee5c10c7fc4d45a8a49cd012188ab9756c695922aa79c73cd1f5c |
|
MD5 | 4e120adf7a6c6dd476272543eb4985bb |
|
BLAKE2b-256 | 021355a7d82f3937e5da7f8d5487444f0c134c37d5b27e3f0d56825f9b0969bf |
File details
Details for the file blacksheep-2.0.7-cp311-cp311-macosx_10_9_universal2.whl
.
File metadata
- Download URL: blacksheep-2.0.7-cp311-cp311-macosx_10_9_universal2.whl
- Upload date:
- Size: 2.4 MB
- Tags: CPython 3.11, macOS 10.9+ universal2 (ARM64, x86-64)
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 676285158a3d3bdd77e5bf48805e6359d86ef718ad21895ad239c7f32b11e110 |
|
MD5 | 7f7199b105eb547de3ed25a4b913b938 |
|
BLAKE2b-256 | 52682854610aea9fe317df6675a27c9d286f5b1073b5eb903c93956e8f422f6e |
File details
Details for the file blacksheep-2.0.7-cp310-cp310-win_amd64.whl
.
File metadata
- Download URL: blacksheep-2.0.7-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd97981fb7e9ee22416df14a17cb88307202e49d90ce5fdd9f99d531c3eb3c2a |
|
MD5 | c95d1cb151136c8308cf6e6ba98971e6 |
|
BLAKE2b-256 | 12c8ab532dfd0d5ae78811a83e3a2b12227f238e3097ef779c0230bbe814ee15 |
File details
Details for the file blacksheep-2.0.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: blacksheep-2.0.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 4.4 MB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b0ef73603ccbb08cb161078f174be059c7ab6881534899891b76a46afde81f00 |
|
MD5 | 900466c5dbf8c6986d0befc523477c5a |
|
BLAKE2b-256 | 21a6dbc51a9b92ff645e8b23878c1c86ff2f5c3190070641394726f918e71a03 |
File details
Details for the file blacksheep-2.0.7-cp310-cp310-macosx_11_0_x86_64.whl
.
File metadata
- Download URL: blacksheep-2.0.7-cp310-cp310-macosx_11_0_x86_64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 3.10, macOS 11.0+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d707010d71f9a7d048966cac9f8e21eb772c7a04b54755ae9a2e51b3e888bea |
|
MD5 | ea51f849524ba6cbf9d7ffdddf14b20d |
|
BLAKE2b-256 | 0b7f50b1d37f7418d14687d896bd662f27b6d3892b7d5b47dae93bf59db2bc8f |
File details
Details for the file blacksheep-2.0.7-cp39-cp39-win_amd64.whl
.
File metadata
- Download URL: blacksheep-2.0.7-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc6ab2376aa76189dd4c6e588e97aa67843d5df7ee95f8d4b8255d6fc1deab52 |
|
MD5 | 962b1c4218a848cb2b9bc87ca92f1483 |
|
BLAKE2b-256 | b6e936b180b14d53ff014272460a25fca2353e414aa5de457fd3f646e20107ca |
File details
Details for the file blacksheep-2.0.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: blacksheep-2.0.7-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 4.4 MB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81cf3c2adab437f1d8e25c54b9920434b47db78463a46108dfacc429bd52b286 |
|
MD5 | c2280ab55423823248d6e736230511ed |
|
BLAKE2b-256 | e57c5870a2061acdd659e72e769a06f6cbd725bbcb3789c7f94b79a101df8fd2 |
File details
Details for the file blacksheep-2.0.7-cp39-cp39-macosx_11_0_x86_64.whl
.
File metadata
- Download URL: blacksheep-2.0.7-cp39-cp39-macosx_11_0_x86_64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 3.9, macOS 11.0+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0a8036eb9475d9387da6b9d21470e22a1acd5da17ea973f0e5f378846f2344d |
|
MD5 | 68d21d41d8da4526b67be53249b58325 |
|
BLAKE2b-256 | 07c4a21c1f9a0716c4ed08882abda80b38dfcad5c493bc3502d19adba0ba4293 |
File details
Details for the file blacksheep-2.0.7-cp38-cp38-win_amd64.whl
.
File metadata
- Download URL: blacksheep-2.0.7-cp38-cp38-win_amd64.whl
- Upload date:
- Size: 1.7 MB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f4cc63f9f18c56ae349d2f03c991fbc5affc6b871674c407b60d574be8683a6 |
|
MD5 | 0062ac9ee27f958d129b761b880f797d |
|
BLAKE2b-256 | ffb149f6e740f769c3065fc7573f45392bc44bf15835bf9b4863c2ca6749a24f |
File details
Details for the file blacksheep-2.0.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
.
File metadata
- Download URL: blacksheep-2.0.7-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 4.7 MB
- Tags: CPython 3.8, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb826d920e5fc75e44f36de36b59b891d2c34004091083f8525c2373d0d4cf75 |
|
MD5 | 9d94e99c456eb8120d6d37da582f190c |
|
BLAKE2b-256 | 4bc69e23b4816983188c5567f274ef1d5788f698efd1d5b8762246f4459ca48b |
File details
Details for the file blacksheep-2.0.7-cp38-cp38-macosx_11_0_x86_64.whl
.
File metadata
- Download URL: blacksheep-2.0.7-cp38-cp38-macosx_11_0_x86_64.whl
- Upload date:
- Size: 1.8 MB
- Tags: CPython 3.8, macOS 11.0+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.11.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f02abc213aa95f8d8453609b3e601da785f11712396a65f3945e5b9b66d20f1b |
|
MD5 | 35a95c345f356faead025017b3d69f90 |
|
BLAKE2b-256 | 4ee51d21aa11ed8108765f11649a19f4d670154447a4d9ef58598d66e570e5a5 |