link.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from typing import Any, Callable, Union
  2. from .. import globals
  3. from ..dependencies import register_component
  4. from ..element import Element
  5. from .mixins.text_element import TextElement
  6. register_component('link', __file__, 'link.js')
  7. class Link(TextElement):
  8. def __init__(self, text: str = '', target: Union[Callable[..., Any], str] = '#', new_tab: bool = False) -> None:
  9. """Link
  10. Create a hyperlink.
  11. To jump to a specific location within a page you can place linkable anchors with `ui.link_target("name")`
  12. and link to it with `ui.link(target="#name")`.
  13. :param text: display text
  14. :param target: page function or string that is a an absolute URL or relative path from base URL
  15. :param new_tab: open link in new tab (default: False)
  16. """
  17. super().__init__(tag='link', text=text)
  18. self._props['href'] = target if isinstance(target, str) else globals.page_routes[target]
  19. self._props['target'] = '_blank' if new_tab else '_self'
  20. self._classes = ['nicegui-link']
  21. class LinkTarget(Element):
  22. def __init__(self, name: str) -> None:
  23. """Link target
  24. Create an anchor tag that can be used as inner-page target for links.
  25. :param name: target name
  26. """
  27. super().__init__('a')
  28. self._props['name'] = name