Skip to content

Commit

Permalink
Merge branch 'develop' into feature/Avaiga#1248-cover-json-data-node-…
Browse files Browse the repository at this point in the history
…control-new
  • Loading branch information
THEBOSS0369 authored Nov 6, 2024
2 parents 9603537 + 633e01c commit 19bb824
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 12 deletions.
192 changes: 192 additions & 0 deletions doc/gui/examples/controls/chat-streaming.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
# 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 <script>
# -----------------------------------------------------------------------------------------
import datetime
import re
import time
import typing as t

import requests # type: ignore[import-untyped]

from taipy.gui import Gui, Icon, State, get_state_id, invoke_callback, invoke_long_callback

# The Wikipedia API used to generate content for a date
wiki_url = "https://en.wikipedia.org/api/rest_v1/feed/onthisday/{type}/{month}/{day}"
event_types = {
"happen": "events",
"passé": "events",
"born": "births",
"né": "births",
"dead": "deaths",
"mort": "deaths",
}
user_agent = "https://taipy.io/demo"

# The list of messages
messages: list[tuple[str, str, str]] = [] # (Message id, message, sender)

# The two users of this app
users = [
["wikipedia", Icon("https://www.wikipedia.org/static/apple-touch/wikipedia.png", "Wikipedia")],
["taipy", Icon("https://docs.taipy.io/en/latest/assets/images/favicon.png", "Taipy")],
]


# Initialize the user state
def on_init(state: State):
# Messages are for this user only
state.messages = []


# Add the image if there is one in the Wikipedia returned data
def add_image_to_message(state: State, idx: int, text: str, image_url: str):
msg_content: str = state.messages[idx][1]
if (pos := msg_content.find(text)) > -1:
msg_content = msg_content[: pos + len(text)] + f"\n\n![{text}]({image_url})" + msg_content[pos + len(text) :]
set_message(state, msg_content, idx)


# Invoked by update_message through a thread
def update_message_with_image(gui: Gui, state_id: str, message_idx: int, text: str, image: dict):
if src := image.get("source"):
time.sleep(0.2) # Apply the typewriter effect
invoke_callback(
gui,
state_id,
add_image_to_message,
[message_idx, text, src],
)


# Invoked by query_wikipedia()
def update_message(state: State, json, event_type: str, for_date: str, idx: int):
if isinstance(json, dict):
# Initial response content
set_message(state, f"{event_type} for {for_date}:\n", idx)

for event in json.get(event_type, []):
time.sleep(0.2) # Apply the typewriter effect
# Update response text
append_to_message(state, f"\n* {event.get('year', '')}: {event.get('text', '')}", idx)
# Invoke update_message_with_image() in a separated thread
invoke_long_callback(
state=state,
user_function=update_message_with_image,
user_function_args=[
state.get_gui(),
get_state_id(state),
idx,
event.get("text", ""),
pages[0].get("thumbnail", {}) if (pages := event.get("pages", [])) and len(pages) else {},
],
)


# Set a new message or append to an existing message.
# Return the message index in the list.
def set_message(state: State, message: str, idx: t.Optional[int] = None):
if idx is not None and idx < len(state.messages):
msg = state.messages[idx]
state.messages[idx] = (msg[0], message, msg[2])
else:
idx = len(state.messages)
state.messages.append((f"{len(state.messages)}", message, users[0][0]))
state.refresh("messages")
return idx


# Append text to an existing message
def append_to_message(state: State, message: str, idx: int):
if idx < len(state.messages):
msg = state.messages[idx]
state.messages[idx] = (msg[0], f"{msg[1]}{message}", msg[2])
state.refresh("messages")
return idx


# Invoke the Wikipedia API. This is invoked by send_message()
def request_wikipedia(gui: Gui, state_id: str, event_type: str, month: str, day: str):
# Let the user known that a query was sent
idx = invoke_callback(
gui,
state_id,
set_message,
["Fetching information from Wikipedia ..."],
)
request = wiki_url.format(type=event_type, month=month, day=day)
req = requests.get(request, headers={"accept": "application/json; charset=utf-8;", "User-Agent": user_agent})
# Handle the response
if req.status_code == 200:
# Display the response
invoke_callback(
gui,
state_id,
update_message,
[req.json(), event_type, f"{day}/{month}", idx],
)
else:
# Display the error
invoke_callback(
gui,
state_id,
set_message,
[f"Wikipedia API call failed: {req.status_code}", idx],
)


# Invoked by the 'on_action' callback of the chat control when the user presses the Send button
def send_message(state: State, id: str, payload: dict):
args = payload.get("args", [])

# Display the request
state.messages.append((f"{len(state.messages)}", args[2], args[3]))
state.refresh("messages")

# Analyse the request
request = args[2].lower()
type_event = None
for word in event_types:
if word in request:
type_event = event_types[word]
break
type_event = type_event if type_event else "events"

month = None
day = None
for m in re.finditer(r"(\d\d?)", request):
if month is None:
month = m.group()
elif day is None:
day = m.group()
break
if month is None:
month = f"{datetime.datetime.now().month}"
if day is None:
day = f"{datetime.datetime.now().day}"

# Process the request
invoke_long_callback(
state=state,
user_function=request_wikipedia,
user_function_args=[state.get_gui(), get_state_id(state), type_event, month, day],
)


page = """
<|{messages}|chat|users={users}|on_action=send_message|height=80vh|>
"""

if __name__ == "__main__":
Gui(page).run(title="Chat - Ask Wikipedia")
7 changes: 4 additions & 3 deletions tests/rest/test_datanode.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def test_delete_datanode(client):
rep = client.get(user_url)
assert rep.status_code == 404

with mock.patch("taipy.core.data._data_manager._DataManager._delete"), mock.patch(
"taipy.core.data._data_manager._DataManager._get"
with (
mock.patch("taipy.core.data._data_manager._DataManager._delete"),
mock.patch("taipy.core.data._data_manager._DataManager._get"),
):
# test get_datanode
rep = client.delete(url_for("api.datanode_by_id", datanode_id="foo"))
Expand Down Expand Up @@ -63,7 +64,7 @@ def test_get_all_datanodes(client, default_datanode_config_list):
for ds in range(10):
with mock.patch("taipy.rest.api.resources.datanode.DataNodeList.fetch_config") as config_mock:
config_mock.return_value = default_datanode_config_list[ds]
datanodes_url = url_for("api.datanodes", config_id=config_mock.name)
datanodes_url = url_for("api.datanodes", config_id=default_datanode_config_list[ds].name)
client.post(datanodes_url)

rep = client.get(datanodes_url)
Expand Down
7 changes: 4 additions & 3 deletions tests/rest/test_scenario.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def test_delete_scenario(client):
rep = client.get(user_url)
assert rep.status_code == 404

with mock.patch("taipy.core.scenario._scenario_manager._ScenarioManager._delete"), mock.patch(
"taipy.core.scenario._scenario_manager._ScenarioManager._get"
with (
mock.patch("taipy.core.scenario._scenario_manager._ScenarioManager._delete"),
mock.patch("taipy.core.scenario._scenario_manager._ScenarioManager._get"),
):
# test get_scenario
rep = client.delete(url_for("api.scenario_by_id", scenario_id="foo"))
Expand Down Expand Up @@ -64,7 +65,7 @@ def test_get_all_scenarios(client, default_sequence, default_scenario_config_lis
for ds in range(10):
with mock.patch("taipy.rest.api.resources.scenario.ScenarioList.fetch_config") as config_mock:
config_mock.return_value = default_scenario_config_list[ds]
scenarios_url = url_for("api.scenarios", config_id=config_mock.name)
scenarios_url = url_for("api.scenarios", config_id=default_scenario_config_list[ds].name)
client.post(scenarios_url)

rep = client.get(scenarios_url)
Expand Down
7 changes: 4 additions & 3 deletions tests/rest/test_sequence.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ def test_delete_sequence(client):
rep = client.get(user_url)
assert rep.status_code == 404

with mock.patch("taipy.core.sequence._sequence_manager._SequenceManager._delete"), mock.patch(
"taipy.core.sequence._sequence_manager._SequenceManager._get"
with (
mock.patch("taipy.core.sequence._sequence_manager._SequenceManager._delete"),
mock.patch("taipy.core.sequence._sequence_manager._SequenceManager._get"),
):
# test get_sequence
rep = client.delete(url_for("api.sequence_by_id", sequence_id="foo"))
Expand Down Expand Up @@ -73,7 +74,7 @@ def test_get_all_sequences(client, default_scenario_config_list):
for ds in range(10):
with mock.patch("taipy.rest.api.resources.scenario.ScenarioList.fetch_config") as config_mock:
config_mock.return_value = default_scenario_config_list[ds]
scenario_url = url_for("api.scenarios", config_id=config_mock.name)
scenario_url = url_for("api.scenarios", config_id=default_scenario_config_list[ds].name)
client.post(scenario_url)

sequences_url = url_for("api.sequences")
Expand Down
7 changes: 4 additions & 3 deletions tests/rest/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ def test_delete_task(client):
rep = client.get(user_url)
assert rep.status_code == 404

with mock.patch("taipy.core.task._task_manager._TaskManager._delete"), mock.patch(
"taipy.core.task._task_manager._TaskManager._get"
with (
mock.patch("taipy.core.task._task_manager._TaskManager._delete"),
mock.patch("taipy.core.task._task_manager._TaskManager._get"),
):
# test get_task
rep = client.delete(url_for("api.task_by_id", task_id="foo"))
Expand Down Expand Up @@ -64,7 +65,7 @@ def test_get_all_tasks(client, task_data, default_task_config_list):
for ds in range(10):
with mock.patch("taipy.rest.api.resources.task.TaskList.fetch_config") as config_mock:
config_mock.return_value = default_task_config_list[ds]
tasks_url = url_for("api.tasks", config_id=config_mock.name)
tasks_url = url_for("api.tasks", config_id=default_task_config_list[ds].name)
client.post(tasks_url)

rep = client.get(tasks_url)
Expand Down

0 comments on commit 19bb824

Please sign in to comment.