Ver código fonte

introduce `ui.page_title`

Falko Schindler 1 ano atrás
pai
commit
6f07092bfd
3 arquivos alterados com 17 adições e 1 exclusões
  1. 3 1
      nicegui/client.py
  2. 12 0
      nicegui/functions/page_title.py
  3. 2 0
      nicegui/ui.py

+ 3 - 1
nicegui/client.py

@@ -63,6 +63,8 @@ class Client:
 
         self.waiting_javascript_commands: Dict[str, Any] = {}
 
+        self.title: Optional[str] = None
+
         self._head_html = ''
         self._body_html = ''
 
@@ -127,7 +129,7 @@ class Client:
             'imports': json.dumps(imports),
             'js_imports': '\n'.join(js_imports),
             'quasar_config': json.dumps(core.app.config.quasar_config),
-            'title': self.page.resolve_title(),
+            'title': self.page.resolve_title() if self.title is None else self.title,
             'viewport': self.page.resolve_viewport(),
             'favicon_url': get_favicon_url(self.page, prefix),
             'dark': str(self.page.resolve_dark()),

+ 12 - 0
nicegui/functions/page_title.py

@@ -0,0 +1,12 @@
+from .. import context, json
+
+
+def page_title(title: str) -> None:
+    """Set the page title for the current client.
+
+    :param title: page title
+    """
+    client = context.get_client()
+    client.title = title
+    if client.has_socket_connection:
+        client.run_javascript(f'document.title = {json.dumps(title)}')

+ 2 - 0
nicegui/ui.py

@@ -87,6 +87,7 @@ __all__ = [
     'run_javascript',
     'notify',
     'open',
+    'page_title',
     'refreshable',
     'state',
     'update',
@@ -188,6 +189,7 @@ from .functions.html import add_body_html, add_head_html
 from .functions.javascript import run_javascript
 from .functions.notify import notify
 from .functions.open import open  # pylint: disable=redefined-builtin
+from .functions.page_title import page_title
 from .functions.refreshable import refreshable, state
 from .functions.update import update
 from .page import page