chat-discuss.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. # Copyright 2021-2024 Avaiga Private Limited
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  4. # the License. You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  9. # an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
  10. # specific language governing permissions and limitations under the License.
  11. # -----------------------------------------------------------------------------------------
  12. # To execute this script, make sure that the taipy-gui package is installed in your
  13. # Python environment and run:
  14. # python <script>
  15. # -----------------------------------------------------------------------------------------
  16. # A chatting application based on the chat control.
  17. # In order to see the users' avatars, the image files must be stored next to this script.
  18. # If you want to test this application locally, you need to use several browsers and/or
  19. # incognito windows so a given user's context is not reused.
  20. # -----------------------------------------------------------------------------------------
  21. from os import path
  22. from taipy.gui import Gui, Icon
  23. from taipy.gui.gui_actions import navigate, notify
  24. username = ""
  25. users: list[str|Icon] = []
  26. messages: list[tuple[str, str, str]] = []
  27. Gui.add_shared_variables("messages", "users")
  28. def on_init(state):
  29. # Copy the global variables users and messages to this user's state
  30. state.users = users
  31. state.messages = messages
  32. def on_navigate(state, path: str):
  33. # Navigate to the 'register' page if the user is not registered
  34. if path == "discuss" and state.username == "":
  35. return "register"
  36. return path
  37. def register(state):
  38. # Check that the user is not already registered
  39. for user in users:
  40. if state.username == user or (isinstance(user, (list, tuple)) and state.username == user[0]):
  41. notify(state, "error", "User already registered.")
  42. return
  43. # Use the avatar image if we can find it
  44. avatar_image_file = f"{state.username.lower()}-avatar.png"
  45. if path.isfile(avatar_image_file):
  46. users.append((state.username, Icon(avatar_image_file, state.username)))
  47. else:
  48. users.append(state.username)
  49. # Because users is a shared variable, this propagates to every client
  50. state.users = users
  51. navigate(state, "discuss")
  52. def send(state, _: str, payload: dict):
  53. (_, _, message, sender_id) = payload.get("args", [])
  54. messages.append((f"{len(messages)}", message, sender_id))
  55. state.messages = messages
  56. register_page = """
  57. Please enter your user name:
  58. <|{username}|input|>
  59. <|Submit|button|on_action=register|>
  60. """
  61. discuss_page = """
  62. <|### Let's discuss, {username}|text|mode=markdown|>
  63. <|{messages}|chat|users={users}|sender_id={username}|on_action=send|>
  64. """
  65. pages = {"register": register_page, "discuss": discuss_page}
  66. gui = Gui(pages=pages).run()