FastUI icon indicating copy to clipboard operation
FastUI copied to clipboard

Clarification on Root Path Access and Integration with FastAPI Returning JSON String

Open hotwa opened this issue 11 months ago • 1 comments

Issue Description: I'm exploring FastUI in combination with FastAPI and encountered a couple of issues that I would like some clarification on:

Accessing the Root Path (/) Without Explicit Definition: In the FastUI README example, there is no explicit route defined for the root path /, yet the application is accessible at this path. How is this behavior achieved? Is there an implicit route handling for / that serves the FastUI frontend, or is there another mechanism at play?

Integration with FastAPI Returning JSON String Instead of Rendering UI: When integrating FastUI with FastAPI, I noticed that accessing certain paths expected to render UI components instead returns a JSON string representation of those components. Here's a simplified example illustrating the issue:

`from fastapi import FastAPI, HTTPException from fastapi.responses import HTMLResponse from fastui import FastUI, AnyComponent, prebuilt_html, components as c from pydantic import BaseModel, Field

app = FastAPI()

class User(BaseModel): id: int name: str dob: date = Field(title='Date of Birth')

@app.get("/api/", response_model=FastUI, response_model_exclude_none=True) def users_table() -> list[AnyComponent]: # Implementation details...

@app.get('/{path:path}') async def html_landing() -> HTMLResponse: """Expected to serve the FastUI frontend but returns JSON instead.""" return HTMLResponse(prebuilt_html(title='FastUI Demo'))`

I expected the /api/ endpoint to serve UI components, but it returns a JSON string. Similarly, the root path / handled by html_landing is expected to render the FastUI frontend but shows the JSON output. Is this the intended behavior, or am I missing a configuration step to render the UI properly?

Any guidance on these issues would be greatly appreciated. Also, I'm curious if there's a recommended way to integrate FastUI with FastAPI to ensure UI components are rendered as expected rather than returned as JSON strings.

hotwa avatar Feb 29 '24 13:02 hotwa

my create env step:

micromamba create -n fastui_env -c conda-forge fastui python=3 nodejs yarn uvicorn pydantic pandas pyarrow pyyaml ipython jupyter ipykernel -y mkdir fastui_project && cd fastui_project yarn init -y yarn add @pydantic/fastui @pydantic/fastui-bootstrap @pydantic/fastui-prebuilt

hotwa avatar Feb 29 '24 13:02 hotwa