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.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
improvement: use frontend markdown rendering for mo.ui.chat so content stays clean (#7066)
Myles Scolnick9fc4f08b9871
diff --git a/examples/ai/chat/generative_ui.py b/examples/ai/chat/generative_ui.pyindex 97fd9bc05a..d56deb254c 100644--- a/examples/ai/chat/generative_ui.py+++ b/examples/ai/chat/generative_ui.py@@ -10,7 +10,7 @@import marimo-__generated_with = "0.15.5"+__generated_with = "0.17.6"app = marimo.App(width="medium")@@ -19,6 +19,7 @@ def _():import polars as plimport marimo as moimport os+ import altairhas_api_key = os.environ.get("OPENAI_API_KEY") is not Nonemo.stop(diff --git a/frontend/islands/vite.config.mts b/frontend/islands/vite.config.mtsindex 21303c371d..b18e05deb9 100644--- a/frontend/islands/vite.config.mts+++ b/frontend/islands/vite.config.mts@@ -42,6 +42,7 @@ export default defineConfig({define: {"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),"process.env.LOG": JSON.stringify(""),+ "process.env.VSCODE_TEXTMATE_DEBUG": JSON.stringify(false),"process.env.NODE_DEBUG": JSON.stringify(false),// Precedence: VITE_MARIMO_VERSION > package.json version > "latest""import.meta.env.VITE_MARIMO_VERSION": process.env.VITE_MARIMO_VERSIONdiff --git a/frontend/src/plugins/impl/chat/ChatPlugin.tsx b/frontend/src/plugins/impl/chat/ChatPlugin.tsxindex 8e5830de39..ae134eec0c 100644--- a/frontend/src/plugins/impl/chat/ChatPlugin.tsx+++ b/frontend/src/plugins/impl/chat/ChatPlugin.tsx@@ -1,4 +1,6 @@/* Copyright 2024 Marimo. All rights reserved. */++import { Suspense } from "react";import { z } from "zod";import { TooltipProvider } from "@/components/ui/tooltip";import { createPlugin } from "@/plugins/core/builder";@@ -74,18 +76,20 @@ export const ChatPlugin = createPlugin<{ messages: ChatMessage[] }>(}).renderer((props) => (<TooltipProvider>- <Chatbot- prompts={props.data.prompts}- showConfigurationControls={props.data.showConfigurationControls}- maxHeight={props.data.maxHeight}- allowAttachments={props.data.allowAttachments}- config={props.data.config}- get_chat_history={props.functions.get_chat_history}- delete_chat_history={props.functions.delete_chat_history}- delete_chat_message={props.functions.delete_chat_message}- send_prompt={props.functions.send_prompt}- value={props.value?.messages || Arrays.EMPTY}- setValue={(messages) => props.setValue({ messages })}- />+ <Suspense>+ <Chatbot+ prompts={props.data.prompts}+ showConfigurationControls={props.data.showConfigurationControls}+ maxHeight={props.data.maxHeight}+ allowAttachments={props.data.allowAttachments}+ config={props.data.config}+ get_chat_history={props.functions.get_chat_history}+ delete_chat_history={props.functions.delete_chat_history}+ delete_chat_message={props.functions.delete_chat_message}+ send_prompt={props.functions.send_prompt}+ value={props.value?.messages || Arrays.EMPTY}+ setValue={(messages) => props.setValue({ messages })}+ />+ </Suspense></TooltipProvider>));diff --git a/frontend/src/plugins/impl/chat/chat-ui.tsx b/frontend/src/plugins/impl/chat/chat-ui.tsxindex cbd42cb42b..c2683e0f15 100644--- a/frontend/src/plugins/impl/chat/chat-ui.tsx+++ b/frontend/src/plugins/impl/chat/chat-ui.tsx@@ -17,7 +17,7 @@ import {Trash2Icon,X,} from "lucide-react";-import React, { useEffect, useRef, useState } from "react";+import React, { lazy, useEffect, useRef, useState } from "react";import { convertToFileUIPart } from "@/components/chat/chat-utils";import {type AdditionalCompletions,@@ -43,7 +43,6 @@ import { Tooltip } from "@/components/ui/tooltip";import { toast } from "@/components/ui/use-toast";import { moveToEndOfEditor } from "@/core/codemirror/utils";import { useAsyncData } from "@/hooks/useAsyncData";-import { renderHTML } from "@/plugins/core/RenderHTML";import { cn } from "@/utils/cn";import { copyToClipboard } from "@/utils/copy";import { Logger } from "@/utils/Logger";@@ -52,6 +51,10 @@ import { ErrorBanner } from "../common/error-banner";import type { PluginFunctions } from "./ChatPlugin";import type { ChatConfig, ChatMessage } from "./types";+const LazyStreamdown = lazy(() =>+ import("streamdown").then((module) => ({ default: module.Streamdown })),+);+interface Props extends PluginFunctions {prompts: string[];config: ChatConfig;@@ -194,9 +197,13 @@ export const Chatbot: React.FC<Props> = (props) => {const textParts = message.parts?.filter((p) => p.type === "text");const textContent = textParts?.map((p) => p.text).join("\n");const content =- message.role === "assistant"- ? renderHTML({ html: textContent })- : textContent;+ message.role === "assistant" ? (+ <LazyStreamdown className="mo-markdown-renderer">+ {textContent}+ </LazyStreamdown>+ ) : (+ textContent+ );const attachments = message.parts?.filter((p) => p.type === "file");diff --git a/frontend/turbo.json b/frontend/turbo.jsonindex a211013ec2..be1e1fda11 100644--- a/frontend/turbo.json+++ b/frontend/turbo.json@@ -4,7 +4,7 @@"tasks": {"build": {"outputs": ["dist/**"],- "env": ["VITE_MARIMO_ISLANDS", "NODE_ENV"]+ "env": ["VITE_MARIMO_ISLANDS", "VITE_MARIMO_VERSION", "NODE_ENV"]},"build-storybook": {"outputs": ["storybook-static/**"]diff --git a/marimo/_plugins/ui/_impl/chat/chat.py b/marimo/_plugins/ui/_impl/chat/chat.pyindex 1711c384a4..140bfcf83c 100644--- a/marimo/_plugins/ui/_impl/chat/chat.py+++ b/marimo/_plugins/ui/_impl/chat/chat.py@@ -11,7 +11,6 @@ from marimo._ai._types import (ChatModelConfigDict,)from marimo._output.formatting import as_html-from marimo._output.md import mdfrom marimo._output.rich_help import mddocfrom marimo._plugins.core.web_component import JSONTypefrom marimo._plugins.ui._core.ui_element import UIElement@@ -270,7 +269,7 @@ class chat(UIElement[dict[str, Any], list[ChatMessage]]):# Return the response as HTML# If the response is a string, convert it to markdownif isinstance(response, str):- return md(response).text+ return responsereturn as_html(response).textdef _convert_value(self, value: dict[str, Any]) -> list[ChatMessage]:diff --git a/tests/_plugins/ui/_impl/chat/test_chat.py b/tests/_plugins/ui/_impl/chat/test_chat.pyindex 417d8c7b05..c7b729427b 100644--- a/tests/_plugins/ui/_impl/chat/test_chat.py+++ b/tests/_plugins/ui/_impl/chat/test_chat.py@@ -11,7 +11,6 @@ from marimo._ai._types import (ChatModelConfig,ChatModelConfigDict,)-from marimo._output.md import mdfrom marimo._plugins import uifrom marimo._plugins.ui._impl.chat.chat import (DEFAULT_CONFIG,@@ -83,7 +82,7 @@ async def test_chat_send_prompt():)response: str = await chat._send_prompt(request)- assert response == md("Response to: Hello").text+ assert response == "Response to: Hello"assert len(chat._chat_history) == 2assert chat._chat_history[0].role == "user"assert chat._chat_history[0].content == "Hello"@@ -106,7 +105,7 @@ async def test_chat_send_prompt_async_function():)response: str = await chat._send_prompt(request)- assert response == md("Response to: Hello").text+ assert response == "Response to: Hello"assert len(chat._chat_history) == 2assert chat._chat_history[0].role == "user"assert chat._chat_history[0].content == "Hello"@@ -132,7 +131,7 @@ async def test_chat_send_prompt_async_generator():response: str = await chat._send_prompt(request)# the last yielded value is the response- assert response == md("2").text+ assert response == "2"assert len(chat._chat_history) == 2assert chat._chat_history[0].role == "user"assert chat._chat_history[0].content == "Hello"diff --git a/turbo.json b/turbo.jsonindex d8612accee..142e475136 100644--- a/turbo.json+++ b/turbo.json@@ -3,7 +3,8 @@"tasks": {"build": {"dependsOn": ["^build", "codegen"],- "outputs": ["dist/**"]+ "outputs": ["dist/**"],+ "env": ["NODE_ENV", "VITE_MARIMO_VERSION", "VITE_MARIMO_ISLANDS"]},"typecheck": {"dependsOn": ["codegen", "^codegen"]