From 4ffb7985b862f8ebaefd6bc5b34ae68f572910ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fred=20Lef=C3=A9v=C3=A8re-Laoide?= <90181748+FredLL-Avaiga@users.noreply.github.com> Date: Mon, 25 Nov 2024 14:28:54 +0100 Subject: [PATCH] support images in chat (#2268) * support images in chat resolves #1314 * Fab's comments --- doc/gui/examples/controls/chat_calculator.py | 11 +- doc/gui/examples/controls/chat_discuss.py | 6 +- doc/gui/examples/controls/chat_images.py | 47 ++++ doc/gui/examples/controls/chat_messages.py | 17 +- frontend/taipy-gui/src/components/Router.tsx | 2 +- .../src/components/Taipy/Chat.spec.tsx | 157 +++++++----- .../taipy-gui/src/components/Taipy/Chat.tsx | 236 +++++++++++------- .../components/Taipy/FileSelector.spec.tsx | 8 +- .../src/components/Taipy/FileSelector.tsx | 10 +- .../components/Taipy/Notification.spec.tsx | 42 ++-- .../src/components/Taipy/Notification.tsx | 40 +-- .../src/context/taipyReducers.spec.ts | 149 +++++++---- .../taipy-gui/src/context/taipyReducers.ts | 56 ++--- frontend/taipy-gui/src/utils/image.ts | 30 +++ taipy/gui/_renderers/factory.py | 5 +- taipy/gui/viselements.json | 10 +- 16 files changed, 516 insertions(+), 310 deletions(-) create mode 100644 doc/gui/examples/controls/chat_images.py create mode 100644 frontend/taipy-gui/src/utils/image.ts diff --git a/doc/gui/examples/controls/chat_calculator.py b/doc/gui/examples/controls/chat_calculator.py index b2ee454448..442b47530a 100644 --- a/doc/gui/examples/controls/chat_calculator.py +++ b/doc/gui/examples/controls/chat_calculator.py @@ -16,20 +16,19 @@ # Human-computer dialog UI based on the chat control. # ----------------------------------------------------------------------------------------- from math import cos, pi, sin, sqrt, tan # noqa: F401 -from typing import Optional from taipy.gui import Gui # The user interacts with the Python interpreter users = ["human", "Result"] -messages: list[tuple[str, str, str, Optional[str]]] = [] +messages: list[tuple[str, str, str]] = [] def evaluate(state, var_name: str, payload: dict): # Retrieve the callback parameters - (_, _, expression, sender_id, image_url) = payload.get("args", []) + (_, _, expression, sender_id, _) = payload.get("args", []) # Add the input content as a sent message - messages.append((f"{len(messages)}", expression, sender_id, image_url)) + messages.append((f"{len(messages)}", expression, sender_id)) # Default message used if evaluation fails result = "Invalid expression" try: @@ -38,12 +37,12 @@ def evaluate(state, var_name: str, payload: dict): except Exception: pass # Add the result as an incoming message - messages.append((f"{len(messages)}", result, users[1], None)) + messages.append((f"{len(messages)}", result, users[1])) state.messages = messages page = """ -<|{messages}|chat|users={users}|sender_id={users[0]}|on_action=evaluate|> +<|{messages}|chat|users={users}|sender_id={users[0]}|on_action=evaluate|don't allow_send_images|> """ Gui(page).run(title="Chat - Calculator") diff --git a/doc/gui/examples/controls/chat_discuss.py b/doc/gui/examples/controls/chat_discuss.py index 4d46f66f26..cd9775057c 100644 --- a/doc/gui/examples/controls/chat_discuss.py +++ b/doc/gui/examples/controls/chat_discuss.py @@ -25,7 +25,7 @@ from taipy.gui.gui_actions import navigate, notify username = "" -users: list[Union[str, Icon]] = [] +users: list[tuple[str, Union[str, Icon]]] = [] messages: list[tuple[str, str, str, Optional[str]]] = [] Gui.add_shared_variables("messages", "users") @@ -82,4 +82,6 @@ def send(state, _: str, payload: dict): """ pages = {"register": register_page, "discuss": discuss_page} -gui = Gui(pages=pages).run(title="Chat - Discuss") + +if __name__ == "__main__": + gui = Gui(pages=pages).run(title="Chat - Discuss") diff --git a/doc/gui/examples/controls/chat_images.py b/doc/gui/examples/controls/chat_images.py new file mode 100644 index 0000000000..2e7667c1de --- /dev/null +++ b/doc/gui/examples/controls/chat_images.py @@ -0,0 +1,47 @@ +# Copyright 2021-2024 Avaiga Private Limited +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on +# an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the +# specific language governing permissions and limitations under the License. +# ----------------------------------------------------------------------------------------- +# To execute this script, make sure that the taipy-gui package is installed in your +# Python environment and run: +# python