starlette-bridge


Namestarlette-bridge JSON
Version 0.3.0 PyPI version JSON
download
home_page
SummaryThe Starlette events bridge that you need.
upload_time2023-06-12 17:11:21
maintainer
docs_urlNone
author
requires_python>=3.8
license
keywords api asgi esmerald fastapi framework http machine learning ml openapi pydantic rest starlette websocket
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Starlette Bridge

<p align="center">
  <a href="https://starlette-bridge.tarsild.io"><img width="420px" src="https://www.starlette.io/img/starlette.png" alt='Starlette Bridge'></a>
</p>

<p align="center">
    <em>🚀 The Starlette events bridge that you need. 🚀</em>
</p>

<p align="center">
<a href="https://github.com/tarsil/starlette-bridge/workflows/Test%20Suite/badge.svg?event=push&branch=main" target="_blank">
    <img src="https://github.com/tarsil/starlette-bridge/workflows/Test%20Suite/badge.svg?event=push&branch=main" alt="Test Suite">
</a>

<a href="https://pypi.org/project/starlette-bridge" target="_blank">
    <img src="https://img.shields.io/pypi/v/starlette-bridge?color=%2334D058&label=pypi%20package" alt="Package version">
</a>

<a href="https://pypi.org/project/starlette-bridge" target="_blank">
    <img src="https://img.shields.io/pypi/pyversions/starlette-bridge.svg?color=%2334D058" alt="Supported Python versions">
</a>
</p>

---

**Documentation**: [https://starlette-bridge.tarsild.io](https://starlette-bridge.tarsild.io) 📚

**Source Code**: [https://github.com/tarsil/starlette-bridge](https://github.com/tarsil/starlette-bridge)

---

## Motivation

Starlette has evolved and it will keep on growing. In the release 0.26+ it was announced the
`on_startup` and `on_shutdown` events would be removed in favour of the newly `lifepsan` and those
would be officially removed in the release 1.0.

The problem is the fact that there are a lof od packages out there still using the old ways of
using the events and this change will introduce breaking changes.

This bridge will make sure that doesn't happen and you can still use the old form of using the
`on_startup` and `on_shutdown` without breaking the `lifespan` from Starlette.

What `Starlette Bridge` does for you is simple. It gets the `on_startup` and `on_shutdown` events
in the same fashion you could do before and internally generates the newly `lifespan` for starlette.

That simple. Keeping the old syntax intact and using the newly `lifespan`.

## Installation

To install Starlette Bridge, simply run:

```shell
$ pip install starlette-bridge
```

## How to use

This is actually very simple to do it. You don't need to do anything particularly difficult, in
fact, you only need to update where your Starlette object comes from.

```python hl_lines="1"
from starlette_bridge import Starlette

app = Starlette()
```

And that is pretty much it.

### How does it work

Starlette bridge simply maps your `on_startup` and `on_shutdown` events and converts them into
the new `lifespan` async generator from `Starlette`.

This way you can continue to use your preferred way of assembling the events while maintaining
the new structure required by Starlette for managing events.

### on_event and add_event_handler

These two pieces of functionality are also supported by the bridge making sure that what you had
in the past, still remains working as is without changing the syntax.

Let us see an example how it works. We will be using [Starlette Bridge](https://saffier.tarsild.io) because
already contains events we want to use.

#### on_startup/on_shutdown

Using the `on_startup` and `on_shutdown`.

```python hl_lines="3 10-11"
from saffier import Database, Registry

from starlette_bridge import Starlette

database = Database("sqlite:///db.sqlite")
models = Registry(database=database)


app = Starlette(
    on_startup=[database.connect],
    on_shutdown=[database.disconnect],
)
```

#### Lifespan

You can, of course, use the lifespan as well.

```python hl_lines="5 20"
from contextlib import asynccontextmanager

from saffier import Database, Registry

from starlette_bridge import Starlette

database = Database("sqlite:///db.sqlite")
models = Registry(database=database)


@asynccontextmanager
async def lifespan(app: Starlette):
    # On startup
    await database.connect()
    yield
    # On shutdown
    await database.disconnect()


app = Starlette(lifespan=lifespan)
```

#### on_event and add_event_handler

As mentioned before, those two functionalities are also available.

##### on_event

```python hl_lines="3 12 17"
from saffier import Database, Registry

from starlette_bridge import Starlette

database = Database("sqlite:///db.sqlite")
models = Registry(database=database)


app = Starlette()


@app.on_event("startup")
async def start_database():
    await database.connect()


@app.on_event("shutdown")
async def close_database():
    await database.disconnect()
```

##### add_event_handler

```python hl_lines="3 10-11"
from saffier import Database, Registry

from starlette_bridge import Starlette

database = Database("sqlite:///db.sqlite")
models = Registry(database=database)


app = Starlette()
app.add_event_handler("startup", database.connect)
app.add_event_handler("shutdown", database.disconnect)
```

## Notes

This is from the same author of [Esmerald](https://esmerald.dev),
[Saffier](https://saffier.tarsild.io) and [Asyncz](https://asyncz.tarsild.io). Have a look around
those techologies as well 😄.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "starlette-bridge",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "",
    "keywords": "api,asgi,esmerald,fastapi,framework,http,machine learning,ml,openapi,pydantic,rest,starlette,websocket",
    "author": "",
    "author_email": "Tiago Silva <tiago.arasilva@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/9a/d9/d3d5f6b7aa78b7423d31dec30913f1bfb6dbed8ca96948b30dea1fb7f898/starlette_bridge-0.3.0.tar.gz",
    "platform": null,
    "description": "# Starlette Bridge\n\n<p align=\"center\">\n  <a href=\"https://starlette-bridge.tarsild.io\"><img width=\"420px\" src=\"https://www.starlette.io/img/starlette.png\" alt='Starlette Bridge'></a>\n</p>\n\n<p align=\"center\">\n    <em>\ud83d\ude80 The Starlette events bridge that you need. \ud83d\ude80</em>\n</p>\n\n<p align=\"center\">\n<a href=\"https://github.com/tarsil/starlette-bridge/workflows/Test%20Suite/badge.svg?event=push&branch=main\" target=\"_blank\">\n    <img src=\"https://github.com/tarsil/starlette-bridge/workflows/Test%20Suite/badge.svg?event=push&branch=main\" alt=\"Test Suite\">\n</a>\n\n<a href=\"https://pypi.org/project/starlette-bridge\" target=\"_blank\">\n    <img src=\"https://img.shields.io/pypi/v/starlette-bridge?color=%2334D058&label=pypi%20package\" alt=\"Package version\">\n</a>\n\n<a href=\"https://pypi.org/project/starlette-bridge\" target=\"_blank\">\n    <img src=\"https://img.shields.io/pypi/pyversions/starlette-bridge.svg?color=%2334D058\" alt=\"Supported Python versions\">\n</a>\n</p>\n\n---\n\n**Documentation**: [https://starlette-bridge.tarsild.io](https://starlette-bridge.tarsild.io) \ud83d\udcda\n\n**Source Code**: [https://github.com/tarsil/starlette-bridge](https://github.com/tarsil/starlette-bridge)\n\n---\n\n## Motivation\n\nStarlette has evolved and it will keep on growing. In the release 0.26+ it was announced the\n`on_startup` and `on_shutdown` events would be removed in favour of the newly `lifepsan` and those\nwould be officially removed in the release 1.0.\n\nThe problem is the fact that there are a lof od packages out there still using the old ways of\nusing the events and this change will introduce breaking changes.\n\nThis bridge will make sure that doesn't happen and you can still use the old form of using the\n`on_startup` and `on_shutdown` without breaking the `lifespan` from Starlette.\n\nWhat `Starlette Bridge` does for you is simple. It gets the `on_startup` and `on_shutdown` events\nin the same fashion you could do before and internally generates the newly `lifespan` for starlette.\n\nThat simple. Keeping the old syntax intact and using the newly `lifespan`.\n\n## Installation\n\nTo install Starlette Bridge, simply run:\n\n```shell\n$ pip install starlette-bridge\n```\n\n## How to use\n\nThis is actually very simple to do it. You don't need to do anything particularly difficult, in\nfact, you only need to update where your Starlette object comes from.\n\n```python hl_lines=\"1\"\nfrom starlette_bridge import Starlette\n\napp = Starlette()\n```\n\nAnd that is pretty much it.\n\n### How does it work\n\nStarlette bridge simply maps your `on_startup` and `on_shutdown` events and converts them into\nthe new `lifespan` async generator from `Starlette`.\n\nThis way you can continue to use your preferred way of assembling the events while maintaining\nthe new structure required by Starlette for managing events.\n\n### on_event and add_event_handler\n\nThese two pieces of functionality are also supported by the bridge making sure that what you had\nin the past, still remains working as is without changing the syntax.\n\nLet us see an example how it works. We will be using [Starlette Bridge](https://saffier.tarsild.io) because\nalready contains events we want to use.\n\n#### on_startup/on_shutdown\n\nUsing the `on_startup` and `on_shutdown`.\n\n```python hl_lines=\"3 10-11\"\nfrom saffier import Database, Registry\n\nfrom starlette_bridge import Starlette\n\ndatabase = Database(\"sqlite:///db.sqlite\")\nmodels = Registry(database=database)\n\n\napp = Starlette(\n    on_startup=[database.connect],\n    on_shutdown=[database.disconnect],\n)\n```\n\n#### Lifespan\n\nYou can, of course, use the lifespan as well.\n\n```python hl_lines=\"5 20\"\nfrom contextlib import asynccontextmanager\n\nfrom saffier import Database, Registry\n\nfrom starlette_bridge import Starlette\n\ndatabase = Database(\"sqlite:///db.sqlite\")\nmodels = Registry(database=database)\n\n\n@asynccontextmanager\nasync def lifespan(app: Starlette):\n    # On startup\n    await database.connect()\n    yield\n    # On shutdown\n    await database.disconnect()\n\n\napp = Starlette(lifespan=lifespan)\n```\n\n#### on_event and add_event_handler\n\nAs mentioned before, those two functionalities are also available.\n\n##### on_event\n\n```python hl_lines=\"3 12 17\"\nfrom saffier import Database, Registry\n\nfrom starlette_bridge import Starlette\n\ndatabase = Database(\"sqlite:///db.sqlite\")\nmodels = Registry(database=database)\n\n\napp = Starlette()\n\n\n@app.on_event(\"startup\")\nasync def start_database():\n    await database.connect()\n\n\n@app.on_event(\"shutdown\")\nasync def close_database():\n    await database.disconnect()\n```\n\n##### add_event_handler\n\n```python hl_lines=\"3 10-11\"\nfrom saffier import Database, Registry\n\nfrom starlette_bridge import Starlette\n\ndatabase = Database(\"sqlite:///db.sqlite\")\nmodels = Registry(database=database)\n\n\napp = Starlette()\napp.add_event_handler(\"startup\", database.connect)\napp.add_event_handler(\"shutdown\", database.disconnect)\n```\n\n## Notes\n\nThis is from the same author of [Esmerald](https://esmerald.dev),\n[Saffier](https://saffier.tarsild.io) and [Asyncz](https://asyncz.tarsild.io). Have a look around\nthose techologies as well \ud83d\ude04.\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "The Starlette events bridge that you need.",
    "version": "0.3.0",
    "project_urls": {
        "Changelog": "https://starlette-bridge.tarsild.io/release-notes/",
        "Documentation": "https://starlette-bridge.tarsild.io/",
        "Funding": "https://github.com/sponsors/tarsil",
        "Homepage": "https://github.com/tarsil/starlette-bridge",
        "Source": "https://github.com/tarsil/starlette-bridge"
    },
    "split_keywords": [
        "api",
        "asgi",
        "esmerald",
        "fastapi",
        "framework",
        "http",
        "machine learning",
        "ml",
        "openapi",
        "pydantic",
        "rest",
        "starlette",
        "websocket"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a904032159a8f219369c72df6f8be11ba48fe2fba06a7d2349a2e482089d6e8a",
                "md5": "c507120f5436e3f1ca05ba329d0bd670",
                "sha256": "2871b1cb81016223405c2176b7d4b2ccc3e6f56e2c4b6e6e2deb34642355e017"
            },
            "downloads": -1,
            "filename": "starlette_bridge-0.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "c507120f5436e3f1ca05ba329d0bd670",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 7155,
            "upload_time": "2023-06-12T17:11:20",
            "upload_time_iso_8601": "2023-06-12T17:11:20.126238Z",
            "url": "https://files.pythonhosted.org/packages/a9/04/032159a8f219369c72df6f8be11ba48fe2fba06a7d2349a2e482089d6e8a/starlette_bridge-0.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9ad9d3d5f6b7aa78b7423d31dec30913f1bfb6dbed8ca96948b30dea1fb7f898",
                "md5": "220878ebbaf1e6f992ccce1228aedc76",
                "sha256": "ba2ded31e27aaeede581d16499121b9f34a089a4bee5599738418ecc4f9ba30b"
            },
            "downloads": -1,
            "filename": "starlette_bridge-0.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "220878ebbaf1e6f992ccce1228aedc76",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 6309,
            "upload_time": "2023-06-12T17:11:21",
            "upload_time_iso_8601": "2023-06-12T17:11:21.865473Z",
            "url": "https://files.pythonhosted.org/packages/9a/d9/d3d5f6b7aa78b7423d31dec30913f1bfb6dbed8ca96948b30dea1fb7f898/starlette_bridge-0.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-06-12 17:11:21",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sponsors",
    "github_project": "tarsil",
    "github_not_found": true,
    "lcname": "starlette-bridge"
}
        
Elapsed time: 0.07458s