Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Add ASGI gateway #236

Merged
merged 2 commits into from
Oct 13, 2021
Merged

Add ASGI gateway #236

merged 2 commits into from
Oct 13, 2021

Conversation

oeway
Copy link
Contributor

@oeway oeway commented Oct 13, 2021

This PR enables making ASGI apps with fastapi etc. in Pyodide. Flask is also possible with a converter.

For example, you can make a web server with ImJoy plugin:

from imjoy import api
from fastapi import FastAPI

def create_fastapi_app():
    app = FastAPI()

    @app.get("/")
    async def root():
        return {"message": "Hello World"}

    return app

async def setup():
    # Registering fastapi app
    fastapi_app = create_fastapi_app()
    async def serve_fastapi(args):
        await fastapi_app(args["scope"], args["receive"], args["send"])
    await api.register_service({
        "name": "hello-fastapi",
        "type": "ASGI",
        "serve": serve_fastapi,
        "visibility": "public"
    })

api.export({"setup": setup})

Assuming the plugin is connected in a workspace called public, the above will result in a web service served under: https://127.0.0.1:9527/public/app/hello-fastapi/.

@github-actions github-actions bot added the enhancement New feature or request label Oct 13, 2021
@oeway oeway merged commit a76fae9 into master Oct 13, 2021
@oeway oeway deleted the support-asgi branch October 13, 2021 10:52
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant