Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Example: FastUI as Sub-Application #17

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion chat-app/README.md

This file was deleted.

6 changes: 6 additions & 0 deletions message-board/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Message Board

- Inspiration:
- [shroominic/fastui-chat: 💬 minimalistic ChatBot Interface in pure python](https://github.com/shroominic/fastui-chat)
- [Fastui-chat/src/fastui_chat/chat.py at main · mem15381/Fastui-chat](https://github.com/mem15381/Fastui-chat/blob/main/src/fastui_chat/chat.py)
- [koyeb/example-fastui-chatbot](https://github.com/koyeb/example-fastui-chatbot)
29 changes: 29 additions & 0 deletions message-board/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2024 Hasan Sezer Taşan <[email protected]>
# Copyright (C) 2024 <[email protected]>
from __future__ import annotations

from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastui import AnyComponent, FastUI, prebuilt_html
from fastui import components as c
from fastui.events import GoToEvent
from typing_extensions import TypedDict


class DisplayAlias(TypedDict):
command: str
output: str


app = FastAPI()


@app.get("/api/", response_model=FastUI, response_model_exclude_none=True)
def page() -> list[AnyComponent]:
return []


@app.get("/{path:path}")
def root() -> HTMLResponse:
"""Simple HTML page which serves the React app, comes last as it matches all paths."""
return HTMLResponse(prebuilt_html(title="FastUI Message Board"))
3 changes: 3 additions & 0 deletions sub-application/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# FastUI as Sub-Application

Example, in response to these two issues: [pydantic/FastUI#311](https://github.com/pydantic/FastUI/issues/311) and [pydantic/FastUI#134](https://github.com/pydantic/FastUI/issues/134), of using FastUI as a sub-application.
52 changes: 52 additions & 0 deletions sub-application/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright 2024 Hasan Sezer Taşan <[email protected]>
# Copyright (C) 2024 <[email protected]>
from __future__ import annotations

from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastui import AnyComponent, FastUI, prebuilt_html
from fastui import components as c

fooapp = FastAPI()


@fooapp.get("/api/", response_model=FastUI, response_model_exclude_none=True)
def foo_page() -> list[AnyComponent]:
return [c.Heading(text="Sub Application Foo API!")]


@fooapp.get("/{path:path}")
def foo_root() -> HTMLResponse:
"""Simple HTML page which serves the React app, comes last as it matches all paths."""
return HTMLResponse(
prebuilt_html(
title="Sub Application Foo",
api_root_url="/foo/api",
api_path_strip="/foo",
)
)


barapp = FastAPI()


@barapp.get("/api/v1/", response_model=FastUI, response_model_exclude_none=True)
def bar_page() -> list[AnyComponent]:
return [c.Heading(text="Sub Application Bar API!")]


@barapp.get("/{path:path}")
def bar_root() -> HTMLResponse:
"""Simple HTML page which serves the React app, comes last as it matches all paths."""
return HTMLResponse(
prebuilt_html(
title="Sub Application Bar",
api_root_url="/bar/api/v1",
api_path_strip="/bar",
)
)


app = FastAPI()
app.mount("/foo", fooapp)
app.mount("/bar", barapp)