Skip to main content

Powerful and exquisite WSGI/ASGI framework/toolkit.

Project description

BáiZé

Codecov PyPI - Python Version

Powerful and exquisite WSGI/ASGI framework/toolkit.

The minimize implementation of methods required in the Web framework. No redundant implementation means that you can freely customize functions without considering the conflict with baize's own implementation.

Under the ASGI/WSGI protocol, the interface of the request object and the response object is almost the same, only need to add or delete await in the appropriate place. In addition, it should be noted that ASGI supports WebSocket but WSGI does not.

Install

pip install -U baize

Document and other website

BáiZé Document

If you have questions or idea, you can send it to Discussions.

Quick Start

A short example for WSGI application, if you don't know what is WSGI, please read PEP3333.

import time
from typing import Callable
from baize.wsgi import (
    middleware,
    request_response,
    Router,
    Request,
    Response,
    PlainTextResponse,
)


@middleware
def timer(request: Request, next_call: Callable[[Request], Response]) -> Response:
    start_time = time.time()
    response = next_call(request)
    end_time = time.time()
    response.headers["x-time"] = str(round((end_time - start_time) * 1000))
    return response


@request_response
@timer
def sayhi(request: Request) -> Response:
    return PlainTextResponse("hi, " + request.path_params["name"])


@request_response
@timer
def echo(request: Request) -> Response:
    return PlainTextResponse(request.body)


application = Router(
    ("/", PlainTextResponse("homepage")),
    ("/echo", echo),
    ("/sayhi/{name}", sayhi),
)


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(application, interface="wsgi", port=8000)

A short example for ASGI application, if you don't know what is ASGI, please read ASGI Documention.

import time
from typing import Awaitable, Callable
from baize.asgi import (
    middleware,
    request_response,
    Router,
    Request,
    Response,
    PlainTextResponse,
)


@middleware
async def timer(
    request: Request, next_call: Callable[[Request], Awaitable[Response]]
) -> Response:
    start_time = time.time()
    response = await next_call(request)
    end_time = time.time()
    response.headers["x-time"] = str(round((end_time - start_time) * 1000))
    return response


@request_response
@timer
async def sayhi(request: Request) -> Response:
    return PlainTextResponse("hi, " + request.path_params["name"])


@request_response
@timer
async def echo(request: Request) -> Response:
    return PlainTextResponse(await request.body)


application = Router(
    ("/", PlainTextResponse("homepage")),
    ("/echo", echo),
    ("/sayhi/{name}", sayhi),
)


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(application, interface="asgi3", port=8000)

License

Apache-2.0.

Project details


Download files

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

Source Distribution

baize-0.20.8.tar.gz (57.4 kB view hashes)

Uploaded Source

Built Distributions

baize-0.20.8-py3-none-any.whl (48.5 kB view hashes)

Uploaded Python 3

baize-0.20.8-cp311-cp311-win_amd64.whl (328.1 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

baize-0.20.8-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (732.2 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

baize-0.20.8-cp311-cp311-macosx_10_9_universal2.whl (788.7 kB view hashes)

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

baize-0.20.8-cp310-cp310-win_amd64.whl (328.9 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

baize-0.20.8-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (741.1 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

baize-0.20.8-cp310-cp310-macosx_11_0_x86_64.whl (427.9 kB view hashes)

Uploaded CPython 3.10 macOS 11.0+ x86-64

baize-0.20.8-cp39-cp39-win_amd64.whl (328.9 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

baize-0.20.8-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (739.6 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

baize-0.20.8-cp39-cp39-macosx_11_0_x86_64.whl (427.6 kB view hashes)

Uploaded CPython 3.9 macOS 11.0+ x86-64

baize-0.20.8-cp38-cp38-win_amd64.whl (325.0 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

baize-0.20.8-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (725.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64

baize-0.20.8-cp38-cp38-macosx_11_0_x86_64.whl (420.6 kB view hashes)

Uploaded CPython 3.8 macOS 11.0+ x86-64

baize-0.20.8-cp37-cp37m-win_amd64.whl (314.3 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

baize-0.20.8-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (571.5 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64

baize-0.20.8-cp37-cp37m-macosx_11_0_x86_64.whl (402.5 kB view hashes)

Uploaded CPython 3.7m macOS 11.0+ x86-64

Supported by

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