Skip to content

Commit

Permalink
introduce on_page_ready callback for ui.page
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Aug 10, 2022
1 parent f769a6a commit ea05fc6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions main.py
Expand Up @@ -489,6 +489,7 @@ def lazy_update() -> None:
- `ui.on_startup`: Called when NiceGUI is started or restarted.
- `ui.on_shutdown`: Called when NiceGUI is shut down or restarted.
- `ui.on_connect`: Called when a client connects to NiceGUI. (Optional argument: Starlette request)
- `ui.on_page_ready`: Called when the page is ready and the websocket is connected. (Optional argument: socket)
- `ui.on_disconnect`: Called when a client disconnects from NiceGUI.
When NiceGUI is shut down or restarted, the startup tasks will be automatically canceled.
Expand Down
16 changes: 16 additions & 0 deletions nicegui/elements/page.py
Expand Up @@ -5,6 +5,7 @@
from typing import Callable, Optional

import justpy as jp
from addict import Dict
from pygments.formatters import HtmlFormatter
from starlette.requests import Request

Expand All @@ -23,6 +24,7 @@ def __init__(self,
classes: str = 'q-ma-md column items-start',
css: str = HtmlFormatter().get_style_defs('.codehilite'),
on_connect: Optional[Callable] = None,
on_page_ready: Optional[Callable] = None,
on_disconnect: Optional[Callable] = None,
):
"""Page
Expand All @@ -47,10 +49,12 @@ def __init__(self,
self.tailwind = True # use Tailwind classes instead of Quasars
self.css = css
self.connect_handler = on_connect
self.page_ready_handler = on_page_ready
self.disconnect_handler = on_disconnect

self.waiting_javascript_commands: dict[str, str] = {}
self.on('result_ready', self.handle_javascript_result)
self.on('page_ready', self.handle_page_ready)

self.view = jp.Div(a=self, classes=classes, style='row-gap: 1em', temp=False)
self.view.add_page(self)
Expand All @@ -70,6 +74,18 @@ async def _route_function(self, request: Request):
raise ValueError(f'invalid number of arguments (0 or 1 allowed, got {arg_count})')
return self

async def handle_page_ready(self, msg: Dict) -> bool:
if self.page_ready_handler:
arg_count = len(inspect.signature(self.page_ready_handler).parameters)
is_coro = is_coroutine(self.page_ready_handler)
if arg_count == 1:
await self.page_ready_handler(msg.socket) if is_coro else self.page_ready_handler(msg.socket)
elif arg_count == 0:
await self.page_ready_handler() if is_coro else self.page_ready_handler()
else:
raise ValueError(f'invalid number of arguments (0 or 1 allowed, got {arg_count})')
return False

async def on_disconnect(self, websocket=None) -> None:
for disconnect_handler in ([self.disconnect_handler] if self.disconnect_handler else []) + disconnect_handlers:
await disconnect_handler() if is_coroutine(disconnect_handler) else disconnect_handler()
Expand Down

0 comments on commit ea05fc6

Please sign in to comment.