ui.chat_ui
ui.chat_ui(
id,
*,
messages=None,
placeholder='Enter a message...',
width='min(680px, 100%)',
height='auto',
fill=True,
icon_assistant=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, a
TagChild). * This includes TagList, 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). 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
HTMLor Tag. IfNone, a default robot icon is used. kwargs :-
Additional attributes for the chat container element.
Examples
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| 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`."
]
}