From e843e56161e3cd05cbfbd0087be5f632396d7393 Mon Sep 17 00:00:00 2001 From: nixevol Date: Wed, 16 Sep 2026 16:42:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=AF=E5=8A=A8=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=AE=B1=E5=89=8D=E7=BB=93=E6=9D=9F=E6=97=A7=E5=AE=9E=E4=BE=8B?= =?UTF-8?q?=E5=B9=B6=E6=89=93=E5=BC=80=E6=B5=8F=E8=A7=88=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- start-ui.ps1 | 109 +++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 98 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 85ce2da..e5a9ad6 100644 --- a/README.md +++ b/README.md @@ -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): diff --git a/start-ui.ps1 b/start-ui.ps1 index 8537bf8..e0d5abd 100644 --- a/start-ui.ps1 +++ b/start-ui.ps1 @@ -1,21 +1,106 @@ $ErrorActionPreference = 'Stop' Set-Location -LiteralPath $PSScriptRoot $env:PYTHONIOENCODING = 'utf-8' -if (Get-Command uv -ErrorAction SilentlyContinue) { - if (-not (Test-Path '.venv-ui/Scripts/python.exe')) { - uv venv .venv-ui --python 3.13 - if ($LASTEXITCODE -ne 0) { throw '创建 Python 环境失败。' } +$HostAddress = '127.0.0.1' +$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 + } } - uv pip install --python .venv-ui/Scripts/python.exe -r requirements-ui.txt - if ($LASTEXITCODE -ne 0) { throw '安装依赖失败。' } - & .venv-ui/Scripts/python.exe -m streamlit run app.py -} else { - if (-not (Test-Path '.venv-ui/Scripts/python.exe')) { + 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 + if ($LASTEXITCODE -ne 0) { throw '创建 Python 环境失败。' } + } + uv pip install --python $Python -r requirements-ui.txt + if ($LASTEXITCODE -ne 0) { throw '安装依赖失败。' } + return + } + if (-not (Test-Path $Python)) { python -m venv .venv-ui 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 '安装依赖失败。' } - & .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 + } +}