Jelajahi Sumber

standalone-mode: allow passing window size

Rodja Trappe 2 tahun lalu
induk
melakukan
d4843c06c9
2 mengubah file dengan 17 tambahan dan 8 penghapusan
  1. 4 4
      nicegui/run.py
  2. 13 4
      nicegui/standalone_mode.py

+ 4 - 4
nicegui/run.py

@@ -3,7 +3,7 @@ import multiprocessing
 import os
 import sys
 import webbrowser
-from typing import List, Optional
+from typing import List, Optional, Tuple, Union
 
 import uvicorn
 from uvicorn.main import STARTUP_FAILURE
@@ -22,7 +22,7 @@ def run(*,
         binding_refresh_interval: float = 0.1,
         show: bool = True,
         standalone: bool = False,
-        fullscreen: bool = False,
+        fullscreen: Union[bool, Tuple[int, int]] = False,
         reload: bool = True,
         uvicorn_logging_level: str = 'warning',
         uvicorn_reload_dirs: str = '.',
@@ -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`, overrides `show`, automatically finds an open port)
+    :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 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'`)
@@ -73,7 +73,7 @@ def run(*,
         return
 
     if standalone or fullscreen:
-        standalone_mode.activate(fullscreen)
+        standalone_mode.activate(fullscreen, standalone)
     elif show:
         webbrowser.open(f'http://{host if host != "0.0.0.0" else "127.0.0.1"}:{port}/')
 

+ 13 - 4
nicegui/standalone_mode.py

@@ -5,14 +5,23 @@ import socket
 import tempfile
 import time
 from threading import Thread
+from typing import Tuple, Union
 
 import webview
 
 shutdown = multiprocessing.Event()
 
 
-def open_window(event, fullscreen) -> None:
-    window = webview.create_window('NiceGUI', url='http://localhost:8080', fullscreen=fullscreen)
+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
     webview.start(storage_path=tempfile.mkdtemp())
 
@@ -24,8 +33,8 @@ def check_shutdown() -> None:
         time.sleep(0.1)
 
 
-def activate(fullscreen: bool = False) -> None:
-    multiprocessing.Process(target=open_window, args=(shutdown, fullscreen), daemon=False).start()
+def activate(fullscreen: bool, standalone: Union[bool, Tuple[int, int]] = False) -> None:
+    multiprocessing.Process(target=open_window, args=(shutdown, fullscreen, standalone), daemon=False).start()
     Thread(target=check_shutdown, daemon=True).start()