浏览代码

fixed ui.open and added ui.link example

Rodja Trappe 3 年之前
父节点
当前提交
7001da2182
共有 3 个文件被更改,包括 12 次插入7 次删除
  1. 5 2
      main.py
  2. 3 3
      nicegui/elements/link.py
  3. 4 2
      nicegui/elements/open.py

+ 5 - 2
main.py

@@ -486,6 +486,9 @@ with example(lifecycle):
 
     ui.on_startup(counter())
 
+with example(ui.link):
+    ui.link('NiceGUI on GitHub', 'https://github.com/zauberzeug/nicegui')
+
 with example(ui.page):
     with ui.page('/other_page'):
         ui.label('Welcome to the other side')
@@ -501,9 +504,9 @@ with example(ui.page):
 with example(ui.open):
     with ui.page('/yet_another_page') as other:
         ui.label('Welcome to yet another page')
-        ui.button('RETURN', on_click=lambda e: ui.open('/', e.socket))
+        ui.button('RETURN', on_click=lambda e: ui.open('#open', e.socket))
 
-    ui.button('REDIRECT', on_click=lambda e: ui.open('/yet_another_page', e.socket))
+    ui.button('REDIRECT', on_click=lambda e: ui.open(other, e.socket))
 
 sessions = """### Sessions
 

+ 3 - 3
nicegui/elements/link.py

@@ -11,10 +11,10 @@ class Link(Group):
     def __init__(self, text: str = '', target: Union[Page, str] = '#'):
         """Link
 
-        Create a link.
+        Create a hyperlink.
 
-        :param text: link text
-        :param target: link target (either a string or a page object)
+        :param text: display text
+        :param target: page or string that is a an absolute URL or relative path from base URL
         """
         href = target if isinstance(target, str) else target.route[1:]
         view = jp.A(text=text, href=href, classes='underline text-blue', temp=False)

+ 4 - 2
nicegui/elements/open.py

@@ -13,14 +13,16 @@ def open(self, target: Union[Page, str], socket: Optional[WebSocket] = None):
 
     Can be used to programmatically trigger redirects for a specific client.
 
-    :param target: page or string that is a relative URL path or an absolute URL
+    :param target: page or string that is a an absolute URL or relative path from base URL
     :param socket: optional WebSocket defining the target client
     """
     create_task(open_async(self, target, socket), name='open_async')
 
 
 async def open_async(self, target: Union[Page, str], socket: Optional[WebSocket] = None):
-    path = target if isinstance(target, str) else target.route
+    path = target if isinstance(target, str) else target.route[1:]
     sockets = [socket] if socket else [s for socket_dict in WebPage.sockets.values() for s in socket_dict.values()]
     for socket in sockets:
+        if not path:
+            path = ' '  # NOTE empty string seems to be transformed to "null" when sending
         await socket.send_json({'type': 'page_update', 'page_options': {'redirect': path}})