Browse Source

Merge pull request #2108 from retsyo/patch-1

make ui.open accept NiceGUI element as link target
Falko Schindler 1 year ago
parent
commit
f143a1b670
1 changed files with 9 additions and 3 deletions
  1. 9 3
      nicegui/functions/open.py

+ 9 - 3
nicegui/functions/open.py

@@ -2,10 +2,11 @@ from typing import Any, Callable, Union
 
 from .. import context
 from ..client import Client
+from ..element import Element
 from ..logging import log
 
 
-def open(target: Union[Callable[..., Any], str], new_tab: bool = False) -> None:  # pylint: disable=redefined-builtin
+def open(target: Union[Callable[..., Any], str, Element], new_tab: bool = False) -> None:  # pylint: disable=redefined-builtin
     """Open
 
     Can be used to programmatically trigger redirects for a specific client.
@@ -18,10 +19,15 @@ def open(target: Union[Callable[..., Any], str], new_tab: bool = False) -> None:
     all clients (i.e. browsers) connected to the page will open the target URL unless a socket is specified.
     User events like button clicks provide such a socket.
 
-    :param target: page function or string that is a an absolute URL or relative path from base URL
+    :param target: page function, NiceGUI element on the same page or string that is a an absolute URL or relative path from base URL
     :param new_tab: whether to open the target in a new tab (might be blocked by the browser)
     """
-    path = target if isinstance(target, str) else Client.page_routes[target]
+    if isinstance(target, str):
+        path = target
+    elif isinstance(target, Element):
+        path = f'#c{target.id}'
+    elif callable(target):
+        path = Client.page_routes[target]
     client = context.get_client()
     if client.has_socket_connection:
         client.open(path, new_tab)