broadcast.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 using the state.broadcast() method.
  19. # The text of the button that starts or stops the thread is updated on every client's browser
  20. # using a direct assignment of the state property because the variable is declared 'shared'.
  21. # -----------------------------------------------------------------------------------------
  22. from threading import Event, Thread
  23. from time import sleep
  24. from taipy.gui import Gui, State, broadcast_callback
  25. counter = 0
  26. # Thread management
  27. thread = None
  28. thread_event = Event()
  29. button_texts = ["Start", "Stop"]
  30. # Text in the start/stop button (initially "Start")
  31. button_text = button_texts[0]
  32. # Invoked by the timer
  33. def update_counter(state: State, c):
  34. # Update all clients
  35. state.broadcast("counter", c)
  36. def count(event, gui):
  37. while not event.is_set():
  38. global counter
  39. counter = counter + 1
  40. broadcast_callback(gui, update_counter, [counter])
  41. sleep(2)
  42. # Start or stop the timer when the button is pressed
  43. def start_or_stop(state):
  44. global thread
  45. if thread: # Timer is running
  46. thread_event.set()
  47. thread = None
  48. else: # Timer is stopped
  49. thread_event.clear()
  50. thread = Thread(target=count, args=[thread_event, state.get_gui()])
  51. thread.start()
  52. # Update button status.
  53. # Because "button_text" is shared, all clients are updated
  54. state.button_text = button_texts[1 if thread else 0]
  55. page = """# Broadcasting values
  56. Counter: <|{counter}|>
  57. Timer: <|{button_text}|button|on_action=start_or_stop|>
  58. """
  59. # Declare "button_text" as a shared variable.
  60. # Assigning a value to a state's 'button_text' property is propagated to all clients
  61. Gui.add_shared_variable("button_text")
  62. Gui(page).run()