VS Code启动命令行参数以更改扩展参数

问题描述 投票:1回答:2

从命令行启动VS Code时,如何指定VS Code扩展参数?

具体来说,我想将一个Jupyter Server的URI输入到MS Python扩展中。

我可以在VS Code启动后选择ctrl + shift + P并选择Python: Specify Jupyter server URI,然后选择Type in the URI for the Jupyter Server,最后输入到Jupyter Server的URI。这里描述:https://code.visualstudio.com/docs/python/jupyter-support#_connect-to-a-remote-jupyter-server

我已经有一个Powershell脚本,它登录并启动远程计算机上的Jupyter Server,使用身份验证令牌捕获URI,并使用远程Jupyter Server自动启动Jupyter笔记本或Jupyter实验室的本地实例。

我还想选择使用远程Jupyter Server启动VS Code。请注意,每次在远程计算机上启动Jupyter Server URI时,它都会更改。

启动VS Code并动态更改扩展参数的命令行参数是什么?

我在这里找不到任何东西:https://vscode.readthedocs.io/en/latest/editor/command-line/#additional-command-line-arguments

python powershell visual-studio-code jupyter-notebook vscode-settings
2个回答
1
投票

我不确定通过命令行参数是否可行。但是,由于这似乎是一个设置,您可以在启动VSCode之前相应地修改工作区的settings.json

{
    "python.dataScience.jupyterServerURI": <uri>
}

1
投票

@ Gama11的建议是有效的,所以他们的回答是公认的答案。

我更改了已保存的代码工作区中的设置,而不是全局代码设置。我还添加了一些逻辑来在调用此脚本的文件夹中启动新的代码工作区。这样每个Workspace都可以拥有自己独立的Jupyter笔记本服务器。

为了完整起见,下面是我的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之后的脚本的最后部分仅适用于:

  1. 安装了VS Code,
  2. 安装并启用了'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

© www.soinside.com 2019 - 2024. All rights reserved.