Skip to main content

Yet another ASGI toolkit that delivers

Project description

Lilya

Lilya

🚀 Yet another ASGI toolkit that delivers. 🚀

Test Suite Package version Supported Python versions CodSpeed


Documentation: https://lilya.dev 📚

Source Code: https://github.com/dymmond/lilya

The official supported version is always the latest released.


Motivation

In the world of ASGI, alternatives are always great to have and no tool serves it all. Lilya, coming from the great inspirations of the ones who paved the way, its a more simple, accurate fast and easy to use Python toolkit/framework that aims for simplicity.

A lot of times you won't be needing a fully fledge Python web framework as it can be overwhelming for some simple tasks, instead you would prefer a simple ASGI toolkit that helps you designing production ready, fast, elegant, maintainable and modular applications.

This is where Lilya places itself.

Almost no hard dependencies, 100% pythonic, fully typed and ready for production.

What does it bring?

Lilya comes bearing fruits.

  • A lightweight ASGI toolkit/framework.
  • Support for HTTP/WebSocket.
  • Tasks (in ASGI known as background tasks).
  • Lifespan events (on_startup/on_shutdown and lifespan).
  • Native permission system.
  • Middlewares (Compressor, CSRF, Session, CORS...).
  • A native and optional client.
  • Directive management control system for any custom scripts to run inside the application.
  • Little hard dependencies.
  • Compatibility with trio and asyncio.
  • Dynamic routing system with the help of the native Include and minimum boilerplate.
  • Native settings system. No more bloated instances.
  • Native dependency injection that is extremely scalable, clean and fast.
  • Dynamic, native and fast custom serializers.

Installation

If you want just the toolkit/framework.

$ pip install lilya

If you want the Lilya client (for scaffolds, and other useful tools)

$ pip install lilya[standard]

If you wish to use to extra functionalities such as the shell or directives (project scaffold generation to speed up).

$ pip install lilya[cli,ipython] # for ipython shell
$ pip install lilya[cli,ptpython] # for ptpython shell

You can learn more about the client in the documentation.

Or if you want to install everything that will allow you to use all the resources of Lilya, such as some specific middlewares.

$ pip install lilya[all]

Additional

You would want to install an ASGI server such as palfrey, uvicorn or hypercorn as well.

Quickstart

If you are familiar with other Python frameworks and toolkits, Lilya provides you with the same feeling.

A Lilya also uses a native settings system which is something that can be extremely useful for any application.

The normal way

from lilya.apps import Lilya
from lilya.requests import Request
from lilya.responses import Ok
from lilya.routing import Path


async def welcome():
    return Ok({"message": "Welcome to Lilya"})


async def user(user: str):
    return Ok({"message": f"Welcome to Lilya, {user}"})


async def user_in_request(request: Request):
    user = request.path_params["user"]
    return Ok({"message": f"Welcome to Lilya, {user}"})


app = Lilya(
    routes=[
        Path("/{user}", user),
        Path("/in-request/{user}", user_in_request),
        Path("/", welcome),
    ]
)

Using Lilya to decorate

from lilya.apps import Lilya
from lilya.requests import Request
from lilya.responses import Ok

app = Lilya()


@app.get("/")
async def welcome():
    return Ok({"message": "Welcome to Lilya"})


@app.get("/{user}")
async def user(user: str):
    return Ok({"message": f"Welcome to Lilya, {user}"})


@app.get("/in-request/{user}")
async def user_in_request(request: Request):
    user = request.path_params["user"]
    return Ok({"message": f"Welcome to Lilya, {user}"})

Is this simple. Although there is a lot to unwrap here. Did you notice the path /{user} not only does not require a request to be declared and instead, receives a user: str?

Well, Lilya does a lot of internal magic for you. If you don't declare a request, that is not a problem as it will only pass it if its there.

If you have the path parameter declared in the function handler as well, Lilya will automatically search for the parameters declared and match them against the path parameters declared in the Path and inject them for you.

Pretty cool, right? This is just scratching the surface.

Definitions

Lilya can be considered a framework or a toolkit and the reasoning for it its because each component such as middlewares, permissions, Path, Router... can be seen as an independent ASGI application.

In other words, you can build a middleware or a permission and share those with any other existing ASGI framework out there, meaning, you could design a Lilya application, middlewares, permissions and any other component and re-use them in Esmerald or FastAPI or any other, really.

Lilya is not a full-fledge framework like Esmerald or FastAPI, instead its a lightweight toolkit/framework that can be used to build those as well as working on its own.

Example

from lilya.exceptions import PermissionDenied
from lilya.protocols.permissions import PermissionProtocol
from lilya.requests import Request
from lilya.types import ASGIApp, Receive, Scope, Send


class AllowAccess(PermissionProtocol):
    def __init__(self, app: ASGIApp, *args, **kwargs):
        super().__init__(app, *args, **kwargs)
        self.app = app

    async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
        request = Request(scope=scope, receive=receive, send=send)

        if "allow-admin" in request.headers:
            await self.app(scope, receive, send)
            return
        raise PermissionDenied()

Run the application

To run the application from the example.

$ palfrey myapp:app
INFO:     Started server process [140552]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Listening on ('127.0.0.1', 8000)

Powered by

Worth mentioning who is helping us.

JetBrains

JetBrains logo.

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

lilya-0.25.0.tar.gz (299.8 kB view details)

Uploaded Source

Built Distribution

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

lilya-0.25.0-py3-none-any.whl (420.7 kB view details)

Uploaded Python 3

File details

Details for the file lilya-0.25.0.tar.gz.

File metadata

  • Download URL: lilya-0.25.0.tar.gz
  • Upload date:
  • Size: 299.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.10.19 HTTPX/0.28.1

File hashes

Hashes for lilya-0.25.0.tar.gz
Algorithm Hash digest
SHA256 f499c9128f13a28f9c35b42efd319d3748d1b526209ff22573771bbd2da60019
MD5 dc11206cd9d69f7221bd0973b4375a96
BLAKE2b-256 793e69d04d76fb804a4863f1b1f565f9b5882c60803d9946ae14d48765b768e4

See more details on using hashes here.

File details

Details for the file lilya-0.25.0-py3-none-any.whl.

File metadata

  • Download URL: lilya-0.25.0-py3-none-any.whl
  • Upload date:
  • Size: 420.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Hatch/1.16.5 cpython/3.10.19 HTTPX/0.28.1

File hashes

Hashes for lilya-0.25.0-py3-none-any.whl
Algorithm Hash digest
SHA256 76cfc4cb2933b05088258d4d7fc5483867d0f2e421a2885486ecde43dbb02065
MD5 af296f8e24562ef9d4d401f4a4d7acbe
BLAKE2b-256 3d5104e1a76733f229764e91d066d6721939ef316918c774a9e54f9adab4b4cd

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