broadcast.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. # Demonstrate how to share variable values across multiple clients.
  17. # This application creates a thread that increments a value every few seconds.
  18. # The value is updated for every client in a function invoked by Gui.broadcast_change().
  19. # The text of the button that starts or stops the thread is updated using the
  20. # State.assign() method, and udateds on every client's browser because the variable was
  21. # declared 'shared' by calling Gui.add_shared_variable().
  22. # -----------------------------------------------------------------------------------------
  23. from threading import Event, Thread
  24. from time import sleep
  25. from taipy.gui import Gui, State
  26. counter = 0
  27. # Thread management
  28. thread = None
  29. thread_event = Event()
  30. def count(event, gui):
  31. while not event.is_set():
  32. global counter
  33. counter = counter + 1
  34. gui.broadcast_change("counter", counter)
  35. sleep(2)
  36. # Start or stop the timer when the button is pressed
  37. def start_or_stop(state: State):
  38. global thread
  39. if thread: # Timer is running
  40. thread_event.set()
  41. thread = None
  42. else: # Timer is stopped
  43. thread_event.clear()
  44. thread = Thread(target=count, args=[thread_event, state.get_gui()])
  45. thread.start()
  46. # Update button status for all states.
  47. state.assign("button_text", button_texts[1 if thread else 0])
  48. button_texts = ["Start", "Stop"]
  49. # Text in the start/stop button (initially "Start")
  50. button_text = button_texts[0]
  51. page = """
  52. Counter: <|{counter}|>
  53. Timer: <|{button_text}|button|on_action=start_or_stop|>
  54. """
  55. if __name__ == "__main__":
  56. # Declare "button_text" as a shared variable.
  57. # Assigning a value to a state's 'button_text' property is propagated to all clients
  58. Gui.add_shared_variable("button_text")
  59. Gui(page).run(title="Broadcasting values")