How to Run FastAPI inside Jupyter Ensure you have these installed and accessible from the notebook pyngrok, nest_asyncio, fastapi, uvicorn and other libs you want %pip install pyngrok nest_asyncio fastapi uvicorn loguru Create a FastAPI app from fastapi import FastAPI from pydantic import BaseModel from loguru import logger app = FastAPI() class UserRequestIn(BaseModel): text: str @app.post("/test") def index(request: UserRequestIn): logger.debug(request) return {"ok": True} Start ngrok tunnel from pyngrok import ngrok ngrok_tunnel = ngrok.connect(8000) ngrok_tunnel Patch Event Loop and start server import nest_asyncio import uvicorn nest_asyncio.apply() uvicorn.run(app, port=8000) Click on the ngrok url printed above, and visit /docs endpoint and you will have the familiar Swagger page and call your api's from there
it works! thank you !
can you explain the rule of each raw, such as :
ngrok_tunnel = ngrok.connect(8000)
ngrok_tunnel - most important to understand !
nest_asyncio.apply()
uvicorn.run(app, port=8000)
can one specify all the imports at the top or it has to be written as you did?
thanks in advance!