Skip to content

Commit 24ef875

Browse files
authored
fix(Chat): fix statically rendered custom icons (#1940)
1 parent f2e774c commit 24ef875

File tree

2 files changed

+26
-36
lines changed

2 files changed

+26
-36
lines changed

shiny/ui/_chat.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,14 @@ def chat_ui(
14341434

14351435
id = resolve_id(id)
14361436

1437+
icon_attr = None
1438+
if icon_assistant is not None:
1439+
icon_attr = str(icon_assistant)
1440+
1441+
icon_deps = None
1442+
if isinstance(icon_assistant, (Tag, TagList)):
1443+
icon_deps = icon_assistant.get_dependencies()
1444+
14371445
message_tags: list[Tag] = []
14381446
if messages is None:
14391447
messages = []
@@ -1462,13 +1470,10 @@ def chat_ui(
14621470
tag_name,
14631471
ui["dependencies"],
14641472
content=ui["html"],
1473+
icon=icon_attr,
14651474
)
14661475
)
14671476

1468-
html_deps = None
1469-
if isinstance(icon_assistant, (Tag, TagList)):
1470-
html_deps = icon_assistant.get_dependencies()
1471-
14721477
res = Tag(
14731478
"shiny-chat-container",
14741479
Tag("shiny-chat-messages", *message_tags),
@@ -1478,7 +1483,7 @@ def chat_ui(
14781483
placeholder=placeholder,
14791484
),
14801485
chat_deps(),
1481-
html_deps,
1486+
icon_deps,
14821487
{
14831488
"style": css(
14841489
width=as_css_unit(width),
@@ -1488,7 +1493,9 @@ def chat_ui(
14881493
id=id,
14891494
placeholder=placeholder,
14901495
fill=fill,
1491-
icon_assistant=str(icon_assistant) if icon_assistant is not None else None,
1496+
# Also include icon on the parent so that when messages are dynamically added,
1497+
# we know the default icon has changed
1498+
icon_assistant=icon_attr,
14921499
**kwargs,
14931500
)
14941501

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

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,14 @@ async def handle_user_input_default(user_input: str):
3131
await chat_default.append_message(f"You said: {user_input}")
3232

3333
# Animal Bot ----------------------------------------------------------------------
34-
chat_animal = ui.Chat(
35-
id="chat_animal",
36-
messages=[
37-
{
38-
"content": "Hello! I'm Animal Bot. How can I help you today?",
39-
"role": "assistant",
40-
},
41-
],
42-
)
34+
chat_animal = ui.Chat(id="chat_animal")
4335

4436
with ui.div():
4537
ui.h2("Animal Bot")
46-
chat_animal.ui(icon_assistant=faicons.icon_svg("otter").add_class("icon-otter"))
38+
chat_animal.ui(
39+
messages=["Hello! I'm Animal Bot. How can I help you today?"],
40+
icon_assistant=faicons.icon_svg("otter").add_class("icon-otter"),
41+
)
4742
ui.input_select("animal", "Animal", choices=["Otter", "Hippo", "Frog", "Dove"])
4843

4944
@chat_animal.on_user_submit
@@ -68,42 +63,30 @@ async def handle_user_input_otter(user_input: str):
6863
</svg>
6964
"""
7065

71-
chat_svg = ui.Chat(
72-
id="chat_svg",
73-
messages=[
74-
{
75-
"content": "Hello! I'm SVG Bot. How can I help you today?",
76-
"role": "assistant",
77-
},
78-
],
79-
)
66+
chat_svg = ui.Chat(id="chat_svg")
8067

8168
with ui.div():
8269
ui.h2("SVG Bot")
83-
chat_svg.ui(icon_assistant=ui.HTML(bs_icon_info_circle_fill))
70+
chat_svg.ui(
71+
messages=["Hello! I'm SVG Bot. How can I help you today?"],
72+
icon_assistant=ui.HTML(bs_icon_info_circle_fill),
73+
)
8474

8575
@chat_svg.on_user_submit
8676
async def handle_user_input_svg(user_input: str):
8777
await chat_svg.append_message(f"You said: {user_input}")
8878

8979
# Image Bot -----------------------------------------------------------------------
90-
chat_image = ui.Chat(
91-
id="chat_image",
92-
messages=[
93-
{
94-
"content": "Hello! I'm Image Bot. How can I help you today?",
95-
"role": "assistant",
96-
},
97-
],
98-
)
80+
chat_image = ui.Chat(id="chat_image")
9981

10082
with ui.div():
10183
ui.h2("Image Bot")
10284
chat_image.ui(
85+
messages=["Hello! I'm Image Bot. How can I help you today?"],
10386
icon_assistant=ui.img(
10487
src="img/grace-hopper.jpg",
10588
class_="icon-image grace-hopper",
106-
)
89+
),
10790
)
10891
ui.input_select("image", "Image", choices=["Grace Hopper", "Shiny"])
10992

0 commit comments

Comments
 (0)