Skip to content

Commit 0cb205f

Browse files
nenbpmeier
andauthored
Fix CopyToCliboard functionality in UI (#411)
Co-authored-by: Philip Meier <github.pmeier@posteo.de>
1 parent 6f7fbac commit 0cb205f

File tree

1 file changed

+24
-17
lines changed

1 file changed

+24
-17
lines changed

ragna/deploy/_ui/central_view.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,38 @@ def __init__(
5555
on_click_source_info_callback: Optional[Callable] = None,
5656
timestamp=None,
5757
show_timestamp=True,
58+
assistant_toolbar_visible=True, # hide the toolbar during streaming
5859
):
5960
css_class = f"message-content-{self.role}"
6061
self.content_pane = pn.pane.Markdown(
6162
content,
6263
css_classes=["message-content", css_class],
6364
)
6465

66+
# we make this available on the instance so that we can update the value later
67+
self.clipboard_button = CopyToClipboardButton(
68+
value=self.content_pane.object, title="Copy"
69+
)
70+
71+
# we make this available on the instance so that we can toggle the visibility
72+
self.assistant_toolbar = pn.Row(
73+
self.clipboard_button,
74+
pn.widgets.Button(
75+
name="Source Info",
76+
icon="info-circle",
77+
css_classes=["source-info-button"],
78+
on_click=lambda event: self.on_click_source_info_callback(
79+
event, self.sources
80+
),
81+
),
82+
visible=assistant_toolbar_visible,
83+
)
84+
6585
if role == "assistant":
6686
assert sources is not None
6787
object = pn.Column(
6888
self.content_pane,
69-
self._copy_and_source_view_buttons(),
89+
self.assistant_toolbar,
7090
css_classes=["message-content-assistant-with-buttons"],
7191
)
7292
else:
@@ -92,22 +112,6 @@ def __init__(
92112
)
93113
self._stylesheets.append("css/chat_interface/chatmessage.css")
94114

95-
def _copy_and_source_view_buttons(self) -> pn.Row:
96-
return pn.Row(
97-
CopyToClipboardButton(
98-
value=self.content_pane.object,
99-
title="Copy",
100-
),
101-
pn.widgets.Button(
102-
name="Source Info",
103-
icon="info-circle",
104-
css_classes=["source-info-button"],
105-
on_click=lambda event: self.on_click_source_info_callback(
106-
event, self.sources
107-
),
108-
),
109-
)
110-
111115
# This cannot be a bound method, because it creates a reference cycle when trying
112116
# to access the repr of the message. See
113117
# https://github.com/Quansight/ragna/issues/359 for details.
@@ -288,11 +292,14 @@ async def chat_callback(
288292
user=self.get_user_from_role("assistant"),
289293
sources=answer["sources"],
290294
on_click_source_info_callback=self.on_click_source_info_wrapper,
295+
assistant_toolbar_visible=False,
291296
)
292297
yield message
293298

294299
async for chunk in answer_stream:
295300
message.content_pane.object += chunk["content"]
301+
message.clipboard_button.value = message.content_pane.object
302+
message.assistant_toolbar.visible = True
296303

297304
except Exception:
298305
yield RagnaChatMessage(

0 commit comments

Comments
 (0)