Getting started with Taipy (honestly, getting started with UI). So, I've got a lot of questions. Here is my sample page, where I want to display a time widget. I can get it to display the time and I can successfully update the time with a button. But how can I have the time update itself?
from taipy.gui import Gui, notify
from time import strftime
# Function to get the current time
def get_current_time():
return strftime('%I:%M:%S %p')
# Variable to hold the current time
current_time = get_current_time()
# Function to update the time
def update_time(state):
state.current_time = get_current_time()
notify(state, "info", f"Time updated: {state.current_time}")
# Create the page content
page = """
### Current Time
<|{current_time}|text|class_name=time-display|>
<|Update Time|button|on_action=update_time|class_name=update-button|>
"""
gui = Gui(page=page)
#gui.css = css
gui.run(debug=True)
I looked up in ChatGPT and got to something called taipy.core() but I can't find any documentation on this.
Your code is correct and works fine for me.
Now, you need a mechanism that regularly executes some code (like every second) to update the time. Taipy exposes a scheduling system to do so: https://docs.taipy.io/en/release-3.1/manuals/core/scheduling/ Note that it is not part of the core package. It is part of the taipy Enterprise Edition.
Note also that the time update must be done for every user state. The broadcast_callback function can do the job.
import taipy as tp
from taipy.gui import Gui, notify, broadcast_callback
from time import strftime
# Function to get the current time
def get_current_time():
return strftime('%I:%M:%S %p')
# Variable to hold the current time
current_time = strftime('%I:%M:%S %p')
# Function to update the time
def update_time(state):
state.current_time = strftime('%I:%M:%S %p')
notify(state, "info", f"Time updated: {state.current_time}")
def update_time_for_all(_gui: Gui) -> None:
print("Updating time for all...")
broadcast_callback(_gui, update_time)
# Create the page content
page = """
### Current Time
<|{current_time}|text|class_name=time-display|>
<|Update Time|button|on_action=update_time|class_name=update-button|>
"""
if __name__ == "__main__":
gui = Gui(page=page)
tp.Scheduler.every(5).seconds.do(update_time_for_all, gui)
tp.Scheduler.start()
gui.run()