feat: 启动工具箱前结束旧实例并打开浏览器
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
## 本地工具箱界面
|
## 本地工具箱界面
|
||||||
|
|
||||||
Windows 双击 `start-ui.cmd`,或在项目目录运行 `./start-ui.ps1`。启动脚本使用独立的 `.venv-ui` 环境,首次安装依赖后在本机打开 Streamlit(默认 `http://127.0.0.1:8501`)。支持 Python 3.13;安装了 uv 时自动使用 uv 创建环境,否则使用系统 Python。原 `.venv` 不会被覆盖。
|
Windows 双击 `start-ui.cmd`,或在项目目录运行 `./start-ui.ps1`。启动时会先结束已有的工具箱界面进程,再使用独立的 `.venv-ui` 环境启动 Streamlit;控制台会打印入口地址(默认 `http://127.0.0.1:8501`)并打开浏览器。支持 Python 3.13;安装了 uv 时自动使用 uv 创建环境,否则使用系统 Python。原 `.venv` 不会被覆盖。
|
||||||
|
|
||||||
手动启动(包括 Linux):
|
手动启动(包括 Linux):
|
||||||
|
|
||||||
|
|||||||
+94
-9
@@ -1,21 +1,106 @@
|
|||||||
$ErrorActionPreference = 'Stop'
|
$ErrorActionPreference = 'Stop'
|
||||||
Set-Location -LiteralPath $PSScriptRoot
|
Set-Location -LiteralPath $PSScriptRoot
|
||||||
$env:PYTHONIOENCODING = 'utf-8'
|
$env:PYTHONIOENCODING = 'utf-8'
|
||||||
if (Get-Command uv -ErrorAction SilentlyContinue) {
|
$HostAddress = '127.0.0.1'
|
||||||
if (-not (Test-Path '.venv-ui/Scripts/python.exe')) {
|
$Port = 8501
|
||||||
|
$EntryUrl = "http://${HostAddress}:${Port}"
|
||||||
|
$Python = Join-Path $PSScriptRoot '.venv-ui\Scripts\python.exe'
|
||||||
|
|
||||||
|
function Test-PortInUse {
|
||||||
|
$pattern = ":$Port\s+"
|
||||||
|
netstat -ano | Where-Object { $_ -match $pattern -and $_ -match 'LISTENING' }
|
||||||
|
}
|
||||||
|
|
||||||
|
function Stop-ExistingUi {
|
||||||
|
$root = $PSScriptRoot
|
||||||
|
$ids = @{}
|
||||||
|
Get-CimInstance Win32_Process | Where-Object {
|
||||||
|
$_.Name -match '^python(\.exe)?$' -and $_.CommandLine -and ($_.CommandLine -match 'streamlit')
|
||||||
|
} | ForEach-Object {
|
||||||
|
$cmd = $_.CommandLine
|
||||||
|
$fromProject = $cmd.IndexOf($root, [StringComparison]::OrdinalIgnoreCase) -ge 0
|
||||||
|
$genericApp = $cmd -match 'streamlit.*run.*app\.py'
|
||||||
|
if ($fromProject -or $genericApp) {
|
||||||
|
$ids[[int]$_.ProcessId] = $true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Test-PortInUse | ForEach-Object {
|
||||||
|
if ($_ -match '\s(\d+)\s*$') {
|
||||||
|
$ids[[int]$Matches[1]] = $true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach ($id in @($ids.Keys)) {
|
||||||
|
if ($id -gt 0) {
|
||||||
|
Stop-Process -Id $id -Force -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$deadline = (Get-Date).AddSeconds(10)
|
||||||
|
do {
|
||||||
|
if (-not (Test-PortInUse)) { return }
|
||||||
|
Start-Sleep -Milliseconds 200
|
||||||
|
} while ((Get-Date) -lt $deadline)
|
||||||
|
throw "端口 $Port 仍被占用,无法启动。"
|
||||||
|
}
|
||||||
|
|
||||||
|
function Wait-UiReady {
|
||||||
|
param($Process)
|
||||||
|
$deadline = (Get-Date).AddSeconds(90)
|
||||||
|
while ((Get-Date) -lt $deadline) {
|
||||||
|
if ($Process.HasExited) {
|
||||||
|
throw 'Streamlit 进程已退出,启动失败。'
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
$client = New-Object System.Net.Sockets.TcpClient
|
||||||
|
$client.Connect($HostAddress, $Port)
|
||||||
|
$client.Close()
|
||||||
|
return
|
||||||
|
} catch {
|
||||||
|
Start-Sleep -Milliseconds 300
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw "等待界面启动超时:$EntryUrl"
|
||||||
|
}
|
||||||
|
|
||||||
|
function Ensure-UiEnvironment {
|
||||||
|
if (Get-Command uv -ErrorAction SilentlyContinue) {
|
||||||
|
if (-not (Test-Path $Python)) {
|
||||||
uv venv .venv-ui --python 3.13
|
uv venv .venv-ui --python 3.13
|
||||||
if ($LASTEXITCODE -ne 0) { throw '创建 Python 环境失败。' }
|
if ($LASTEXITCODE -ne 0) { throw '创建 Python 环境失败。' }
|
||||||
}
|
}
|
||||||
uv pip install --python .venv-ui/Scripts/python.exe -r requirements-ui.txt
|
uv pip install --python $Python -r requirements-ui.txt
|
||||||
if ($LASTEXITCODE -ne 0) { throw '安装依赖失败。' }
|
if ($LASTEXITCODE -ne 0) { throw '安装依赖失败。' }
|
||||||
& .venv-ui/Scripts/python.exe -m streamlit run app.py
|
return
|
||||||
} else {
|
}
|
||||||
if (-not (Test-Path '.venv-ui/Scripts/python.exe')) {
|
if (-not (Test-Path $Python)) {
|
||||||
python -m venv .venv-ui
|
python -m venv .venv-ui
|
||||||
if ($LASTEXITCODE -ne 0) { throw '创建 Python 环境失败。' }
|
if ($LASTEXITCODE -ne 0) { throw '创建 Python 环境失败。' }
|
||||||
}
|
}
|
||||||
& .venv-ui/Scripts/python.exe -m pip install -r requirements-ui.txt
|
& $Python -m pip install -r requirements-ui.txt
|
||||||
if ($LASTEXITCODE -ne 0) { throw '安装依赖失败。' }
|
if ($LASTEXITCODE -ne 0) { throw '安装依赖失败。' }
|
||||||
& .venv-ui/Scripts/python.exe -m streamlit run app.py
|
|
||||||
}
|
}
|
||||||
exit $LASTEXITCODE
|
|
||||||
|
Stop-ExistingUi
|
||||||
|
Ensure-UiEnvironment
|
||||||
|
|
||||||
|
Write-Host ''
|
||||||
|
Write-Host "正在启动 MarketGrid 工具箱"
|
||||||
|
Write-Host "入口地址:$EntryUrl"
|
||||||
|
Write-Host ''
|
||||||
|
|
||||||
|
$psi = New-Object System.Diagnostics.ProcessStartInfo
|
||||||
|
$psi.FileName = $Python
|
||||||
|
$psi.Arguments = "-m streamlit run `"$PSScriptRoot\app.py`" --server.headless true --server.address $HostAddress --server.port $Port --browser.serverAddress $HostAddress --browser.gatherUsageStats false"
|
||||||
|
$psi.WorkingDirectory = $PSScriptRoot
|
||||||
|
$psi.UseShellExecute = $false
|
||||||
|
$ui = [System.Diagnostics.Process]::Start($psi)
|
||||||
|
try {
|
||||||
|
Wait-UiReady -Process $ui
|
||||||
|
Write-Host "已启动:$EntryUrl"
|
||||||
|
Start-Process $EntryUrl
|
||||||
|
$ui.WaitForExit()
|
||||||
|
exit $ui.ExitCode
|
||||||
|
} finally {
|
||||||
|
if (-not $ui.HasExited) {
|
||||||
|
Stop-Process -Id $ui.Id -Force -ErrorAction SilentlyContinue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user