瀏覽代碼

#519 code review

Falko Schindler 2 年之前
父節點
當前提交
4dbcce61c1
共有 2 個文件被更改,包括 22 次插入22 次删除
  1. 14 6
      nicegui/run.py
  2. 8 16
      nicegui/standalone_mode.py

+ 14 - 6
nicegui/run.py

@@ -44,7 +44,7 @@ def run(*,
     :param dark: whether to use Quasar's dark mode (default: `False`, use `None` for "auto" mode)
     :param binding_refresh_interval: time between binding updates (default: `0.1` seconds, bigger is more CPU friendly)
     :param show: automatically open the UI in a browser tab (default: `True`)
-    :param standalone: open the UI in a standalone window (default: `False`, accepts size as tuple or True (800, 600), overrules `show` parameter, automatically finds an open port)
+    :param standalone: open the UI in a standalone window (default: `False`, accepts size as tuple or True (800, 600), deactivates `show`, automatically finds an open port)
     :param fullscreen: open the UI in a fullscreen, standalone window (default: `False`, also activates `standalone`)
     :param reload: automatically reload the UI on file changes (default: `True`)
     :param uvicorn_logging_level: logging level for uvicorn server (default: `'warning'`)
@@ -57,9 +57,15 @@ def run(*,
     :param kwargs: additional keyword arguments are passed to `uvicorn.run`
     '''
     globals.ui_run_has_been_called = True
+
+    if fullscreen:
+        standalone = True
+    if standalone:
+        port = standalone_mode.find_open_port()  # NOTE search for open port to avoid conflict with other apps
+        show = False
+
     globals.host = host
-    # NOTE when running standalone, we search for an open port to make sure we don't conflict with other apps
-    globals.port = standalone_mode.find_open_port() if standalone or fullscreen else port
+    globals.port = port
     globals.reload = reload
     globals.title = title
     globals.viewport = viewport
@@ -72,9 +78,11 @@ def run(*,
     if multiprocessing.current_process().name != 'MainProcess':
         return
 
-    if standalone or fullscreen:
-        standalone_mode.activate(fullscreen, standalone)
-    elif show:
+    if standalone:
+        width, height = (800, 600) if standalone is True else standalone
+        standalone_mode.activate(f'http://localhost:{port}', title, width, height, fullscreen)
+
+    if show:
         webbrowser.open(f'http://{host if host != "0.0.0.0" else "127.0.0.1"}:{port}/')
 
     def split_args(args: str) -> List[str]:

+ 8 - 16
nicegui/standalone_mode.py

@@ -5,24 +5,15 @@ import socket
 import tempfile
 import time
 from threading import Thread
-from typing import Tuple, Union
 
 import webview
 
 shutdown = multiprocessing.Event()
 
 
-def open_window(event: multiprocessing.Event, fullscreen: bool, standalone: Union[bool, Tuple[int, int]]) -> None:
-    if standalone is True:
-        width, height = 800, 600
-    else:
-        width, height = standalone
-    window = webview.create_window(
-        'NiceGUI', url='http://localhost:8080',
-        fullscreen=fullscreen,
-        width=width, height=height
-    )
-    window.events.closing += event.set  # signal that the program should be closed to the main process
+def open_window(url: str, title: str, width: int, height: int, fullscreen: bool, shutdown: multiprocessing.Event) -> None:
+    window = webview.create_window(title, url=url, width=width, height=height, fullscreen=fullscreen)
+    window.events.closing += shutdown.set  # signal that the program should be closed to the main process
     webview.start(storage_path=tempfile.mkdtemp())
 
 
@@ -33,13 +24,14 @@ def check_shutdown() -> None:
         time.sleep(0.1)
 
 
-def activate(fullscreen: bool, standalone: Union[bool, Tuple[int, int]] = False) -> None:
-    multiprocessing.Process(target=open_window, args=(shutdown, fullscreen, standalone), daemon=False).start()
+def activate(url: str, title: str, width: int, height: int, fullscreen: bool) -> None:
+    args = url, title, width, height, fullscreen, shutdown
+    multiprocessing.Process(target=open_window, args=args, daemon=False).start()
     Thread(target=check_shutdown, daemon=True).start()
 
 
-def find_open_port(start_port=8000, end_port=9000):
-    for port in range(start_port, end_port+1):
+def find_open_port(start_port: int = 8000, end_port: int = 8999) -> int:
+    for port in range(start_port, end_port + 1):
         try:
             with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
                 s.bind(('localhost', port))