VS Code launch command line arguments to change extension parameters












1















How can I specify a VS Code extension parameter when starting VS Code from the command line?



Specifically, I want to input a URI of a Jupyter Server into the MS Python extension.



I can do this after VS Code starts by selecting ctrl+shift+P and selecting Python: Specify Jupyter server URI, then selecting Type in the URI for the Jupyter Server, and finally entering the URI to the Jupyter Server. Described here: https://code.visualstudio.com/docs/python/jupyter-support#_connect-to-a-remote-jupyter-server



I already have a Powershell script that logs in and starts the Jupyter Server on the remote machine, captures the URI with the authentication token, and automatically launches a local instance of either a Jupyter notebook, or Jupyter labs using the remote Jupyter Server.



I would like to also have the option to launch VS Code using the remote Jupyter Server. Note that the Jupyter Server URI changes each time you start it on the remote machine.



What are the command line arguments to start VS Code and dynamically change extension parameters?



I did not find anything here:
https://vscode.readthedocs.io/en/latest/editor/command-line/#additional-command-line-arguments










share|improve this question





























    1















    How can I specify a VS Code extension parameter when starting VS Code from the command line?



    Specifically, I want to input a URI of a Jupyter Server into the MS Python extension.



    I can do this after VS Code starts by selecting ctrl+shift+P and selecting Python: Specify Jupyter server URI, then selecting Type in the URI for the Jupyter Server, and finally entering the URI to the Jupyter Server. Described here: https://code.visualstudio.com/docs/python/jupyter-support#_connect-to-a-remote-jupyter-server



    I already have a Powershell script that logs in and starts the Jupyter Server on the remote machine, captures the URI with the authentication token, and automatically launches a local instance of either a Jupyter notebook, or Jupyter labs using the remote Jupyter Server.



    I would like to also have the option to launch VS Code using the remote Jupyter Server. Note that the Jupyter Server URI changes each time you start it on the remote machine.



    What are the command line arguments to start VS Code and dynamically change extension parameters?



    I did not find anything here:
    https://vscode.readthedocs.io/en/latest/editor/command-line/#additional-command-line-arguments










    share|improve this question



























      1












      1








      1


      1






      How can I specify a VS Code extension parameter when starting VS Code from the command line?



      Specifically, I want to input a URI of a Jupyter Server into the MS Python extension.



      I can do this after VS Code starts by selecting ctrl+shift+P and selecting Python: Specify Jupyter server URI, then selecting Type in the URI for the Jupyter Server, and finally entering the URI to the Jupyter Server. Described here: https://code.visualstudio.com/docs/python/jupyter-support#_connect-to-a-remote-jupyter-server



      I already have a Powershell script that logs in and starts the Jupyter Server on the remote machine, captures the URI with the authentication token, and automatically launches a local instance of either a Jupyter notebook, or Jupyter labs using the remote Jupyter Server.



      I would like to also have the option to launch VS Code using the remote Jupyter Server. Note that the Jupyter Server URI changes each time you start it on the remote machine.



      What are the command line arguments to start VS Code and dynamically change extension parameters?



      I did not find anything here:
      https://vscode.readthedocs.io/en/latest/editor/command-line/#additional-command-line-arguments










      share|improve this question
















      How can I specify a VS Code extension parameter when starting VS Code from the command line?



      Specifically, I want to input a URI of a Jupyter Server into the MS Python extension.



      I can do this after VS Code starts by selecting ctrl+shift+P and selecting Python: Specify Jupyter server URI, then selecting Type in the URI for the Jupyter Server, and finally entering the URI to the Jupyter Server. Described here: https://code.visualstudio.com/docs/python/jupyter-support#_connect-to-a-remote-jupyter-server



      I already have a Powershell script that logs in and starts the Jupyter Server on the remote machine, captures the URI with the authentication token, and automatically launches a local instance of either a Jupyter notebook, or Jupyter labs using the remote Jupyter Server.



      I would like to also have the option to launch VS Code using the remote Jupyter Server. Note that the Jupyter Server URI changes each time you start it on the remote machine.



      What are the command line arguments to start VS Code and dynamically change extension parameters?



      I did not find anything here:
      https://vscode.readthedocs.io/en/latest/editor/command-line/#additional-command-line-arguments







      python powershell visual-studio-code jupyter-notebook vscode-settings






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 20 at 14:01







      Clay

















      asked Jan 15 at 21:32









      ClayClay

      602719




      602719
























          2 Answers
          2






          active

          oldest

          votes


















          1














          I'm not sure that's possible via command line arguments. However, since this appears to be a setting, you could modify the workspace's settings.json accordingly before starting VSCode:



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





          share|improve this answer































            1














            It was @Gama11's suggestion that worked, so their answer is the accepted answer.



            I changed the setting in the saved Code Workspaces instead of the global Code settings. I also added some logic to start a new Code Workspace in the folder this script is called from. This way each Workspace can have its own separate Jupyter notebook server.



            For completeness, below is my Powershell script.



            ## 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:UsersUserNameMiniconda3";
            $env:Path += ";C:UsersUserNameMiniconda3";
            $env:Path += ";C:UsersUserNameMiniconda3Librarymingw-w64bin";
            $env:Path += ";C:UsersUserNameMiniconda3Libraryusrbin";
            $env:Path += ";C:UsersUserNameMiniconda3Librarybin";
            $env:Path += ";C:UsersUserNameMiniconda3Scripts";
            $env:Path += ";C:nltk_data";
            $env:Path += ";C:UsersUserNamescripts";
            C:UsersUserNamescriptsAdditionalSettingsFile.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:UsersUserNameAppDataRoamingCodeUserworkspaceStorage"

            $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.vscodesettings.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
            }


            The final part of the script after $WorkSpacesPath will only work if:




            1. VS Code is installed,

            2. the 'ms-python.python' extension installed and enabled,


            Obviously, you will need to change the additions to the remote's $PATH to point to your installation of python and location of other files you want to run on the remote machine.



            Note that the select-string "URLs:" and .Contains("URLs:") work for the most recent (relative to this posting) version of Jupyter. Previously, the string was "token:" but the Jupyter team changed the startup output. Nothing to stop them changing it again and breaking the code above.



            This can easily be changed to start Jupyter labs instead of VS Code. To do that simply replace the lines after $CodeSettingsPath to the following (assuming you have Google Chrome installed).



            Start-Process chrome.exe --app=$jurl


            If you wanted to start a Jupyter notebook instead, you will need to replace lab with notebook in the $jnCommand variable.






            share|improve this answer

























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54207144%2fvs-code-launch-command-line-arguments-to-change-extension-parameters%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              1














              I'm not sure that's possible via command line arguments. However, since this appears to be a setting, you could modify the workspace's settings.json accordingly before starting VSCode:



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





              share|improve this answer




























                1














                I'm not sure that's possible via command line arguments. However, since this appears to be a setting, you could modify the workspace's settings.json accordingly before starting VSCode:



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





                share|improve this answer


























                  1












                  1








                  1







                  I'm not sure that's possible via command line arguments. However, since this appears to be a setting, you could modify the workspace's settings.json accordingly before starting VSCode:



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





                  share|improve this answer













                  I'm not sure that's possible via command line arguments. However, since this appears to be a setting, you could modify the workspace's settings.json accordingly before starting VSCode:



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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 16 at 16:06









                  Gama11Gama11

                  11.5k32348




                  11.5k32348

























                      1














                      It was @Gama11's suggestion that worked, so their answer is the accepted answer.



                      I changed the setting in the saved Code Workspaces instead of the global Code settings. I also added some logic to start a new Code Workspace in the folder this script is called from. This way each Workspace can have its own separate Jupyter notebook server.



                      For completeness, below is my Powershell script.



                      ## 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:UsersUserNameMiniconda3";
                      $env:Path += ";C:UsersUserNameMiniconda3";
                      $env:Path += ";C:UsersUserNameMiniconda3Librarymingw-w64bin";
                      $env:Path += ";C:UsersUserNameMiniconda3Libraryusrbin";
                      $env:Path += ";C:UsersUserNameMiniconda3Librarybin";
                      $env:Path += ";C:UsersUserNameMiniconda3Scripts";
                      $env:Path += ";C:nltk_data";
                      $env:Path += ";C:UsersUserNamescripts";
                      C:UsersUserNamescriptsAdditionalSettingsFile.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:UsersUserNameAppDataRoamingCodeUserworkspaceStorage"

                      $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.vscodesettings.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
                      }


                      The final part of the script after $WorkSpacesPath will only work if:




                      1. VS Code is installed,

                      2. the 'ms-python.python' extension installed and enabled,


                      Obviously, you will need to change the additions to the remote's $PATH to point to your installation of python and location of other files you want to run on the remote machine.



                      Note that the select-string "URLs:" and .Contains("URLs:") work for the most recent (relative to this posting) version of Jupyter. Previously, the string was "token:" but the Jupyter team changed the startup output. Nothing to stop them changing it again and breaking the code above.



                      This can easily be changed to start Jupyter labs instead of VS Code. To do that simply replace the lines after $CodeSettingsPath to the following (assuming you have Google Chrome installed).



                      Start-Process chrome.exe --app=$jurl


                      If you wanted to start a Jupyter notebook instead, you will need to replace lab with notebook in the $jnCommand variable.






                      share|improve this answer






























                        1














                        It was @Gama11's suggestion that worked, so their answer is the accepted answer.



                        I changed the setting in the saved Code Workspaces instead of the global Code settings. I also added some logic to start a new Code Workspace in the folder this script is called from. This way each Workspace can have its own separate Jupyter notebook server.



                        For completeness, below is my Powershell script.



                        ## 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:UsersUserNameMiniconda3";
                        $env:Path += ";C:UsersUserNameMiniconda3";
                        $env:Path += ";C:UsersUserNameMiniconda3Librarymingw-w64bin";
                        $env:Path += ";C:UsersUserNameMiniconda3Libraryusrbin";
                        $env:Path += ";C:UsersUserNameMiniconda3Librarybin";
                        $env:Path += ";C:UsersUserNameMiniconda3Scripts";
                        $env:Path += ";C:nltk_data";
                        $env:Path += ";C:UsersUserNamescripts";
                        C:UsersUserNamescriptsAdditionalSettingsFile.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:UsersUserNameAppDataRoamingCodeUserworkspaceStorage"

                        $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.vscodesettings.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
                        }


                        The final part of the script after $WorkSpacesPath will only work if:




                        1. VS Code is installed,

                        2. the 'ms-python.python' extension installed and enabled,


                        Obviously, you will need to change the additions to the remote's $PATH to point to your installation of python and location of other files you want to run on the remote machine.



                        Note that the select-string "URLs:" and .Contains("URLs:") work for the most recent (relative to this posting) version of Jupyter. Previously, the string was "token:" but the Jupyter team changed the startup output. Nothing to stop them changing it again and breaking the code above.



                        This can easily be changed to start Jupyter labs instead of VS Code. To do that simply replace the lines after $CodeSettingsPath to the following (assuming you have Google Chrome installed).



                        Start-Process chrome.exe --app=$jurl


                        If you wanted to start a Jupyter notebook instead, you will need to replace lab with notebook in the $jnCommand variable.






                        share|improve this answer




























                          1












                          1








                          1







                          It was @Gama11's suggestion that worked, so their answer is the accepted answer.



                          I changed the setting in the saved Code Workspaces instead of the global Code settings. I also added some logic to start a new Code Workspace in the folder this script is called from. This way each Workspace can have its own separate Jupyter notebook server.



                          For completeness, below is my Powershell script.



                          ## 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:UsersUserNameMiniconda3";
                          $env:Path += ";C:UsersUserNameMiniconda3";
                          $env:Path += ";C:UsersUserNameMiniconda3Librarymingw-w64bin";
                          $env:Path += ";C:UsersUserNameMiniconda3Libraryusrbin";
                          $env:Path += ";C:UsersUserNameMiniconda3Librarybin";
                          $env:Path += ";C:UsersUserNameMiniconda3Scripts";
                          $env:Path += ";C:nltk_data";
                          $env:Path += ";C:UsersUserNamescripts";
                          C:UsersUserNamescriptsAdditionalSettingsFile.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:UsersUserNameAppDataRoamingCodeUserworkspaceStorage"

                          $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.vscodesettings.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
                          }


                          The final part of the script after $WorkSpacesPath will only work if:




                          1. VS Code is installed,

                          2. the 'ms-python.python' extension installed and enabled,


                          Obviously, you will need to change the additions to the remote's $PATH to point to your installation of python and location of other files you want to run on the remote machine.



                          Note that the select-string "URLs:" and .Contains("URLs:") work for the most recent (relative to this posting) version of Jupyter. Previously, the string was "token:" but the Jupyter team changed the startup output. Nothing to stop them changing it again and breaking the code above.



                          This can easily be changed to start Jupyter labs instead of VS Code. To do that simply replace the lines after $CodeSettingsPath to the following (assuming you have Google Chrome installed).



                          Start-Process chrome.exe --app=$jurl


                          If you wanted to start a Jupyter notebook instead, you will need to replace lab with notebook in the $jnCommand variable.






                          share|improve this answer















                          It was @Gama11's suggestion that worked, so their answer is the accepted answer.



                          I changed the setting in the saved Code Workspaces instead of the global Code settings. I also added some logic to start a new Code Workspace in the folder this script is called from. This way each Workspace can have its own separate Jupyter notebook server.



                          For completeness, below is my Powershell script.



                          ## 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:UsersUserNameMiniconda3";
                          $env:Path += ";C:UsersUserNameMiniconda3";
                          $env:Path += ";C:UsersUserNameMiniconda3Librarymingw-w64bin";
                          $env:Path += ";C:UsersUserNameMiniconda3Libraryusrbin";
                          $env:Path += ";C:UsersUserNameMiniconda3Librarybin";
                          $env:Path += ";C:UsersUserNameMiniconda3Scripts";
                          $env:Path += ";C:nltk_data";
                          $env:Path += ";C:UsersUserNamescripts";
                          C:UsersUserNamescriptsAdditionalSettingsFile.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:UsersUserNameAppDataRoamingCodeUserworkspaceStorage"

                          $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.vscodesettings.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
                          }


                          The final part of the script after $WorkSpacesPath will only work if:




                          1. VS Code is installed,

                          2. the 'ms-python.python' extension installed and enabled,


                          Obviously, you will need to change the additions to the remote's $PATH to point to your installation of python and location of other files you want to run on the remote machine.



                          Note that the select-string "URLs:" and .Contains("URLs:") work for the most recent (relative to this posting) version of Jupyter. Previously, the string was "token:" but the Jupyter team changed the startup output. Nothing to stop them changing it again and breaking the code above.



                          This can easily be changed to start Jupyter labs instead of VS Code. To do that simply replace the lines after $CodeSettingsPath to the following (assuming you have Google Chrome installed).



                          Start-Process chrome.exe --app=$jurl


                          If you wanted to start a Jupyter notebook instead, you will need to replace lab with notebook in the $jnCommand variable.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 24 at 13:30

























                          answered Jan 16 at 17:20









                          ClayClay

                          602719




                          602719






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54207144%2fvs-code-launch-command-line-arguments-to-change-extension-parameters%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Liquibase includeAll doesn't find base path

                              How to use setInterval in EJS file?

                              Petrus Granier-Deferre