Skip to content

Commit efdd8cb

Browse files
committed
feat: Make images (i.e. avatars) full-bleed
1 parent 4907d7f commit efdd8cb

File tree

7 files changed

+54
-21
lines changed

7 files changed

+54
-21
lines changed

js/chat/chat.scss

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,26 @@ shiny-chat-message {
7070
width: 2rem;
7171
display: grid;
7272
place-items: center;
73+
overflow: clip;
7374

7475
> * {
75-
height: 20px;
76-
width: 20px;
76+
// images and avatars are full-bleed
77+
height: 100%;
78+
width: 100%;
7779
margin: 0 !important;
78-
max-height: 85%;
79-
max-width: 85%;
8080
object-fit: contain;
8181
}
82+
83+
> svg,
84+
> .icon,
85+
> .fa,
86+
> .bi {
87+
// icons and svgs need some padding within the circle
88+
max-height: 66%;
89+
max-width: 66%;
90+
}
8291
}
92+
8393
/* Vertically center the 2nd column (message content) */
8494
shiny-markdown-stream {
8595
align-self: center;

shiny/www/py-shiny/chat/chat.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

shiny/www/py-shiny/chat/chat.css.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/playwright/shiny/components/chat/icon/app.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import asyncio
2+
from pathlib import Path
23

34
import faicons
45

5-
from shiny.express import input, ui
6+
from shiny.express import app_opts, input, ui
67

78
ui.page_opts(title="Chat Icons")
89

10+
app_opts({ "/img": Path(__file__).parent / "img" })
11+
912
with ui.layout_columns():
1013
# Default Bot ---------------------------------------------------------------------
1114
chat_default = ui.Chat(
@@ -83,26 +86,36 @@ async def handle_user_input_otter(user_input: str):
8386
async def handle_user_input_svg(user_input: str):
8487
await chat_svg.append_message(f"You said: {user_input}")
8588

86-
# Shiny Bot -----------------------------------------------------------------------
87-
chat_shiny = ui.Chat(
88-
id="chat_shiny",
89+
# Image Bot -----------------------------------------------------------------------
90+
chat_image = ui.Chat(
91+
id="chat_image",
8992
messages=[
9093
{
91-
"content": "Hello! I'm Shiny Bot. How can I help you today?",
94+
"content": "Hello! I'm Image Bot. How can I help you today?",
9295
"role": "assistant",
9396
},
9497
],
9598
)
9699

97100
with ui.div():
98-
ui.h2("Shiny Bot")
99-
chat_shiny.ui(
101+
ui.h2("Image Bot")
102+
chat_image.ui(
100103
icon_assistant=ui.img(
101-
src="https://github.com/rstudio/hex-stickers/raw/refs/heads/main/PNG/shiny.png",
102-
class_="icon-shiny",
104+
src="img/grace-hopper.jpg",
105+
class_="icon-image grace-hopper",
103106
)
104107
)
105-
106-
@chat_shiny.on_user_submit
107-
async def handle_user_input_shiny(user_input: str):
108-
await chat_shiny.append_message(f"You said: {user_input}")
108+
ui.input_select("image", "Image", choices=["Grace Hopper", "Shiny"])
109+
110+
@chat_image.on_user_submit
111+
async def handle_user_input_image(user_input: str):
112+
icon = None
113+
if input.image() == "Shiny":
114+
icon = ui.img(
115+
src="img/shiny.png",
116+
class_="icon-image shiny",
117+
)
118+
await chat_image.append_message(
119+
f"You said: {user_input}",
120+
icon=icon
121+
)
8.74 KB
Loading
9.45 KB
Loading

tests/playwright/shiny/components/chat/icon/test_chat_icon.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_validate_chat_basic(page: Page, local_app: ShinyAppProc) -> None:
2626
ChatModule(page, "default", "bi bi-robot"),
2727
ChatModule(page, "animal", "fa icon-otter"),
2828
ChatModule(page, "svg", "bi bi-info-circle-fill icon-svg"),
29-
ChatModule(page, "shiny", "icon-shiny"),
29+
ChatModule(page, "image", "icon-image grace-hopper"),
3030
]
3131

3232
for mod in chats:
@@ -38,6 +38,7 @@ def test_validate_chat_basic(page: Page, local_app: ShinyAppProc) -> None:
3838

3939
mod.expect_last_message_icon_to_have_classes()
4040

41+
# Test changing icons during the chat
4142
animal = controller.InputSelect(page, "animal")
4243
chat_animal = chats[1]
4344

@@ -57,3 +58,12 @@ def test_validate_chat_basic(page: Page, local_app: ShinyAppProc) -> None:
5758
chat_animal.chat.set_user_input("hello")
5859
chat_animal.chat.send_user_input()
5960
chat_animal.expect_last_message_icon_to_have_classes()
61+
62+
# Test changing icon images during the chat
63+
image = controller.InputSelect(page, "image")
64+
chat_image = chats[3]
65+
66+
image.set("Shiny")
67+
chat_image.chat.set_user_input("hi shiny")
68+
chat_image.chat.send_user_input()
69+
chat_image.expect_last_message_icon_to_have_classes("icon-image shiny")

0 commit comments

Comments
 (0)