正是@gama11的建议起了作用,所以他们的答案是公认的答案。
我更改了保存的代码工作区中的设置,而不是全局代码设置。我还添加了一些逻辑,以便在调用该脚本的文件夹中启动新的代码工作区。这样,每个工作区都可以有自己的独立Jupyter笔记本服务器。
为了完整起见,下面是我的PowerShell脚本。
## Set $remoteName to either remote server IP or network name
# $remoteName = "111.111.111.111"
$remoteName = "ServerNetworkName"
$cred = Get-Credential -UserName "$env:username" -Message "network username and password"
$jobname = Read-Host 'Enter a name for the remote job'
$s2 = New-PSSession -ComputerName $remoteName -Name $jobname -Credential $cred
if ($s2 -eq $null){
Write-Host "Log in failed"
sleep 3
Exit
}
Invoke-Command -Session $s2 -ScriptBlock {
$env:PYTHONPATH = "C:\Users\UserName\Miniconda3";
$env:Path += ";C:\Users\UserName\Miniconda3";
$env:Path += ";C:\Users\UserName\Miniconda3\Library\mingw-w64\bin";
$env:Path += ";C:\Users\UserName\Miniconda3\Library\usr\bin";
$env:Path += ";C:\Users\UserName\Miniconda3\Library\bin";
$env:Path += ";C:\Users\UserName\Miniconda3\Scripts";
$env:Path += ";C:\nltk_data";
$env:Path += ";C:\Users\UserName\scripts";
C:\Users\UserName\scripts\AdditionalSettingsFile.ps1;
cd "C:\Users"
}
$jnCommand = [scriptblock]::Create("jupyter lab --no-browser --ip=$remoteName")
$jn = Invoke-Command -Session $s2 -ScriptBlock $jnCommand -AsJob
$jo = $null
$timeout = new-timespan -Seconds 30
$sw = [diagnostics.stopwatch]::StartNew()
do{
Receive-Job -Name $jn.Name -Keep -ErrorVariable jo
$jo = $jo | select-string "URLs:" | Out-String
$jnRunning = $jo.Contains("URLs:")
sleep 2
}until(($jnRunning -eq $True) -or ($sw.elapsed -ge $timeout))
$splt = "URLs:", ""
$option = [System.StringSplitOptions]::RemoveEmptyEntries
$jurl = $jo.split($splt, 2, $option)[1].Trim()
## -IdleTimeoutSec in sec/min * min/hr * hrs/day * days
## 60*60*24*3 = 259200
Disconnect-PSSession -Session $s2 -IdleTimeoutSec (60*60*24*3)
$WorkSpacesPath = "C:\Users\UserName\AppData\Roaming\Code\User\workspaceStorage"
$wsArray = (
Get-Item -Path $CodeWorkSpaces\*\*.json | `
Foreach-Object {
(Get-Content ($_.FullName) | ConvertFrom-Json).configuration `
-Replace 'file:///|[\s]+', '' `
-Replace '/', '\' `
-Replace '%3a', ':' `
-Replace '%20', ' ' `
}
) | `
Where-Object { $_ } | `
Get-Unique -AsString | `
Foreach-Object {
Get-Item -Path $_ -EA SilentlyContinue | `
Select-Object -Property BaseName, FullName, LastAccessTime `
} | `
Sort-Object -Property LastAccessTime
$cwd = Get-Location
$NewSettings = [PSCustomObject]@{BaseName="New Workspace"; FullName=$cwd; LastAccessTime=Get-Date}
$wsArray += $NewSettings
$idx = 0
$wsArray | Foreach-Object {$_ | Add-Member @{Index = $idx } -Force; $idx++ }
$wsArray | Select-Object -Property Index, BaseName, LastAccessTime | Format-Table *
$idxSel = Read-Host 'Select workspace index'
$SelPath = $wsArray[$idxSel].FullName
$SelName = $wsArray[$idxSel].BaseName
if ($SelName -eq $NewSettings.BaseName) {
if ($jurl -eq $null) {$jurl = "local"}
[PSCustomObject]@{
"python.dataScience.jupyterServerURI"=$jurl
} | `
ConvertTo-Json | `
Set-Content ("$SelPath\.vscode\settings.json")
code .
} else {
$SelCont = Get-Content($SelPath) | ConvertFrom-Json
$SelCont.settings | `
Add-Member `
-NotePropertyName "python.dataScience.jupyterServerURI" `
-NotePropertyValue $jurl `
-Force
$SelCont | ConvertTo-Json | Set-Content ($SelPath)
code $SelPath
}
之后的脚本的最后部分
$WorkSpacesPath
只有在以下情况下才能工作:
-
已安装vs代码,
-
已安装并启用'ms python.python'扩展,
显然,您需要更改遥控器的
$PATH
指向要在远程计算机上运行的python的安装和其他文件的位置。
请注意
select-string "URLs:"
和
.Contains("URLs:")
为Jupyter的最新版本(相对于本帖)工作。以前,字符串是
"token:"
但Jupyter团队改变了启动输出。没有什么可以阻止他们再次更改它并破坏上面的代码。
这可以很容易地更改为启动Jupyter实验室而不是VS代码。为此,只需在
$CodeSettingsPath
到以下(假设您安装了Google Chrome)。
Start-Process chrome.exe --app=$jurl
如果你想启动一个Jupyter笔记本,你需要更换
lab
具有
notebook
在
$jnCommand
变量。