You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromdatetimeimportdatefromfastapiimportFastAPI, HTTPExceptionfromfastapi.responsesimportHTMLResponsefromfastuiimportFastUI, AnyComponent, prebuilt_html, componentsascfromfastui.components.displayimportDisplayMode, DisplayLookupfromfastui.eventsimportGoToEvent, BackEventfrompydanticimportBaseModel, Fieldapp=FastAPI()
classUser(BaseModel):
id: intname: strdob: date=Field(title='Date of Birth')
# define some usersusers= [
User(id=1, name='John', dob=date(1990, 1, 1)),
User(id=2, name='Jack', dob=date(1991, 1, 1)),
User(id=3, name='Jill', dob=date(1992, 1, 1)),
User(id=4, name='Jane', dob=date(1993, 1, 1)),
]
@app.get("/api/", response_model=FastUI, response_model_exclude_none=True)defusers_table() ->list[AnyComponent]:
""" Show a table of four users, `/api` is the endpoint the frontend will connect to when a user visits `/` to fetch components to render. """return [
c.Page( # Page provides a basic container for componentscomponents=[
c.Heading(text='Users', level=2), # renders `<h2>Users</h2>`c.Table(
data=users,
# define two columns for the tablecolumns=[
# the first is the users, name rendered as a link to their profileDisplayLookup(field='name', on_click=GoToEvent(url='/user/{id}/')),
# the second is the date of birth, rendered as a dateDisplayLookup(field='dob', mode=DisplayMode.date),
],
),
]
),
]
@app.get("/api/user/{user_id}/", response_model=FastUI, response_model_exclude_none=True)defuser_profile(user_id: int) ->list[AnyComponent]:
""" User profile page, the frontend will fetch this when the user visits `/user/{id}/`. """try:
user=next(uforuinusersifu.id==user_id)
exceptStopIteration:
raiseHTTPException(status_code=404, detail="User not found")
return [
c.Page(
components=[
c.Heading(text=user.name, level=2),
c.Link(components=[c.Text(text='Back')], on_click=BackEvent()),
c.Details(data=user),
]
),
]
@app.get('/{path:path}')asyncdefhtml_landing() ->HTMLResponse:
"""Simple HTML page which serves the React app, comes last as it matches all paths."""returnHTMLResponse(prebuilt_html(title='FastUI Demo'))
I use example from https://docs.pydantic.dev/fastui/
Then run
uvicorn example:app --reload
See an error instead of content

Packages:
What do I do wrong?
The text was updated successfully, but these errors were encountered: