1
0

open.py 1.3 KB

123456789101112131415161718192021222324252627282930
  1. from typing import Any, Callable, Union
  2. from .. import context
  3. from ..client import Client
  4. from ..element import Element
  5. def open(target: Union[Callable[..., Any], str, Element], new_tab: bool = False) -> None: # pylint: disable=redefined-builtin
  6. """Open
  7. Can be used to programmatically trigger redirects for a specific client.
  8. When using the `new_tab` parameter, the browser might block the new tab.
  9. This is a browser setting and cannot be changed by the application.
  10. You might want to use `ui.link` and its `new_tab` parameter instead.
  11. Note: When using an `auto-index page </documentation#auto-index_page>`_ (e.g. no `@page` decorator),
  12. all clients (i.e. browsers) connected to the page will open the target URL unless a socket is specified.
  13. User events like button clicks provide such a socket.
  14. :param target: page function, NiceGUI element on the same page or string that is a an absolute URL or relative path from base URL
  15. :param new_tab: whether to open the target in a new tab (might be blocked by the browser)
  16. """
  17. if isinstance(target, str):
  18. path = target
  19. elif isinstance(target, Element):
  20. path = f'#c{target.id}'
  21. elif callable(target):
  22. path = Client.page_routes[target]
  23. context.get_client().open(path, new_tab)