Jupyter on the same server as FastAPI route #52
liquidcarbon
started this conversation in
Ideas
Replies: 2 comments 3 replies
-
Hi @liquidcarbon, can you check if this works for you? Two key points:
from collections.abc import AsyncIterator, Generator
from contextlib import asynccontextmanager
from typing import Any
import httpx
from fastapi import FastAPI, Request, WebSocket
from fastapi_proxy_lib.core.http import ReverseHttpProxy
from fastapi_proxy_lib.core.websocket import ReverseWebSocketProxy
from httpx import AsyncClient
class JupyterCrossHost(httpx.Auth):
# ref: <https://www.python-httpx.org/advanced/authentication/>
def auth_flow(self, request: httpx.Request) -> Generator[httpx.Request, Any, None]:
request.headers["Origin"] = str(request.url)
yield request
client = AsyncClient(auth=JupyterCrossHost())
# `localhost:8888` is my jupyter server
ws_proxy = ReverseWebSocketProxy(client, base_url="ws://localhost:8888/")
http_proxy = ReverseHttpProxy(client, base_url="http://localhost:8888/")
@asynccontextmanager
async def close_proxy_event(_: FastAPI) -> AsyncIterator[None]:
"""Close proxy."""
yield
await ws_proxy.aclose()
await http_proxy.aclose()
app = FastAPI(lifespan=close_proxy_event)
@app.websocket("/{path:path}")
async def _(websocket: WebSocket, path: str = ""):
return await ws_proxy.proxy(websocket=websocket, path=path)
@app.get("/{path:path}")
@app.post("/{path:path}")
@app.put("/{path:path}")
@app.delete("/{path:path}")
@app.patch("/{path:path}")
@app.head("/{path:path}")
@app.options("/{path:path}")
async def _(request: Request, path: str = ""):
return await http_proxy.proxy(request=request, path=path) |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks for your response! Need more help... Here's my setup: Dockerfile
app.pyfrom collections.abc import AsyncIterator, Generator
from contextlib import asynccontextmanager
from typing import Any
import httpx
from fastapi import FastAPI, Request, WebSocket
from fastapi_proxy_lib.core.http import ReverseHttpProxy
from fastapi_proxy_lib.core.websocket import ReverseWebSocketProxy
from httpx import AsyncClient
class JupyterCrossHost(httpx.Auth):
def auth_flow(self, request: httpx.Request) -> Generator[httpx.Request, Any, None]:
request.headers["Origin"] = str(request.url)
yield request
client = AsyncClient(auth=JupyterCrossHost())
jupyter_http_proxy = ReverseHttpProxy(client, base_url="http://0.0.0.0:8888/")
jupyter_ws_proxy = ReverseWebSocketProxy(client, base_url="ws://0.0.0.0:8888/")
jupyter_path = "/jupyter/{path:path}"
@asynccontextmanager
async def close_proxy_event(_: FastAPI) -> AsyncIterator[None]:
"""Close proxy."""
yield
await jupyter_ws_proxy.aclose()
await jupyter_http_proxy.aclose()
app = FastAPI(lifespan=close_proxy_event)
@app.websocket(jupyter_path)
async def _(websocket: WebSocket, path: str = ""):
return await jupyter_ws_proxy.proxy(websocket=websocket, path=path)
@app.get(jupyter_path)
@app.post(jupyter_path)
@app.put(jupyter_path)
@app.delete(jupyter_path)
@app.patch(jupyter_path)
@app.head(jupyter_path)
@app.options(jupyter_path)
async def _(request: Request, path: str = ""):
return await jupyter_http_proxy.proxy(request=request, path=path) On launch:
on going to localhost:7860
What partially worked before:
|
Beta Was this translation helpful? Give feedback.
3 replies
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
-
Hi! For shits & giggles, I'd like to have Jupyter running on the same server mounted to a FastAPI route. I got some of the way there following this thread. On launching both uvicorn and jupyter,
localhost:8000/jupyter/
behaves like a regular jupyter notebook would: it asks for a token, accepts correct token, opens file browser, opens a new notebook, but running code cells doesn't work. 1+1 hangs forever, with endless loop in the logs.Would you be so kind to provide a minimal working example?
Beta Was this translation helpful? Give feedback.
All reactions