reflex icon indicating copy to clipboard operation
reflex copied to clipboard

Cannot show docs for additional FastAPI into reflex app

Open spsoni opened this issue 1 year ago β€’ 1 comments

Describe the bug I need to bundle some additional FastAPI code into reflex API so that I do not need to run separate fastapi service for additional api. I can add it, api possibly works, but I cannot see the /docs generated for it.

To Reproduce reflex app code snippet, last line is where I am adding additional API


@asynccontextmanager
def lifespan(app):
    SQLModel.metadata.create_all(engine)
    yield

app = rx.App(lifespan=lifespan)
app.add_page(index)

# Include the admin_api as a sub-API
app.api.mount("/admin", admin_api)

source for admin_api

from fastapi import FastAPI
from admin.api import user

admin_api = FastAPI()

router_map = [
    dict(router=user.router, prefix="/users", tags=["users"])
]

for router in router_map:
    admin_api.include_router(**router)

Expected behavior When I run http://0.0.0.0/admin/docs I should see openapi docs

Specifics (please complete the following information):

  • Python Version: python3.12
  • Reflex Version: 0.5.8
  • OS: MacOs

spsoni avatar Jul 31 '24 13:07 spsoni

If I run the same code with a pure fastapi code, I can see the nested docs for mount sub-api.

@asynccontextmanager
async def lifespan(app):
    SQLModel.metadata.create_all(engine)
    yield

app = FastAPI(lifespan=lifespan)

# Include the admin_api as a sub-API
app.mount("/admin", admin_api)

spsoni avatar Jul 31 '24 13:07 spsoni

It work if you use APIRouter from FastAPI

import reflex as rx
from fastapi import APIRouter


app = rx.App()
admin_api = APIRouter()

admin_api.add_api_route("/foo", lambda: {"foo": "bar"}, methods=["GET"])

# Include the admin_api as a sub-API
if app.api:
    app.api.include_router(
        admin_api,
        prefix="/admin",
    )

Lendemor avatar Jan 27 '25 17:01 Lendemor