diff --git a/docs/_sidebar.md b/docs/_sidebar.md index 132e9407..474e0cc7 100644 --- a/docs/_sidebar.md +++ b/docs/_sidebar.md @@ -1,4 +1,5 @@ * [Getting Started](/getting-started) * [ImJoy RPC V2](/imjoy-rpc) * [Serverless Functions](/serverless-functions) + * [Serve ASGI Web Apps](/asgi-apps) * [Development](/development) diff --git a/docs/asgi-apps.md b/docs/asgi-apps.md new file mode 100644 index 00000000..5cca70cf --- /dev/null +++ b/docs/asgi-apps.md @@ -0,0 +1,61 @@ +## Serve ASGI Web Applications + +ASGI is a standard to create async web applications. With hypha, you can register a service which serves an ASGI application, e.g. a FastAPI app which serve a web page, or a web API. + +The following example shows how to use FastAPI to create a web application in the browser, e.g. https://imjoy-notebook.netlify.app/, and serve it through Hypha. + +```python +import asyncio +from imjoy_rpc.hypha import connect_to_server + +import micropip + +# This assumes you are in a Pyodide environment +await micropip.install(["fastapi==0.70.0", "pyotritonclient==0.1.37", "numpy", "imageio", "Pillow", "matplotlib"]) + +from fastapi import FastAPI +from fastapi.responses import HTMLResponse + +# Connect to the Hypha server +server_url = "https://ai.imjoy.io" +server = await connect_to_server({"server_url": server_url}) + +app = FastAPI() + +@app.get("/", response_class=HTMLResponse) +async def root(): + html_content = """ + +
+