-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
419 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}") # << |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}") |
Oops, something went wrong.