Skip to main content

ZipLine

ZipLine is a simple asyncronous ASGI web framework for Python. It is designed to be simple and easy to use, while still being powerful and flexible.

Quick Start

from zipline import ZipLine

app = ZipLine()

@app.route("/")
async def home(request):
    planet = request.query.get("planet")
    return f"Hello, {planet}!"
uvicorn my_awesome_project:app
curl http://localhost:8000/?planet=Earth

Handlers

a ZipLine handler is a simple async function that takes a request object and returns a response, or throws an exception.

A response can be bytes, str, dict, or the ZipeLine Response object.

If a dict is returned, it will be serialized to JSON.

If an Exception is thrown, it will be caught and handled by the framework, returning a basic error response.

Middleware

Zipline middleware is inspired by Express.js. Any number of handler functions can be added to the middleware stack.

Each middleware function is just another ZipLine Handler.

Middleware functions are called in the order they are added to the stack, and pass along their context to the next handler.

The first handler in the stack to return something other than a Request object (including Exception) will short-circuit the stack and return the response.

from zipline import ZipLine


# middleware functions
def auth_middleware(request):
    if request.headers.get("Authorization") == "Bearer 1234":
        is_authed = True
    else:
        is_authed = False
    return request, { "is_authed": is_authed }

def auth_guard(request, ctx):
    if not ctx.get("is_authed"):
        raise Exception("Unauthorized")


app = ZipLine()

# apply middleware to all routes
app.middleware(auth_middleware)

@app.get("/profile")
@app.middleware([auth_guard])
async def user_profile(request):
    return "Hello, World!"

Dependency Injection

Like with middeleware, ZipLine supports dependency injection at the route level or application level. Dependencies are passed to the handler function as keyword arguments.

from zipline import ZipLine

class LoggingService:
    def log_request(self, request):
        print(f"Request to {request.url}")

class UserService:
    def __init__(self):
        self.connection = "Connected to database"

    def get_user():
        return "User"

app = ZipLine()

# available to all routes
app.inject(LoggingService)

@app.route("/")
@app.inject(UserService, name="user_service")
async def home(request, user_service: UserService, logger: LoggingService):
    logger.log_request(request)
    return user_service.get_user()

Routing

Like Express.js, ZipLine supports multiple, nested routers.

from zipline import ZipLine, Router

app = ZipLine()

user_router = Router("/user")

@user_router.get("/:id")
async def get_user(request):
    return f"User {request.path_params.get('id')}"

app.router(user_router)

Download files

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

Source Distribution

ziplineio-0.1.3.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

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

ziplineio-0.1.3-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file ziplineio-0.1.3.tar.gz.

File metadata

  • Download URL: ziplineio-0.1.3.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for ziplineio-0.1.3.tar.gz
Algorithm Hash digest
SHA256 564d9b84486239c8e2360b5b4b55d33753823b87af523be11caa9a51dc6d9a65
MD5 c95924185dc1c5897ea8e48741fd6888
BLAKE2b-256 9ec9e65dd8705386625895fa3ec7f5adc634dd52825f368bcb42b48a10e8383c

See more details on using hashes here.

File details

Details for the file ziplineio-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: ziplineio-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 3.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for ziplineio-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 930a9001f0c01a9e2f388a86a3e06974113e8131cea5d517b27803d765208437
MD5 44bccba1036e1666ee31b7cb16f2698c
BLAKE2b-256 6c2098ff8c28744c1006d85872fe3d5077734aa45295d624f01966b22cf31bd0

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 Sentry Error logging StatusPage Status page