Skip to content

Commit b9868c8

Browse files
committed
Ressurect the hello starter template as an api-example (for API reference)
1 parent b80f1ee commit b9868c8

File tree

5 files changed

+74
-3
lines changed

5 files changed

+74
-3
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "app",
3+
"id": "chat-hello",
4+
"title": "Hello Shiny Chat",
5+
"next_steps": [
6+
"Run the app with `shiny run app.py`."
7+
]
8+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from shiny import App, ui
2+
3+
app_ui = ui.page_fillable(
4+
ui.panel_title("Hello Shiny Chat"),
5+
ui.chat_ui("chat"),
6+
fillable_mobile=True,
7+
)
8+
9+
# Create a welcome message
10+
welcome = """
11+
Hi! This is a simple Shiny `Chat` UI. Enter a message below and I will
12+
simply repeat it back to you.
13+
14+
To learn more about chatbots and how to build them with Shiny, check out
15+
[the documentation](https://shiny.posit.co/py/docs/genai-chatbots.html).
16+
"""
17+
18+
19+
def server(input, output, session):
20+
chat = ui.Chat(id="chat", messages=[welcome])
21+
22+
# Define a callback to run when the user submits a message
23+
@chat.on_user_submit
24+
async def handle_user_input(user_input: str):
25+
# Append a response to the chat
26+
await chat.append_message(f"You said: {user_input}")
27+
28+
29+
app = App(app_ui, server)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from shiny.express import ui
2+
3+
# Set some Shiny page options
4+
ui.page_opts(
5+
title="Hello Shiny Chat",
6+
fillable=True,
7+
fillable_mobile=True,
8+
)
9+
10+
# Create a welcome message
11+
welcome = """
12+
Hi! This is a simple Shiny `Chat` UI. Enter a message below and I will
13+
simply repeat it back to you.
14+
15+
To learn more about chatbots and how to build them with Shiny, check out
16+
[the documentation](https://shiny.posit.co/py/docs/genai-chatbots.html).
17+
"""
18+
19+
# Create a chat instance
20+
chat = ui.Chat(
21+
id="chat",
22+
messages=[welcome],
23+
)
24+
25+
# Display it
26+
chat.ui()
27+
28+
29+
# Define a callback to run when the user submits a message
30+
@chat.on_user_submit
31+
async def handle_user_input(user_input: str):
32+
# Append a response to the chat
33+
await chat.append_message(f"You said: {user_input}")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
shiny

shiny/ui/_chat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
]
9191

9292

93-
@add_example(ex_dir="../templates/chat/starters/hello")
93+
@add_example("app-core.py")
9494
class Chat:
9595
"""
9696
Create a chat interface.
@@ -1326,7 +1326,7 @@ async def _send_custom_message(self, handler: str, obj: ClientMessage | None):
13261326
)
13271327

13281328

1329-
@add_example(ex_dir="../templates/chat/starters/hello")
1329+
@add_example("app-express.py")
13301330
class ChatExpress(Chat):
13311331
def ui(
13321332
self,
@@ -1377,7 +1377,7 @@ def ui(
13771377
)
13781378

13791379

1380-
@add_example(ex_dir="../templates/chat/starters/hello")
1380+
@add_example(ex_dir="../api-examples/Chat")
13811381
def chat_ui(
13821382
id: str,
13831383
*,

0 commit comments

Comments
 (0)