Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

improvement: add max_height to mo.ui.chat() (#2624)

Myles Scolnickbb59c7dfca3f
diff --git a/examples/ai/chat/anthropic_example.py b/examples/ai/chat/anthropic_example.py
index b1bff6fc77..ca33948ca9 100644
--- a/examples/ai/chat/anthropic_example.py
+++ b/examples/ai/chat/anthropic_example.py
@@ -7,7 +7,7 @@
import marimo
-__generated_with = "0.9.0"
+__generated_with = "0.9.9"
app = marimo.App(width="medium")
@@ -60,13 +60,14 @@ def __(key, mo):
),
allow_attachments=[
"image/png",
- "image/jpeg"
+ "image/jpeg",
],
prompts=[
"Hello",
"How are you?",
"I'm doing great, how about you?",
],
+ max_height=400,
)
chatbot
return (chatbot,)
diff --git a/frontend/src/plugins/impl/chat/ChatPlugin.tsx b/frontend/src/plugins/impl/chat/ChatPlugin.tsx
index 99e942892b..1bba56507c 100644
--- a/frontend/src/plugins/impl/chat/ChatPlugin.tsx
+++ b/frontend/src/plugins/impl/chat/ChatPlugin.tsx
@@ -18,6 +18,7 @@ export const ChatPlugin = createPlugin<ChatMessage[]>("marimo-chatbot")
z.object({
prompts: z.array(z.string()).default(Arrays.EMPTY),
showConfigurationControls: z.boolean(),
+ maxHeight: z.number().optional(),
config: z.object({
maxTokens: z.number().default(100),
temperature: z.number().default(0.5),
@@ -75,6 +76,7 @@ export const ChatPlugin = createPlugin<ChatMessage[]>("marimo-chatbot")
<Chatbot
prompts={props.data.prompts}
showConfigurationControls={props.data.showConfigurationControls}
+ maxHeight={props.data.maxHeight}
allowAttachments={props.data.allowAttachments}
config={props.data.config}
sendPrompt={props.functions.send_prompt}
diff --git a/frontend/src/plugins/impl/chat/chat-ui.tsx b/frontend/src/plugins/impl/chat/chat-ui.tsx
index 105371c3d2..13979339ad 100644
--- a/frontend/src/plugins/impl/chat/chat-ui.tsx
+++ b/frontend/src/plugins/impl/chat/chat-ui.tsx
@@ -51,6 +51,7 @@ interface Props {
prompts: string[];
config: ChatConfig;
showConfigurationControls: boolean;
+ maxHeight: number | undefined;
allowAttachments: boolean | string[];
sendPrompt(req: SendMessageRequest): Promise<string>;
value: ChatMessage[];
@@ -62,6 +63,7 @@ export const Chatbot: React.FC<Props> = (props) => {
const [config, setConfig] = useState<ChatConfig>(props.config);
const [files, setFiles] = useState<FileList | undefined>(undefined);
const fileInputRef = useRef<HTMLInputElement>(null);
+ const scrollContainerRef = useRef<HTMLDivElement>(null);
const {
messages,
@@ -193,14 +195,28 @@ export const Chatbot: React.FC<Props> = (props) => {
props.allowAttachments.length > 0) ||
props.allowAttachments === true;
+ useEffect(() => {
+ // When the message length changes, scroll to the bottom
+ scrollContainerRef.current?.scrollTo({
+ top: scrollContainerRef.current.scrollHeight,
+ behavior: "smooth",
+ });
+ }, [messages.length, scrollContainerRef]);
+
return (
- <div className="flex flex-col h-full bg-[var(--slate-1)] rounded-lg shadow border border-[var(--slate-6)]">
- <div className="flex justify-end p-1">
+ <div
+ className="flex flex-col h-full bg-[var(--slate-1)] rounded-lg shadow border border-[var(--slate-6)] overflow-hidden"
+ style={{ maxHeight: props.maxHeight }}
+ >
+ <div className="absolute top-0 right-0 flex justify-end z-10 border border-[var(--slate-6)] bg-inherit rounded-bl-lg">
<Button variant="text" size="icon" onClick={() => setMessages([])}>
<Trash2Icon className="h-3 w-3" />
</Button>
</div>
- <div className="flex-grow overflow-y-auto gap-4 py-4 px-2 flex flex-col">
+ <div
+ className="flex-grow overflow-y-auto gap-4 pt-8 pb-4 px-2 flex flex-col"
+ ref={scrollContainerRef}
+ >
{messages.length === 0 && (
<div className="flex flex-col items-center justify-center h-full text-muted-foreground text-center p-4">
<BotMessageSquareIcon className="h-12 w-12 mb-4" />
@@ -250,30 +266,30 @@ export const Chatbot: React.FC<Props> = (props) => {
</div>
</div>
))}
- </div>
- {isLoading && (
- <div className="flex items-center justify-center space-x-2 mb-4">
- <Spinner size="small" />
- <Button
- variant="link"
- size="sm"
- onClick={() => stop()}
- className="text-[var(--red-9)] hover:text-[var(--red-11)]"
- >
- Stop
- </Button>
- </div>
- )}
+ {isLoading && (
+ <div className="flex items-center justify-center space-x-2 mb-4">
+ <Spinner size="small" />
+ <Button
+ variant="link"
+ size="sm"
+ onClick={() => stop()}
+ className="text-[var(--red-9)] hover:text-[var(--red-11)]"
+ >
+ Stop
+ </Button>
+ </div>
+ )}
- {error && (
- <div className="flex items-center justify-center space-x-2 mb-4">
- <ErrorBanner error={error} />
- <Button variant="outline" size="sm" onClick={() => reload()}>
- Retry
- </Button>
- </div>
- )}
+ {error && (
+ <div className="flex items-center justify-center space-x-2 mb-4">
+ <ErrorBanner error={error} />
+ <Button variant="outline" size="sm" onClick={() => reload()}>
+ Retry
+ </Button>
+ </div>
+ )}
+ </div>
<form
onSubmit={(evt) => {
@@ -531,7 +547,7 @@ const PromptsPopover: React.FC<{
size="sm"
className="border-none shadow-initial"
>
- <ChatBubbleIcon className="h-3 w-3" />
+ <ChatBubbleIcon className="h-3 w-3 mx-1" />
</Button>
</DropdownMenuTrigger>
</Tooltip>
diff --git a/marimo/_plugins/ui/_impl/chat/chat.py b/marimo/_plugins/ui/_impl/chat/chat.py
index 78e21c1c9b..17b8acc8fa 100644
--- a/marimo/_plugins/ui/_impl/chat/chat.py
+++ b/marimo/_plugins/ui/_impl/chat/chat.py
@@ -125,6 +125,7 @@ class chat(UIElement[Dict[str, Any], List[ChatMessage]]):
- `presence_penalty`
- `allow_attachments`: (bool | List[str]) allow attachments. True for any
attachments types, or pass a list of mime types
+ - `max_height`: optional maximum height for the chat element
"""
_name: Final[str] = "marimo-chatbot"
@@ -138,6 +139,7 @@ class chat(UIElement[Dict[str, Any], List[ChatMessage]]):
show_configuration_controls: bool = False,
config: Optional[ChatModelConfigDict] = None,
allow_attachments: Union[bool, List[str]] = False,
+ max_height: Optional[int] = None,
) -> None:
self._model = model
self._chat_history: List[ChatMessage] = []
@@ -152,6 +154,7 @@ class chat(UIElement[Dict[str, Any], List[ChatMessage]]):
"show-configuration-controls": show_configuration_controls,
"config": cast(JSONType, config or {}),
"allow-attachments": allow_attachments,
+ "max-height": max_height,
},
functions=(
Function(