ui.chat_ui
ui.chat_ui(
id,
*,
messages=None,
greeting=None,
placeholder='Enter a message...',
width='min(680px, 100%)',
height='auto',
fill=True,
icon_assistant=None,
enable_cancel=False,
footer=None,
**kwargs,
)UI container for a chat component (Shiny Core).
This function is for locating a Chat instance in a Shiny Core app. If you are using Shiny Express, use the ui method instead.
Parameters
id :-
A unique identifier for the chat UI.
messages :-
A sequence of messages to display in the chat. A given message can be one of the following: * A string, which is interpreted as markdown and rendered to HTML on the client. * To prevent interpreting as markdown, mark the string as
HTML. * A UI element (specifically, aTagChild). * This includesTagList, which take UI elements (including strings) as children. In this case, strings are still interpreted as markdown as long as they’re not inside HTML. * A dictionary withcontentandrolekeys. Thecontentkey can contain a content as described above, and therolekey can be “assistant” or “user”. * More generally, any type registered withshinychat.message_content. NOTE: content may include specially formatted input suggestion links (seeappend_messagefor more info). greeting :-
An optional greeting to display at the top of the chat before any conversation messages. Can be a markdown string or a
chat_greetingobject. For a dynamic or streaming greeting, useset_greetingfrom the server instead. When no greeting is set and the chat is visible with no messages, an input named{id}_greeting_requestedfires. Use this input with@reactive.event(input.{id}_greeting_requested)to generate a greeting on demand from the server. It fires again afterclear_messagesis called withgreeting=True. placeholder :-
Placeholder text for the chat input.
width :-
The width of the chat container.
height :-
The height of the chat container.
fill :-
Whether the chat should vertically take available space inside a fillable container.
icon_assistant :-
The icon to use for the assistant chat messages. Can be a HTML or a tag in the form of
HTMLorTag. IfNone, a default robot icon is used. enable_cancel :-
Whether to show a stop button during streaming that allows the user to cancel the in-progress response. When
True, the chat UI shows a stop button in place of the send button while streaming. You must observeinput.<id>_cancelon the server and callctrl.cancel()on a chatlasStreamControllerto actually stop the stream. Defaults toFalse. footer :-
Optional HTML content to display below the chat input. This can be any HTML content (tags, tag lists, or strings). Useful for adding disclaimers, attribution, or other information. The footer text is styled slightly smaller and lighter than body text by default. Customize with CSS properties
--shiny-chat-footer-font-sizeand--shiny-chat-footer-coloron the chat container or footer element. kwargs :-
Additional attributes for the chat container element.
Examples
#| standalone: true
#| components: [editor, viewer]
#| layout: vertical
#| viewerHeight: 400
## file: app.py
from shiny import App, ui
app_ui = ui.page_fillable(
ui.panel_title("Hello Shiny Chat"),
ui.chat_ui("chat"),
ui.chat_ui(
"chat",
messages=["""
Hi! This is a simple Shiny `Chat` UI. Enter a message below and I will
simply repeat it back to you.
To learn more about chatbots and how to build them with Shiny, check out
[the documentation](https://shiny.posit.co/py/docs/genai-chatbots.html).
"""],
),
fillable_mobile=True,
)
def server(input, output, session):
chat = ui.Chat(id="chat")
# Define a callback to run when the user submits a message
@chat.on_user_submit
async def handle_user_input(user_input: str):
# Append a response to the chat
await chat.append_message(f"You said: {user_input}")
app = App(app_ui, server)
## file: requirements.txt
shiny
## file: _template.json
{
"type": "app",
"id": "chat-hello",
"title": "Hello Shiny Chat",
"next_steps": [
"Run the app with `shiny run app.py`."
]
}