Files
TanishqandHarry 84e99b3168 fix(webui): start webui.bat from other folders, such as System32 with Run as administrator
webui.bat took the folder it was started in for the project: it looked
for .venv and lib\python there, set PYTHONPATH to it and ran Streamlit
with the relative .\webui\Main.py. Run as administrator starts a batch
file in C:\Windows\System32, and webui.bat started by its path from a
cmd or PowerShell window in another folder runs in that folder. The
launcher then said "Neither project Python, uv, nor streamlit was
found. Please install dependencies first." although the project had its
.venv, or, with uv on PATH, ran uv run streamlit in that folder.

The script now changes to its own folder right after setlocal, as
webui.sh and the mpt launcher already go by their own location. When
the script ends, the endlocal that cmd adds there returns the caller to
its folder. A double-click already starts the script in its own
folder, so Streamlit gets the same folder and arguments there as
before.

The new tests start webui.bat by its full path from System32, with the
project Python in the folders that the existing tests use, and with uv.
2026-11-02 17:22:42 +08:00

64 lines
4.0 KiB
Batchfile

@echo off
setlocal
rem Work in this script's folder, the project, also when it is started elsewhere: Run as administrator starts batch files in C:\Windows\System32.
cd /d "%~dp0"
set "CURRENT_DIR=%CD%"
echo ***** Current directory: "%CURRENT_DIR%" *****
set "PYTHONPATH=%CURRENT_DIR%"
rem set HF_ENDPOINT=https://hf-mirror.com
rem Drop stray spaces from the host, such as the one a cmd one-liner keeps before its second command.
if defined MPT_WEBUI_HOST set "MPT_WEBUI_HOST=%MPT_WEBUI_HOST: =%"
if not defined MPT_WEBUI_HOST set "MPT_WEBUI_HOST=127.0.0.1"
if not defined MPT_WEBUI_PORT set "MPT_WEBUI_PORT=8501"
set "STREAMLIT_CMD="
rem Quote only the python.exe path: quoting the whole assignment leaves the path itself unquoted, and a ")" or "&" in it breaks this block.
if exist "%CURRENT_DIR%\.venv\Scripts\python.exe" (
set STREAMLIT_CMD="%CURRENT_DIR%\.venv\Scripts\python.exe" -m streamlit
) else if exist "%CURRENT_DIR%\lib\python\python.exe" (
set STREAMLIT_CMD="%CURRENT_DIR%\lib\python\python.exe" -m streamlit
) else (
where uv >nul 2>nul
if not errorlevel 1 set "STREAMLIT_CMD=uv run streamlit"
)
if not defined STREAMLIT_CMD (
where streamlit >nul 2>nul
if not errorlevel 1 (
echo ***** Warning: using streamlit from PATH. If dependencies fail, run 'uv sync --frozen' first. *****
set "STREAMLIT_CMD=streamlit"
)
)
if not defined STREAMLIT_CMD (
echo ***** Neither project Python, uv, nor streamlit was found. Please install dependencies first. *****
pause
exit /b 1
)
set "SELECTED_WEBUI_PORT="
rem Dns.GetHostAddresses() rejects 0.0.0.0, the host for LAN access, so IP addresses are parsed and only host names are looked up.
rem ExclusiveAddressUse: without it, a 0.0.0.0 bind succeeds even when a program listens on the same port at 127.0.0.1, and the browser would open that program.
for /f %%P in ('powershell -NoProfile -ExecutionPolicy Bypass -Command "$hostAddress=$null; $parsedAddress=$null; if ([Net.IPAddress]::TryParse($env:MPT_WEBUI_HOST,[ref]$parsedAddress)) { $addresses=@($parsedAddress) } else { $addresses=[Net.Dns]::GetHostAddresses($env:MPT_WEBUI_HOST) }; foreach ($address in $addresses) { if ($address.AddressFamily -eq [Net.Sockets.AddressFamily]::InterNetwork) { $hostAddress=$address; break } }; if ($null -eq $hostAddress) { exit 1 }; $preferred=[int]$env:MPT_WEBUI_PORT; $candidates=New-Object System.Collections.Generic.List[int]; $candidates.Add($preferred); foreach ($candidate in 8502..8599) { if ($candidate -ne $preferred) { $candidates.Add($candidate) } }; foreach ($port in $candidates) { $socket=[Net.Sockets.Socket]::new([Net.Sockets.AddressFamily]::InterNetwork,[Net.Sockets.SocketType]::Stream,[Net.Sockets.ProtocolType]::Tcp); try { $socket.ExclusiveAddressUse=$true; $socket.Bind([Net.IPEndPoint]::new($hostAddress,$port)); $socket.Close(); Write-Output $port; exit 0 } catch { try { $socket.Close() } catch {} } }; exit 1"') do set "SELECTED_WEBUI_PORT=%%P"
if not defined SELECTED_WEBUI_PORT (
echo ***** No available WebUI port found in 8501-8599 for %MPT_WEBUI_HOST%. *****
echo ***** If Windows reports WinError 10013, check reserved ports: netsh interface ipv4 show excludedportrange protocol=tcp *****
pause
exit /b 1
)
if not "%SELECTED_WEBUI_PORT%"=="%MPT_WEBUI_PORT%" (
echo ***** Port %MPT_WEBUI_PORT% is unavailable, using %SELECTED_WEBUI_PORT% instead. *****
)
set "MPT_WEBUI_PORT=%SELECTED_WEBUI_PORT%"
rem 0.0.0.0 makes the WebUI listen on every network interface, but Windows does not connect to 0.0.0.0, so the browser opens 127.0.0.1.
set "BROWSER_WEBUI_HOST=%MPT_WEBUI_HOST%"
if "%MPT_WEBUI_HOST%"=="0.0.0.0" set "BROWSER_WEBUI_HOST=127.0.0.1"
echo ***** WebUI address: http://%BROWSER_WEBUI_HOST%:%MPT_WEBUI_PORT% *****
%STREAMLIT_CMD% run .\webui\Main.py --server.address=%MPT_WEBUI_HOST% --server.port=%MPT_WEBUI_PORT% --browser.serverAddress=%BROWSER_WEBUI_HOST% --browser.gatherUsageStats=False --client.toolbarMode=minimal --logger.hideWelcomeMessage=True --server.showEmailPrompt=False --server.enableCORS=True