1

I want to get app instance in my router file , what should I do ?

My main.py is

...
app = FastAPI()
app.machine_learning_model = joblib.load(some_path)
app.include_router(some_router)
...

Now I want to use app.machine_learning_model in some_router's file , what should I do ?

DachuanZhao
  • 881
  • 3
  • 10
  • 24

1 Answers1

2

You could access the app instance using request.app, as shown below:

from fastapi import Request

@router.get("/some_route")
async def some_router_function(request: Request):
    model = request.app.machine_learning_model
Chris
  • 4,940
  • 2
  • 7
  • 28