add native mode checks and fallback for pywebview in UI launch process; create new pywebview module
This commit is contained in:
@@ -133,6 +133,7 @@ async def read_nng_messages():
|
|||||||
# This shouldn't be necessary
|
# This shouldn't be necessary
|
||||||
global SHUTDOWN
|
global SHUTDOWN
|
||||||
SHUTDOWN = True
|
SHUTDOWN = True
|
||||||
|
logger.debug("Triggering shutdown due to socket in use")
|
||||||
return
|
return
|
||||||
|
|
||||||
# asyncio.create_task(read_nng_messages())
|
# asyncio.create_task(read_nng_messages())
|
||||||
@@ -752,8 +753,56 @@ def launch_app(
|
|||||||
port = native.find_open_port()
|
port = native.find_open_port()
|
||||||
#port = 8001
|
#port = 8001
|
||||||
logger.debug(f"Run on port {port}")
|
logger.debug(f"Run on port {port}")
|
||||||
#ui.run(reload=False, show=True, port=port, native=native_mode, favicon="icon/icon1.ico")
|
|
||||||
|
# If native mode was requested, ensure pywebview has a usable GUI
|
||||||
|
# backend (Qt or GTK). If not present, disable native mode to avoid
|
||||||
|
# runtime WebViewException errors when webview.initialize() runs.
|
||||||
|
if native_mode:
|
||||||
|
try:
|
||||||
|
import webview
|
||||||
|
|
||||||
|
backend_available = False
|
||||||
|
# Check common backends: PyQt5/PySide6 or GTK (gi)
|
||||||
|
try:
|
||||||
|
import PyQt5 # type: ignore
|
||||||
|
backend_available = True
|
||||||
|
except Exception:
|
||||||
|
try:
|
||||||
|
import PySide6 # type: ignore
|
||||||
|
backend_available = True
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
|
||||||
|
if not backend_available:
|
||||||
|
try:
|
||||||
|
# GTK
|
||||||
|
from gi.repository import Gtk # type: ignore
|
||||||
|
|
||||||
|
backend_available = True
|
||||||
|
except Exception:
|
||||||
|
backend_available = False
|
||||||
|
|
||||||
|
if not backend_available:
|
||||||
|
logger.debug(
|
||||||
|
"pywebview backend (Qt/GTK) not available; disabling native mode"
|
||||||
|
)
|
||||||
|
native_mode = False
|
||||||
|
except Exception as e:
|
||||||
|
logger.debug(f"pywebview import/check failed: {e}; disabling native mode")
|
||||||
|
native_mode = False
|
||||||
|
|
||||||
|
# Try to run with the requested mode. If native startup fails at
|
||||||
|
# runtime, catch and fall back to browser mode so the app stays usable.
|
||||||
|
try:
|
||||||
ui.run(reload=False, show=True, port=port, native=native_mode)
|
ui.run(reload=False, show=True, port=port, native=native_mode)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Failed to start UI in native mode: {e}")
|
||||||
|
if native_mode:
|
||||||
|
logger.debug("Retrying without native mode (browser fallback)")
|
||||||
|
try:
|
||||||
|
ui.run(reload=False, show=True, port=port, native=False)
|
||||||
|
except Exception as e2:
|
||||||
|
logger.error(f"Fallback to browser mode failed: {e2}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(e)
|
logger.error(e)
|
||||||
pass
|
pass
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
import webview
|
||||||
|
webview.create_window('Hello world', 'https://pywebview.flowrl.com/hello')
|
||||||
|
webview.start()
|
||||||
Reference in New Issue
Block a user