nicegui icon indicating copy to clipboard operation
nicegui copied to clipboard

Absence of ui.on_page_ready

Open me21 opened this issue 3 years ago • 2 comments

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.

me21 avatar Oct 21 '22 07:10 me21

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.

falkoschindler avatar Oct 21 '22 07:10 falkoschindler

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()))

rodja avatar Oct 22 '22 03:10 rodja

Would the callback provide a page object or a websocket object?

me21 avatar Oct 22 '22 23:10 me21

It provides a websocket object. See https://github.com/zauberzeug/nicegui/blob/38053252af34b7aeeaebd84ec762da2de4c8b627/nicegui/page.py#L85-L92

rodja avatar Oct 23 '22 05:10 rodja

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.

rodja avatar Oct 24 '22 12:10 rodja