nicegui
nicegui copied to clipboard
Absence of ui.on_page_ready
The NiceGUI reference mentions ui.on_page_ready handler at https://nicegui.io/reference#lifecycle
But I couldn't find it in the IntelliSense hints when I type ui.. Are you sure this handler exists? Other handlers are defined in lifecycle.py.
Oh, this is a mistake in the documentation. on_page_ready is (currently) only available as an argument for the page decorator: https://github.com/zauberzeug/nicegui/blob/38053252af34b7aeeaebd84ec762da2de4c8b627/nicegui/page.py#L160
We need to decide if we only fix the documentation or provide a ui.on_page_ready like the other lifecycle hooks.
I think providing ui.on_page_ready would be good. That way you can access local variables. Consider the example:
@ui.page('/')
def index():
loaded = ui.label()
ui.on_page_ready(lambda: loaded.set_text(time.time()))
Would the callback provide a page object or a websocket object?
It provides a websocket object. See https://github.com/zauberzeug/nicegui/blob/38053252af34b7aeeaebd84ec762da2de4c8b627/nicegui/page.py#L85-L92
Oh. We already have something like ui.on_page_ready. You can write
@ui.page('/')
def index():
loaded = ui.label()
yield
loaded.set_text(time.time())
Everything after yield is executed after the page becomes ready. @falkoschindler has already removed the documentation about ui.on_page_ready. In #130 we are currently thinking about a solution to provide the web socket with the yield statement.