chat_discuss.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 typing import Union
  23. from taipy.gui import Gui, Icon
  24. from taipy.gui.gui_actions import navigate, notify
  25. username = ""
  26. users: list[Union[str, Icon]] = []
  27. messages: list[tuple[str, str, str]] = []
  28. Gui.add_shared_variables("messages", "users")
  29. def on_init(state):
  30. # Copy the global variables users and messages to this user's state
  31. state.users = users
  32. state.messages = messages
  33. def on_navigate(state, path: str):
  34. # Navigate to the 'register' page if the user is not registered
  35. if path == "discuss" and state.username == "":
  36. return "register"
  37. return path
  38. def register(state):
  39. # Check that the user is not already registered
  40. for user in users:
  41. if state.username == user or (isinstance(user, (list, tuple)) and state.username == user[0]):
  42. notify(state, "error", "User already registered.")
  43. return
  44. # Use the avatar image if we can find it
  45. avatar_image_file = f"{state.username.lower()}-avatar.png"
  46. if path.isfile(avatar_image_file):
  47. users.append((state.username, Icon(avatar_image_file, state.username)))
  48. else:
  49. users.append(state.username)
  50. # Because users is a shared variable, this propagates to every client
  51. state.users = users
  52. navigate(state, "discuss")
  53. def send(state, _: str, payload: dict):
  54. (_, _, message, sender_id) = payload.get("args", [])
  55. messages.append((f"{len(messages)}", message, sender_id))
  56. state.messages = messages
  57. register_page = """
  58. Please enter your user name:
  59. <|{username}|input|>
  60. <|Submit|button|on_action=register|>
  61. """
  62. discuss_page = """
  63. <|### Let's discuss, {username}|text|mode=markdown|>
  64. <|{messages}|chat|users={users}|sender_id={username}|on_action=send|>
  65. """
  66. pages = {"register": register_page, "discuss": discuss_page}
  67. gui = Gui(pages=pages).run(title="Chat - Discuss")