-
Hi, I have a datetime picker that should show the current datetime when a user visits my page. However, it seems the selected default datetime is always the one when the server was started. What is the correct way of calculating the current datetime (timestamp) for the default value? My page.py file has this code atm:
And my page.md file has this tag: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi @s-wel, Your date visual element is bound to the I am not sure I understand your use case. 1. You want to initialize your variable at the user session creationYou can use the from datetime import datetime, timedelta
from taipy.gui import Gui, Markdown
def on_init(state):
state.current_time = datetime.now()
if __name__ == "__main__":
current_time = datetime.now()
gui = Gui(Markdown("<|{current_time}|date|with_time|format=dd/MM/yyyy HH:mm:ss|>"))
gui.run() With this piece of code, the 2. You want to display the current time in your applicationIn this case, you need to trigger the variable change when you want it to be updated (for example, every second). from datetime import datetime
import taipy as tp
from taipy.gui import Gui, Markdown
def refresh(gui):
gui.broadcast_change("current_time", datetime.now())
if __name__ == "__main__":
current_time = datetime.now()
gui = Gui(Markdown("<|{current_time}|date|with_time|format=dd/MM/yyyy HH:mm:ss|>"))
tp.Scheduler.every(1).seconds.do(refresh, gui)
tp.Scheduler.start(1)
gui.run() Note that the scheduler functionality is part of Taipy Enterprise Edition. Enregistrement.de.l.ecran.2024-12-24.145849.mp4 |
Beta Was this translation helpful? Give feedback.
Hi @s-wel,
Your date visual element is bound to the
state.start_time
variable, initialized at the application start. Then, your variable is never changed, so its value is always the same.I am not sure I understand your use case.
1. You want to initialize your variable at the user session creation
You can use the
on_init
callback to initialize state variables when a new user connects, such as: