Fix Linux native window when uv venv hides system PyGObject
Isolated uv venvs cannot import apt-installed python3-gi, so native mode always fell back to the browser. Append system dist-packages and verify Gtk/WebKit typelibs before deciding native mode is unavailable; document uv venv --system-site-packages as the durable install option. Co-authored-by: Diaco Soltanpour <s-diaco@users.noreply.github.com>
This commit is contained in:
13
AGENTS.md
13
AGENTS.md
@@ -28,8 +28,10 @@ Select with `--backend com|ooxml` (CLI) or the Backend dropdown (web UI).
|
||||
interactive selection still works for config loading, but globbing finds no templates.
|
||||
- NiceGUI defaults to a native window (`pywebview`). On Linux that needs **system**
|
||||
GTK/WebKit packages (`python3-gi`, `gir1.2-gtk-3.0`, `gir1.2-webkit2-4.1` via apt) —
|
||||
not installable with `uv`. On this headless VM native mode is unavailable; the app
|
||||
falls back to the browser, or use `--no-browser` / `--browser`.
|
||||
not installable with `uv`. Isolated uv venvs do not see apt `python3-gi` unless
|
||||
created with `uv venv --system-site-packages` (or the app appends
|
||||
`/usr/lib/python3/dist-packages`). On this headless/cloud VM native mode is often
|
||||
unavailable; the app falls back to the browser, or use `--no-browser` / `--browser`.
|
||||
|
||||
### Environment
|
||||
- Prefer **uv** for all installs (`uv` in `~/.local/bin`, on PATH via `~/.bashrc`).
|
||||
@@ -39,6 +41,13 @@ Select with `--backend com|ooxml` (CLI) or the Backend dropdown (web UI).
|
||||
uv sync
|
||||
```
|
||||
|
||||
On a Linux desktop where you want the native window, prefer:
|
||||
|
||||
```bash
|
||||
uv venv --system-site-packages
|
||||
uv sync
|
||||
```
|
||||
|
||||
- Run Python via the venv interpreter: `.venv/bin/python`.
|
||||
- Optional: `XLSX_CLEAN_ROOT` remaps `D:\OpenCloud\...` paths from `file_data.csv`.
|
||||
- Note: the `uv`-created `.venv` does not include `pip`; use `uv pip ...` / `uv sync`
|
||||
|
||||
12
README.md
12
README.md
@@ -80,18 +80,24 @@ That creates `.venv` and installs locked deps from `uv.lock`.
|
||||
### Linux single app window (native mode)
|
||||
|
||||
Native mode uses **pywebview**, which on Linux needs **system** GTK/WebKit packages
|
||||
(`uv sync` cannot install these):
|
||||
(`uv sync` cannot install these). Install them, then sync:
|
||||
|
||||
```bash
|
||||
# Ubuntu / Debian (names may vary by release: webkit2-4.0 vs 4.1)
|
||||
sudo apt install python3-gi gir1.2-gtk-3.0 gir1.2-webkit2-4.1
|
||||
|
||||
# Prefer a venv that can see apt's python3-gi (recommended once):
|
||||
rm -rf .venv
|
||||
uv venv --system-site-packages
|
||||
uv sync
|
||||
|
||||
.venv/bin/python -m xlsx_clean.web_app
|
||||
```
|
||||
|
||||
Without those packages, or on a headless machine (no display), the app falls back to
|
||||
opening the default browser, or you can force:
|
||||
If you already have a normal isolated `.venv`, the app will still try to load
|
||||
system `python3-gi` from `/usr/lib/python3/dist-packages` when those apt packages
|
||||
are installed. Without GTK/WebKit (or on a headless machine with no display), it
|
||||
falls back to opening the default browser, or you can force:
|
||||
|
||||
```bash
|
||||
.venv/bin/python -m xlsx_clean.web_app --browser
|
||||
|
||||
@@ -22,8 +22,11 @@ _LINUX_NATIVE_HINT = (
|
||||
"Linux native window needs system GTK/WebKit packages "
|
||||
"(uv cannot install them), e.g.:\n"
|
||||
" sudo apt install python3-gi gir1.2-gtk-3.0 gir1.2-webkit2-4.1\n"
|
||||
"Isolated uv venvs do not see those packages unless you either:\n"
|
||||
" recreate: rm -rf .venv && uv venv --system-site-packages && uv sync\n"
|
||||
" or rely on this app exposing /usr/lib/python3/dist-packages when present.\n"
|
||||
"Falling back to the default browser. "
|
||||
"Use --no-browser for server-only, or install the packages above for a "
|
||||
"Use --no-browser for server-only, or fix the packages/venv above for a "
|
||||
"single app window."
|
||||
)
|
||||
|
||||
@@ -32,14 +35,56 @@ def _linux_has_display() -> bool:
|
||||
return bool(os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY"))
|
||||
|
||||
|
||||
def _linux_webview_libs_available() -> bool:
|
||||
"""True if GTK (gi) or Qt (qtpy) Python bindings are importable."""
|
||||
try:
|
||||
import gi # noqa: F401
|
||||
def _linux_system_dist_packages() -> list[str]:
|
||||
"""Debian/Ubuntu (and similar) PyGObject install locations."""
|
||||
major, minor = sys.version_info[:2]
|
||||
return [
|
||||
"/usr/lib/python3/dist-packages",
|
||||
f"/usr/lib/python{major}.{minor}/dist-packages",
|
||||
f"/usr/local/lib/python{major}.{minor}/dist-packages",
|
||||
]
|
||||
|
||||
return True
|
||||
|
||||
def _ensure_linux_system_gi_path() -> None:
|
||||
"""Make system python3-gi visible inside an isolated uv/venv.
|
||||
|
||||
apt installs PyGObject into dist-packages; plain `uv sync` venvs do not
|
||||
include system site-packages, so `import gi` fails and NiceGUI falls back
|
||||
to the browser. Exposing dist-packages is enough when typelibs are installed.
|
||||
"""
|
||||
if not sys.platform.startswith("linux"):
|
||||
return
|
||||
for path in _linux_system_dist_packages():
|
||||
if os.path.isdir(path) and path not in sys.path:
|
||||
sys.path.append(path)
|
||||
|
||||
|
||||
def _gi_gtk_webkit_available() -> bool:
|
||||
"""True if pywebview's GTK backend requirements can be satisfied."""
|
||||
try:
|
||||
import gi
|
||||
except ImportError:
|
||||
pass
|
||||
return False
|
||||
try:
|
||||
gi.require_version("Gtk", "3.0")
|
||||
gi.require_version("Gdk", "3.0")
|
||||
except (ValueError, AttributeError):
|
||||
return False
|
||||
try:
|
||||
gi.require_version("WebKit2", "4.1")
|
||||
except ValueError:
|
||||
try:
|
||||
gi.require_version("WebKit2", "4.0")
|
||||
except ValueError:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def _linux_webview_libs_available() -> bool:
|
||||
"""True if GTK (gi) or Qt (qtpy) Python bindings are usable for pywebview."""
|
||||
_ensure_linux_system_gi_path()
|
||||
if _gi_gtk_webkit_available():
|
||||
return True
|
||||
try:
|
||||
import qtpy # noqa: F401
|
||||
|
||||
|
||||
Reference in New Issue
Block a user