Falko Schindler 2 лет назад
Родитель
Сommit
a98849ae98
5 измененных файлов с 28 добавлено и 4 удалено
  1. 5 1
      nicegui/client.py
  2. 18 0
      nicegui/functions/open.py
  3. 1 0
      nicegui/templates/index.html
  4. 1 0
      nicegui/ui.py
  5. 3 3
      website/api_docs_and_examples.py

+ 5 - 1
nicegui/client.py

@@ -3,7 +3,7 @@ import json
 import time
 import uuid
 from pathlib import Path
-from typing import Any, Dict, List, Optional
+from typing import Any, Callable, Dict, List, Optional, Union
 
 from fastapi.responses import HTMLResponse
 
@@ -98,3 +98,7 @@ class Client:
                 raise TimeoutError('JavaScript did not respond in time')
             await asyncio.sleep(check_interval)
         return self.waiting_javascript_commands.pop(request_id)
+
+    def open(self, target: Union[Callable, str]) -> None:
+        path = target if isinstance(target, str) else globals.page_routes[target]
+        create_task(globals.sio.emit('open', path, room=str(self.id)))

+ 18 - 0
nicegui/functions/open.py

@@ -0,0 +1,18 @@
+from typing import Callable, Union
+
+from .. import globals
+
+
+def open(target: Union[Callable, str]) -> None:
+    """Open
+
+    Can be used to programmatically trigger redirects for a specific client.
+
+    Note that *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 socket: optional WebSocket defining the target client
+    """
+    path = target if isinstance(target, str) else globals.page_routes[target]
+    globals.client_stack[-1].open(path)

+ 1 - 0
nicegui/templates/index.html

@@ -76,6 +76,7 @@
           });
           window.socket.on("run_method", (msg) => this.$refs['r' + msg.id][msg.name](...msg.args));
           window.socket.on("run_javascript", (msg) => run_javascript(msg['code'], msg['request_id']));
+          window.socket.on("open", (msg) => (location.href = msg));
           window.socket.on("notify", (msg) => Quasar.Notify.create(msg));
           window.socket.on("disconnect", () => window.location.reload());
         },

+ 1 - 0
nicegui/ui.py

@@ -43,6 +43,7 @@ from .elements.upload import Upload as upload
 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
 from .functions.static_files import add_static_files
 from .functions.timer import Timer as timer
 from .functions.update import update

+ 3 - 3
website/api_docs_and_examples.py

@@ -667,14 +667,14 @@ See <https://quasar.dev/layout/page-sticky> for more information.
 
         ui.link('show page with fancy layout', page_layout)
 
-    # @example(ui.open)
+    @example(ui.open, skip=False)
     def ui_open_example():
         @ui.page('/yet_another_page')
         def yet_another_page():
             ui.label('Welcome to yet another page')
-            ui.button('RETURN', on_click=lambda e: ui.open('#open', e.socket))
+            ui.button('RETURN', on_click=lambda: ui.open('/#open'))
 
-        ui.button('REDIRECT', on_click=lambda e: ui.open(yet_another_page, e.socket))
+        ui.button('REDIRECT', on_click=lambda: ui.open(yet_another_page))
 
     @example('''#### Sessions