Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions shiny/ui/_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)
from weakref import WeakValueDictionary

from htmltools import HTML, Tag, TagAttrValue, TagChild, TagList, css
from htmltools import HTML, RenderedHTML, Tag, TagAttrValue, TagChild, TagList, css

from .. import _utils, reactive
from .._deprecated import warn_deprecated
Expand Down Expand Up @@ -1450,7 +1450,12 @@ def chat_ui(
if "role" in x:
role = x["role"]

ui = TagList(content).render()
# `content` is most likely a string, so avoid overhead in that case
# (it's also important that we *don't escape HTML* here).
if isinstance(content, str):
ui: RenderedHTML = {"html": content, "dependencies": []}
else:
ui = TagList(content).render()

if role == "user":
tag_name = "shiny-user-message"
Expand Down
10 changes: 8 additions & 2 deletions shiny/ui/_markdown_stream.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from contextlib import asynccontextmanager
from typing import AsyncIterable, Iterable, Literal, Union

from htmltools import TagChild, TagList, css
from htmltools import RenderedHTML, TagChild, TagList, css

from .. import _utils, reactive
from .._deprecated import warn_deprecated
Expand Down Expand Up @@ -353,7 +353,13 @@ def output_markdown_stream(
height
The height of the UI element.
"""
ui = TagList(content).render()

# `content` is most likely a string, so avoid overhead in that case
# (it's also important that we *don't escape HTML* here).
if isinstance(content, str):
ui: RenderedHTML = {"html": content, "dependencies": []}
else:
ui = TagList(content).render()

return Tag(
"shiny-markdown-stream",
Expand Down
14 changes: 6 additions & 8 deletions tests/playwright/shiny/components/chat/input-suggestion/app.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
from shiny.express import ui

welcome = """
suggestions1 = """
<p>Here is the <span id="first" class='suggestion'>1st input suggestion</span>.
And here is a <span id="second" data-suggestion='The actual suggestion'>2nd suggestion</span>.
And here is a <span id="second" class='suggestion' data-suggestion='The actual suggestion'>2nd suggestion</span>.
Finally, a <img id="third" data-suggestion='A 3rd, image-based, suggestion' src='shiny-hex.svg' height="50px" alt='Shiny logo'> image suggestion.</p>
"""

suggestion2 = """
<p>On the other hand, <span id="fourth" class="suggestion submit">this suggestion will auto-submit</span>.
And <span id="fifth" data-suggestion="another suggestion" data-suggestion-submit="true">this suggestion will also auto-submit</span>.</p>
"""

chat = ui.Chat(
"chat",
messages=[welcome],
)

chat = ui.Chat("chat", messages=[suggestion2])

chat.ui()
chat.ui(messages=[suggestions1])


@chat.on_user_submit
Expand Down
Loading