Chat UI component for Shiny for Python.
Installation
You can install shinychat from PyPI with:
uv pip install shinychat
Or, install the development version of shinychat from GitHub with:
uv pip install git+https://github.com/posit-dev/shinychat.git
Example
To run this example, you’ll first need to create an OpenAI API key, and set it in your environment as OPENAI_API_KEY
.
from shiny.express import render, ui
from shinychat.express import Chat
# Set some Shiny page options
ui.page_opts(title="Hello Chat")
# Create a chat instance, with an initial message
= Chat(
chat id="chat",
messages=[
"content": "Hello! How can I help you today?", "role": "assistant"},
{
],
)
# Display the chat
chat.ui()
# Define a callback to run when the user submits a message
@chat.on_user_submit
handle_user_input(user_input: str):
async def chat.append_message(f"You said: {user_input}")
await
"Message state:"
@render.code
message_state():
def str(chat.messages()) return