Giter VIP home page Giter VIP logo

baize's Introduction

BáiZé

Codecov PyPI - Python Version

Powerful and exquisite WSGI/ASGI framework/toolkit. Use MyPyC to improve code running speed.

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

Or install from GitHub master branch

pip install -U git+https://github.com/abersheeran/[email protected]

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.

baize's People

Contributors

abersheeran avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.