FastUI icon indicating copy to clipboard operation
FastUI copied to clipboard

FastUI as Sub-Application

Open MMartin09 opened this issue 1 year ago • 3 comments

Hi, I have tried to mount FastUI as a sub-application in my FastAPI service. I tried to follow the proposed solution(s) from #134. However, I still haven't found a way to get this up and running.

I am using the latest release 0.6.0

MMartin09 avatar May 12 '24 10:05 MMartin09

Can you share a MRE and tell us exactly what went wrong?

hasansezertasan avatar May 14 '24 00:05 hasansezertasan

Can you share a MRE and tell us exactly what went wrong?

Sure. I tried implementing it as a subapplication with a base route "ui/".

fastui_app = FastAPI()

@fastui_app.get("/api/", response_model=FastUI, response_model_exclude_none=True)
def users_table() -> list[AnyComponent]:
    return [
        c.Page(
            components=[
                c.Heading(text='FastUI-Test', level=2)
            ]
        )
    ]

@fastui_app.get('/{path:path}')
async def html_landing() -> HTMLResponse:
    return HTMLResponse(prebuilt_html(title='FastUI Demo'))

app_configuration: ApplicationConfiguration = _build_application_configuration()
app = FastAPI(**app_configuration.model_dump(), lifespan=application_lifespan)

app.mount("/ui", fastui_app)

MMartin09 avatar May 19 '24 15:05 MMartin09

I’ve had success with creating a router for the FastUI /ui endpoints, rather than an app.

from fastapi import APIRouter
from fastui import FastUI
 
router = APIRouter()

@router.get("/api/", response_model=FastUI)
async def home():
  …

then, in your main app, include the ui router.

from fastapi import FastAPI

from .ui.router import router as ui_router

app = FastAPI()

app.include_router(ui_router, prefix='/ui')

jimkring avatar May 20 '24 00:05 jimkring

@jimkring I also tried it. However, I am always receiving a Page not found error. Can you please share the full example? How do you configure the html_landing function? Is it part of the ui_router or?

MMartin09 avatar May 20 '24 09:05 MMartin09

I'll add an example to @hasansezertasan/fastui-tutorials and address this issue when I get the chance.

hasansezertasan avatar May 22 '24 18:05 hasansezertasan

I'll add an example to @hasansezertasan/fastui-tutorials and address this issue when I get the chance.

Hey 👋, I've finished the example, could you please check it out?

Here is the related pr: hasansezertasan/fastui-tutorials#17

hasansezertasan avatar May 23 '24 18:05 hasansezertasan

Nice thank you. It is working.

MMartin09 avatar May 25 '24 14:05 MMartin09

Nice thank you. It is working.

Happy to help 🙏.

hasansezertasan avatar May 26 '24 07:05 hasansezertasan