浏览代码

code review (open function instead of class, exception handling)

Falko Schindler 3 年之前
父节点
当前提交
34e8d40d56
共有 2 个文件被更改,包括 12 次插入18 次删除
  1. 11 17
      nicegui/elements/open.py
  2. 1 1
      nicegui/ui.py

+ 11 - 17
nicegui/elements/open.py

@@ -1,23 +1,17 @@
-import asyncio
 from starlette.websockets import WebSocket
+from ..task_logger import create_task
 
 
-class Open:
+def open(self, path: str, socket: WebSocket):
+    """
+    Open
 
-    def __init__(self, path: str, socket: WebSocket):
-        """
-        Open
+    Can be used to programmatically trigger redirects for a specific client.
 
-        Can be used to programmatically trigger redirects for a specific client.
+    :param path: string that is a relative url path or an absolute url
+    :param socket: WebSocket defining the target client
+    """
+    create_task(open_async(path, socket))
 
-        :param path: string that is a relative url path or an absolute url
-        :param socket: WebSocket defining the target client
-        """
-        asyncio.get_event_loop().create_task(self.redirect_async(path, socket))
-
-    @staticmethod
-    async def redirect_async(path: str, socket: WebSocket):
-        # Depends on the 'page_update' in the main.html.
-        await socket.send_json({'type': 'page_update', 'page_options': {'redirect': path}})
-        # So the page itself does not update, return True not None
-        return True
+async def open_async(path: str, socket: WebSocket):
+    await socket.send_json({'type': 'page_update', 'page_options': {'redirect': path}})

+ 1 - 1
nicegui/ui.py

@@ -20,7 +20,7 @@ class Ui:
     from .elements.menu_separator import MenuSeparator as menu_separator
     from .elements.notify import Notify as notify
     from .elements.number import Number as number
-    from .elements.open import Open as open
+    from .elements.open import open, open_async
     from .elements.page import Page as page
     from .elements.radio import Radio as radio
     from .elements.scene import Scene as scene