Skip to content

Commit

Permalink
Add Chat to component gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert committed Jul 12, 2024
1 parent b7f7fc0 commit 6b60041
Show file tree
Hide file tree
Showing 8 changed files with 419 additions and 0 deletions.
22 changes: 22 additions & 0 deletions components/display-messages/chat/app-core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from shiny import App, ui

app_ui = ui.page_fillable(
ui.panel_title("Hello Shiny Chat"),
ui.chat_ui("chat"), # <<
fillable_mobile=True,
)


def server(input):
# Create a chat instance and display it
chat = ui.Chat(id="chat") # <<
chat.ui() # <<

# Define a callback to run when the user submits a message
@chat.on_user_submit # <<
async def _(): # <<
# Simply echo the user's input back to them
await chat.append_message(f"You said: {chat.user_input()}") # <<


app = App(app_ui, server)
18 changes: 18 additions & 0 deletions components/display-messages/chat/app-express.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from shiny.express import ui

ui.page_opts(
title="Hello Shiny Chat",
fillable=True,
fillable_mobile=True,
)

# Create a chat instance and display it
chat = ui.Chat(id="chat") # <<
chat.ui() # <<


# Define a callback to run when the user submits a message
@chat.on_user_submit # <<
async def _(): # <<
# Simply echo the user's input back to them
await chat.append_message(f"You said: {chat.user_input()}") # <<
19 changes: 19 additions & 0 deletions components/display-messages/chat/app-preview-code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from shiny.express import ui

# Set some Shiny page options
ui.page_opts(
title="Hello Shiny Chat",
fillable=True,
fillable_mobile=True,
)

# Create a chat instance and display it
chat = ui.Chat(id="chat")
chat.ui()


# Define a callback to run when the user submits a message
@chat.on_user_submit
async def _():
# Append a response to the chat
await chat.append_message(f"You said: {chat.user_input()}")
38 changes: 38 additions & 0 deletions components/display-messages/chat/app-preview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from shiny.express import ui

# Set some Shiny page options
ui.page_opts(
title="Hello Shiny Chat",
fillable=True,
fillable_mobile=True,
)

# Create a welcome message
welcome = ui.markdown(
"""
Hi! This is a simple Shiny `Chat` UI. Enter a message below and I will
simply repeat it back to you. For more examples, see this
[folder of examples](https://github.com/posit-dev/py-shiny/tree/main/examples/chat).
"""
)

# Create a chat instance
chat = ui.Chat(
id="chat",
messages=[welcome],
)

# Display it
chat.ui()


# Define a callback to run when the user submits a message
@chat.on_user_submit
async def _():
# Get the chat messages.
messages = chat.messages()
# Typically you'd pass messages to an LLM for response generation,
# but for this example, we'll just echo the user's input
user = messages[-1]["content"]
# Append a response to the chat
await chat.append_message(f"You said: {user}")
Loading

0 comments on commit 6b60041

Please sign in to comment.