← Whispering

fix: Add pywebview for native mode and avoid module loading warning

35a61ae6a07c493d0d7924f6abeead048aa18499 · Claude · Dec 1, 2025 20:15

- Add pywebview>=4.0.0 to requirements.txt for native window support
- Make native mode optional: gracefully fall back to browser mode if
  pywebview is not installed
- Change launcher to use direct execution (python3 whispering_ui/main.py)
  instead of module execution (python3 -m whispering_ui.main) to avoid
  RuntimeWarning about module loading
- Update scripts/run_nicegui.sh with direct execution

+50 −5 · 4 files changed

  • requirements.txt +1 −0
  • scripts/run_nicegui.sh +2 −2
  • scripts/run_nicegui_direct.sh +35 −0
  • src/whispering_ui/main.py +12 −3
diff --git a/requirements.txt b/requirements.txt
index e972717..2ea98a5 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -29,3 +29,4 @@ pyperclip
 
 # NiceGUI for modern UI
 nicegui>=1.4.0
+pywebview>=4.0.0  # Required for NiceGUI native mode
diff --git a/scripts/run_nicegui.sh b/scripts/run_nicegui.sh
index 487145f..e1cd0b1 100755
--- a/scripts/run_nicegui.sh
+++ b/scripts/run_nicegui.sh
@@ -29,7 +29,7 @@ export SDL_AUDIODRIVER=pulse
 # Add src to Python path
 export PYTHONPATH="$PROJECT_DIR/src:$PYTHONPATH"
 
-# Change to src directory and run
+# Change to src directory and run (direct execution avoids module loading warning)
 cd "$PROJECT_DIR/src"
 echo "Starting Whispering with NiceGUI interface..."
-python3 -m whispering_ui.main "$@"
+python3 whispering_ui/main.py "$@"
diff --git a/scripts/run_nicegui_direct.sh b/scripts/run_nicegui_direct.sh
new file mode 100755
index 0000000..63cbc15
--- /dev/null
+++ b/scripts/run_nicegui_direct.sh
@@ -0,0 +1,35 @@
+#!/bin/bash
+# Run Whispering with NiceGUI interface (direct execution)
+
+# Get the project directory (parent of scripts directory)
+PROJECT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
+
+# Check if NiceGUI is installed
+if ! python3 -c "import nicegui" 2>/dev/null; then
+    echo "Error: NiceGUI is not installed."
+    echo "Please install dependencies:"
+    echo "  pip install nicegui pyperclip pywebview"
+    exit 1
+fi
+
+# Check if pyperclip is installed
+if ! python3 -c "import pyperclip" 2>/dev/null; then
+    echo "Warning: pyperclip is not installed. Copy/Cut features may not work."
+    echo "Install with: pip install pyperclip"
+fi
+
+# Activate virtual environment if it exists
+if [ -d "$PROJECT_DIR/.venv" ]; then
+    source "$PROJECT_DIR/.venv/bin/activate"
+fi
+
+# Prefer PulseAudio/PipeWire over ALSA for audio
+export SDL_AUDIODRIVER=pulse
+
+# Add src to Python path
+export PYTHONPATH="$PROJECT_DIR/src:$PYTHONPATH"
+
+# Change to src directory and run directly (avoids module loading warning)
+cd "$PROJECT_DIR/src"
+echo "Starting Whispering with NiceGUI interface..."
+python3 whispering_ui/main.py "$@"
diff --git a/src/whispering_ui/main.py b/src/whispering_ui/main.py
index c2327fc..fcf88a8 100644
--- a/src/whispering_ui/main.py
+++ b/src/whispering_ui/main.py
@@ -160,12 +160,21 @@ def main():
     app.on_shutdown(save_settings_on_exit)
 
     # === RUN APPLICATION ===
-    # Run as native desktop window
+    # Try to run as native desktop window, fall back to web if pywebview not available
+    try:
+        import pywebview
+        native_mode = True
+    except ImportError:
+        print("Note: pywebview not installed. Running in browser mode.")
+        print("Install pywebview for native window: pip install pywebview")
+        native_mode = False
+
     ui.run(
         title='Whispering',
-        native=True,
+        native=native_mode,
         window_size=(400 if not state.text_visible else 900, 1000),
-        reload=False
+        reload=False,
+        show=not native_mode  # Auto-open browser if not native mode
     )