-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add examples for the chat control (#1631)
- Examples for the chat control - Example for the table control - Remove fallback hover text in Chat component - Hide JsonAdapter class - Minor fixes in reference and elements docs
- Loading branch information
1 parent
bd686a4
commit 1ad66d6
Showing
15 changed files
with
230 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# 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> | ||
# ----------------------------------------------------------------------------------------- | ||
# Human-computer dialog UI based on the chat control. | ||
# ----------------------------------------------------------------------------------------- | ||
from math import cos, pi, sin, sqrt, tan # noqa: F401 | ||
|
||
from taipy.gui import Gui | ||
|
||
# The user interacts with the Python interpreter | ||
users = ["human", "Result"] | ||
messages: list[tuple[str, str, str]] = [] | ||
|
||
|
||
def evaluate(state, var_name: str, payload: dict): | ||
# Retrieve the callback parameters | ||
(_, _, expression, sender_id) = payload.get("args", []) | ||
# Add the input content as a sent message | ||
messages.append((f"{len(messages)}", expression, sender_id)) | ||
# Default message used if evaluation fails | ||
result = "Invalid expression" | ||
try: | ||
# Evaluate the expression and store the result | ||
result = f"= {eval(expression)}" | ||
except Exception: | ||
pass | ||
# Add the result as an incoming message | ||
messages.append((f"{len(messages)}", result, users[1])) | ||
state.messages = messages | ||
|
||
|
||
page = """ | ||
<|{messages}|chat|users={users}|sender_id={users[0]}|on_action=evaluate|> | ||
""" | ||
|
||
Gui(page).run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# 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> | ||
# ----------------------------------------------------------------------------------------- | ||
# A chatting application based on the chat control. | ||
# In order to see the users' avatars, the image files must be stored next to this script. | ||
# If you want to test this application locally, you need to use several browsers and/or | ||
# incognito windows so a given user's context is not reused. | ||
# ----------------------------------------------------------------------------------------- | ||
from os import path | ||
|
||
from taipy.gui import Gui, Icon | ||
from taipy.gui.gui_actions import navigate, notify | ||
|
||
username = "" | ||
users: list[str|Icon] = [] | ||
messages: list[tuple[str, str, str]] = [] | ||
|
||
Gui.add_shared_variables("messages", "users") | ||
|
||
|
||
def on_init(state): | ||
# Copy the global variables users and messages to this user's state | ||
state.users = users | ||
state.messages = messages | ||
|
||
|
||
def on_navigate(state, path: str): | ||
# Navigate to the 'register' page if the user is not registered | ||
if path == "discuss" and state.username == "": | ||
return "register" | ||
return path | ||
|
||
|
||
def register(state): | ||
# Check that the user is not already registered | ||
for user in users: | ||
if state.username == user or (isinstance(user, (list, tuple)) and state.username == user[0]): | ||
notify(state, "error", "User already registered.") | ||
return | ||
# Use the avatar image if we can find it | ||
avatar_image_file = f"{state.username.lower()}-avatar.png" | ||
if path.isfile(avatar_image_file): | ||
users.append((state.username, Icon(avatar_image_file, state.username))) | ||
else: | ||
users.append(state.username) | ||
# Because users is a shared variable, this propagates to every client | ||
state.users = users | ||
navigate(state, "discuss") | ||
|
||
|
||
def send(state, _: str, payload: dict): | ||
(_, _, message, sender_id) = payload.get("args", []) | ||
messages.append((f"{len(messages)}", message, sender_id)) | ||
state.messages = messages | ||
|
||
|
||
register_page = """ | ||
Please enter your user name: | ||
<|{username}|input|> | ||
<|Submit|button|on_action=register|> | ||
""" | ||
|
||
discuss_page = """ | ||
<|### Let's discuss, {username}|text|mode=markdown|> | ||
<|{messages}|chat|users={users}|sender_id={username}|on_action=send|> | ||
""" | ||
|
||
pages = {"register": register_page, "discuss": discuss_page} | ||
gui = Gui(pages=pages).run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# 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 | ||
|
||
from taipy.gui import Gui | ||
|
||
stock = { | ||
"date": [datetime.datetime(year=2000, month=12, day=d) for d in range(20, 30)], | ||
"price": [119.88, 112.657, 164.5, 105.42, 188.36, 103.9, 143.97, 160.11, 136.3, 174.06], | ||
"change": [7.814, -5.952, 0.01, 8.781, 7.335, 6.623, -6.635, -6.9, 0.327, -0.089], | ||
"volume": [773, 2622, 2751, 1108, 7400, 3772, 9398, 4444, 9264, 1108], | ||
} | ||
|
||
columns = { | ||
"date" : {"title": "Data", "format": "MMM d"}, | ||
"price" : {"title": "Price", "format": "$%.02f"}, | ||
"change" : {"title": "% change", "format": "%.01f"}, | ||
"volume" : {"title": "Volume"} | ||
} | ||
|
||
page = """ | ||
# Formatting cells in a table | ||
<|{stock}|table|columns={columns}|> | ||
""" | ||
|
||
Gui(page).run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,9 +26,6 @@ | |
""" # noqa W291 | ||
|
||
page = """ | ||
# Text - Markdown | ||
<|toggle|theme|> | ||
<|{markdown}|text|mode=markdown|> | ||
""" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,9 +24,6 @@ def say_hello(name: str): | |
""" | ||
|
||
page = """ | ||
# Text - pre | ||
<|toggle|theme|> | ||
<|{code}|text|mode=pre|> | ||
""" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,9 +18,6 @@ | |
name = "Taipy" | ||
|
||
page = """ | ||
# Text - simple | ||
<|toggle|theme|> | ||
<|Hello {name}!|> | ||
""" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
|
||
|
||
class JsonAdapter(ABC): | ||
"""NOT DOCUMENTED""" | ||
def register(self): | ||
_TaipyJsonAdapter().register(self) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.