[Python] How to load a different file than index.html in FastAPI root path while using...

Discussão em 'Python' iniciado por Stack, Março 25, 2025.

  1. Stack

    Stack Membro Participativo

    Here is a simple static FastAPI app. With this setup even though the root path is expected to return a FileResponse of custom.html, the app still returns index.html. How can I get the root path work and render custom.html?

    from fastapi import FastAPI
    from fastapi.staticfiles import StaticFiles
    from fastapi.responses import FileResponse

    app = FastAPI()


    app.mount(
    "/",
    StaticFiles(directory="static", html=True),
    name="static",
    )

    @app.get("/")
    async def index() -> FileResponse:
    return FileResponse("custom.html", media_type="html")

    Continue reading...

Compartilhe esta Página