Perspective with FastAPI and Starlette
perspective-python includes a WebSocket handler for
Starlette, and therefore
FastAPI. Add one WebSocket route and every
Table hosted by your perspective.Server is available to
<perspective-viewer> clients in the browser.
pip install "perspective-python[starlette]" fastapi uvicorn
import uvicorn
from fastapi import FastAPI, WebSocket
from perspective import Server
from perspective.handlers.starlette import PerspectiveStarletteHandler
server = Server()
client = server.new_local_client()
table = client.table(
{"symbol": "string", "price": "float", "time": "datetime"},
index="symbol",
name="prices",
)
app = FastAPI()
async def websocket_handler(websocket: WebSocket):
handler = PerspectiveStarletteHandler(
perspective_server=server,
websocket=websocket,
)
await handler.run()
app.add_api_websocket_route("/websocket", websocket_handler)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8080)
Anything in your application can now write to the table — a REST endpoint, a background task, a queue consumer:
@app.post("/ticks")
async def ticks(rows: list[dict]):
table.update(rows)
In the browser:
const websocket = await perspective.websocket("ws://localhost:8080/websocket");
const table = await websocket.open_table("prices");
await document.querySelector("perspective-viewer").load(table);
Every connected viewer updates as table.update() is called.
- Hosting a WebSocket server — replicated vs server-only modes.
- Multithreading — the
executorhandler argument andon_poll_request. - Real-time dashboards over WebSocket
python-starletteexample