VS Code 启动命令行参数以更改扩展参数
VS Code launch command line arguments to change extension parameters
从命令行启动 VS Code 时如何指定 VS Code 扩展参数?
具体来说,我想将 Jupyter 服务器的 URI 输入到 MS Python 扩展中。
我可以在 VS Code 启动后执行此操作,方法是选择 ctrl
+shift
+P
并选择 Python: Specify Jupyter server URI
,然后选择 Type in the URI for the Jupyter Server
,最后输入 Jupyter 服务器的 URI。此处描述:https://code.visualstudio.com/docs/python/jupyter-support#_connect-to-a-remote-jupyter-server
我已经有一个 Powershell 脚本可以登录并启动远程计算机上的 Jupyter 服务器,使用身份验证令牌捕获 URI,并使用远程计算机自动启动 Jupyter notebook 或 Jupyter labs 的本地实例Jupyter 服务器。
我还希望可以选择使用远程 Jupyter 服务器启动 VS Code。请注意,每次在远程计算机上启动时,Jupyter 服务器 URI 都会更改。
启动 VS Code 和动态更改扩展参数的命令行参数是什么?
我在这里没有找到任何东西:
https://vscode.readthedocs.io/en/latest/editor/command-line/#additional-command-line-arguments
我不确定是否可以通过命令行参数实现。但是,由于这似乎是一个设置,您可以在开始 VSCode:
之前相应地修改工作区的 settings.json
{
"python.dataScience.jupyterServerURI": <uri>
}
@Gama11 的建议奏效了,所以他们的答案是公认的答案。
我更改了保存的代码工作区中的设置,而不是全局代码设置。我还添加了一些逻辑以在调用此脚本的文件夹中启动一个新的代码工作区。这样每个工作区都可以拥有自己独立的 Jupyter notebook 服务器。
为了完整起见,下面是我的 Powershell 脚本。
编辑 以反映最近对 vscode 核心的更改以及它如何保存对工作区文件的引用。可能是更好的方法,但这就足够了。
## 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
## BEGIN EDIT
$wsArray += (
(Get-Item -Path $CodeWorkSpaces\*\*.json | `
Foreach-Object {
(Get-Content ($_.FullName) | ConvertFrom-Json).configuration.fsPath `
}
) | `
Where-Object { $_ } | `
Get-Unique -AsString | `
Foreach-Object {
Get-Item -Path $_ -EA SilentlyContinue | `
Select-Object -Property BaseName, FullName, LastAccessTime `
} | `
Sort-Object -Property LastAccessTime `
)
## END EDIT
$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 Code 已安装,
- 已安装并启用 '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 notebook,则需要将 $jnCommand
变量中的 lab
替换为 notebook
。
从命令行启动 VS Code 时如何指定 VS Code 扩展参数?
具体来说,我想将 Jupyter 服务器的 URI 输入到 MS Python 扩展中。
我可以在 VS Code 启动后执行此操作,方法是选择 ctrl
+shift
+P
并选择 Python: Specify Jupyter server URI
,然后选择 Type in the URI for the Jupyter Server
,最后输入 Jupyter 服务器的 URI。此处描述:https://code.visualstudio.com/docs/python/jupyter-support#_connect-to-a-remote-jupyter-server
我已经有一个 Powershell 脚本可以登录并启动远程计算机上的 Jupyter 服务器,使用身份验证令牌捕获 URI,并使用远程计算机自动启动 Jupyter notebook 或 Jupyter labs 的本地实例Jupyter 服务器。
我还希望可以选择使用远程 Jupyter 服务器启动 VS Code。请注意,每次在远程计算机上启动时,Jupyter 服务器 URI 都会更改。
启动 VS Code 和动态更改扩展参数的命令行参数是什么?
我在这里没有找到任何东西: https://vscode.readthedocs.io/en/latest/editor/command-line/#additional-command-line-arguments
我不确定是否可以通过命令行参数实现。但是,由于这似乎是一个设置,您可以在开始 VSCode:
之前相应地修改工作区的settings.json
{
"python.dataScience.jupyterServerURI": <uri>
}
@Gama11 的建议奏效了,所以他们的答案是公认的答案。
我更改了保存的代码工作区中的设置,而不是全局代码设置。我还添加了一些逻辑以在调用此脚本的文件夹中启动一个新的代码工作区。这样每个工作区都可以拥有自己独立的 Jupyter notebook 服务器。
为了完整起见,下面是我的 Powershell 脚本。
编辑 以反映最近对 vscode 核心的更改以及它如何保存对工作区文件的引用。可能是更好的方法,但这就足够了。
## 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
## BEGIN EDIT
$wsArray += (
(Get-Item -Path $CodeWorkSpaces\*\*.json | `
Foreach-Object {
(Get-Content ($_.FullName) | ConvertFrom-Json).configuration.fsPath `
}
) | `
Where-Object { $_ } | `
Get-Unique -AsString | `
Foreach-Object {
Get-Item -Path $_ -EA SilentlyContinue | `
Select-Object -Property BaseName, FullName, LastAccessTime `
} | `
Sort-Object -Property LastAccessTime `
)
## END EDIT
$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 Code 已安装,
- 已安装并启用 '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 notebook,则需要将 $jnCommand
变量中的 lab
替换为 notebook
。