Skip to content

Commit 24e548e

Browse files
committed
Avoid reinitializing virtual display
1 parent 03cd079 commit 24e548e

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

doc/llms/pyimagej-ai-guide.ipynb

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -373,27 +373,30 @@
373373
"# Install system dependencies only if needed\n",
374374
"!command -v mvn >/dev/null 2>&1 && command -v xdpyinfo >/dev/null 2>&1 || (apt-get update && apt-get install -y maven xvfb x11-utils libxext6 libxrender1 libxtst6 libxi6 fonts-dejavu)\n",
375375
"\n",
376-
"# Start up a virtual display and test if it works\n",
377-
"print(\"Setting up virtual display...\")\n",
378-
"os.environ[\"DISPLAY\"] = \":99\"\n",
379-
"xvfb_process = subprocess.Popen([\"Xvfb\", \":99\", \"-screen\", \"0\", \"1024x768x24\"])\n",
380-
"\n",
381-
"# Wait a moment for Xvfb to start\n",
382-
"time.sleep(2)\n",
383-
"\n",
384-
"# Test if display is working\n",
385-
"try:\n",
386-
" result = subprocess.run([\"xdpyinfo\"], capture_output=True, text=True, timeout=5)\n",
387-
" display_working = result.returncode == 0\n",
388-
" if display_working:\n",
389-
" print(\"✓ Virtual display working - using interactive mode\")\n",
390-
" ui_mode = 'interactive'\n",
391-
" else:\n",
392-
" print(\"⚠ Virtual display failed - falling back to headless mode\")\n",
376+
"# Start up a virtual display only if needed\n",
377+
"if \"DISPLAY\" not in os.environ:\n",
378+
" print(\"Setting up virtual display...\")\n",
379+
" os.environ[\"DISPLAY\"] = \":99\"\n",
380+
" xvfb_process = subprocess.Popen([\"Xvfb\", \":99\", \"-screen\", \"0\", \"1024x768x24\"])\n",
381+
"\n",
382+
" # Wait a moment for Xvfb to start\n",
383+
" time.sleep(2)\n",
384+
"\n",
385+
" # Test if display is working\n",
386+
" try:\n",
387+
" result = subprocess.run([\"xdpyinfo\"], capture_output=True, text=True, timeout=5)\n",
388+
" display_working = result.returncode == 0\n",
389+
" if display_working:\n",
390+
" print(\"✓ Virtual display working - using interactive mode\")\n",
391+
" ui_mode = 'interactive'\n",
392+
" else:\n",
393+
" print(\"⚠ Virtual display failed - falling back to headless mode\")\n",
394+
" ui_mode = 'headless'\n",
395+
" except (subprocess.TimeoutExpired, FileNotFoundError):\n",
396+
" print(\"⚠ Cannot test display - falling back to headless mode\")\n",
393397
" ui_mode = 'headless'\n",
394-
"except (subprocess.TimeoutExpired, FileNotFoundError):\n",
395-
" print(\"⚠ Cannot test display - falling back to headless mode\")\n",
396-
" ui_mode = 'headless'\n",
398+
"else:\n",
399+
" print(f\"Display already configured: {os.environ['DISPLAY']}\")\n",
397400
"\n",
398401
"# Install PyImageJ if not already installed\n",
399402
"try:\n",

0 commit comments

Comments
 (0)