I have two diffrent fastaoi files.I want intergate as a single file. I have posted my code. Kindly review it and let me know your thoughts and advice.
main.py
from fastapi import FastAPI
from app import submain as subapi
app = FastAPI()
app.mount("/subapi", subapi)
@app.get("/app")
def read_main():
return {"message": "Hello World from main app"}
submain.py
from fastapi import FastAPI
subapi = FastAPI()
@subapi.get("/sub")
async def read_sub():
return {"message": "Hello World from sub API"}
Error
$ uvicorn app.main:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [43148] using statreload
INFO: Started server process [43150]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:46334 - "GET /subapi/sub HTTP/1.1" 500 Internal Server Error
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py", line 394, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
return await self.app(scope, receive, send)
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/fastapi/applications.py", line 190, in __call__
await super().__call__(scope, receive, send)
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/starlette/applications.py", line 111, in __call__
await self.middleware_stack(scope, receive, send)
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/starlette/middleware/errors.py", line 181, in __call__
raise exc from None
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/starlette/middleware/errors.py", line 159, in __call__
await self.app(scope, receive, _send)
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/starlette/exceptions.py", line 82, in __call__
raise exc from None
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/starlette/exceptions.py", line 71, in __call__
await self.app(scope, receive, sender)
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/starlette/routing.py", line 566, in __call__
await route.handle(scope, receive, send)
File "/home/laptop-obs-123/anaconda3/envs/albert/lib/python3.8/site-packages/starlette/routing.py", line 376, in handle
await self.app(scope, receive, send)
TypeError: 'module' object is not callable
Kindly let me know how to mount fastapi application with in another location or another file?
you need to use Router see FAST API doc herefrom fastapi import APIRouter
about FAST API big application
see on below:
submain.py
#!/user/bin/env python
# -*- coding: utf-8 -*-
from fastapi import APIRouter
subapi = APIRouter()
@subapi.get("/sub")
async def read_sub():
return {"message": "Hello World from sub API"}
main.py
:
#!/user/bin/env python
# -*- coding: utf-8 -*-
from fastapi import FastAPI
import submain
app = FastAPI()
app.mount("/subapi", submain)
@app.get("/app")
def read_main():
return {"message": "Hello World from main app"}
running the code:
$ uvicorn main:app --reload
←[32mINFO←[0m: Uvicorn running on ←[1mhttp://127.0.0.1:8000←[0m (Press CTRL+C to quit)
←[32mINFO←[0m: Started reloader process [←[36m←[1m6616←[0m] using ←[36m←[1mwatchgod←[0m
←[32mINFO←[0m: Started server process [←[36m1084←[0m]
←[32mINFO←[0m: Waiting for application startup.
←[32mINFO←[0m: Application startup complete.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.