Skip to main content

starlette-plugins – A helper to make plugins faster with Starlette

Starlette-Plugins is a helper to write plugins easier

Tests Status PYPI Version

Requirements

  • python >= 3.7

Installation

starlette-plugins should be installed using pip:

pip install starlette-plugins

Usage

Let’s imagine that we need to write Starlette plugins for Peewee ORM.

from aiopeewee import db_url

from starlette_plugins import StarlettePlugin

class Peewee(StarlettePlugin):

    # Give your plugin a name
    name = 'db'

    # Prepare a default configuration
    config = {
        'url': 'sqlite+async:///db.sqlite',
        'connection_params': {},
    }

def __init__(self, app=None, **settings):
    super(Peewee, self).__init__(app, **settings)
    self.database = None

def setup(self, app, **settings):
    """Setup the plugin."""
    super(Peewee, self).setup(app, **settings)
    self.database = db_url.connect(self.config.url, **self.config.connection_params)

async def middleware(self, scope, receive, send, app):
    """An optional ASGI middleware."""
    try:
        await self.database.connect_async()
        await app(scope, receive, send)
    finally:
        await self.database.close_async()

async def shutdown(self, scope):
    """ The methods are supported: `startup`, `shutdown`."""
    if hasattr(self.database, 'close_all'):
        self.database.close_all()

Use the plugin

from starlette.applications import Starlette


db = Peewee(url='postgres+async://database')

app = Starlette()
db.setup(app)

Bug tracker

If you have any suggestions, bug reports or annoyances please report them to the issue tracker at https://github.com/klen/starlette-plugins/issues

Contributing

Development of the project happens at: https://github.com/klen/starlette-plugins

License

Licensed under a MIT license.

Metadata

Release files for starlette-plugins 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for starlette-plugins 0.0.7
File Interpreter ABI Platform
starlette_plugins-0.0.7-py3-none-any.whl Python 3 none any Details

Release files / starlette_plugins-0.0.7-py3-none-any.whl

Download URL starlette_plugins-0.0.7-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4cb8b6094b30e9569e8a82a6ac438c03d5fe766ee2b32e3e0a469d242369da9c
BLAKE2b-256 checksum
How to use checksums
e8135ab925578c033a933d75b6ca44e8cbd014e23f8be478cc9a5a6e5063842a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.0

Release history Release notifications | RSS feed

This release

0.0.7 This release

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page