chat_images.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 taipy.gui import Gui, Icon
  22. msgs = [
  23. ["1", "msg 1", "Alice", None],
  24. ["2", "msg From Another unknown User", "Charles", None],
  25. ["3", "This from the sender User", "taipy", "./beatrix-avatar.png"],
  26. ["4", "And from another known one", "Alice", None],
  27. ]
  28. users = [
  29. ["Alice", Icon("./alice-avatar.png", "Alice avatar")],
  30. ["Charles", Icon("./charles-avatar.png", "Charles avatar")],
  31. ["taipy", Icon("./beatrix-avatar.png", "Beatrix avatar")],
  32. ]
  33. def on_action(state, id: str, payload: dict):
  34. (reason, varName, text, senderId, imageData) = payload.get("args", [])
  35. msgs.append([f"{len(msgs) +1 }", text, senderId, imageData])
  36. state.msgs = msgs
  37. page = """
  38. <|{msgs}|chat|users={users}|allow_send_images|>
  39. """
  40. if __name__ == "__main__":
  41. Gui(page).run(title="Chat - Images")