Jupyter notebooks in VSCode with PowerShell support

Saw a video about Jupyter in Visual Studio Code a while back, tried to get it up and running, and… It seems that things are changed now. It was there in VSCode preview versions and the PowerShell preview extension. All the How-To’s I found showed me that way of getting it installed/configured. So I played around with Jupyter and the PowerShell kernel installation and got it working in VSCode. In this blog post, I will show you how to install and configure everything 🙂

What is Jupyter?

“The Jupyter Notebook is an open-source web application that you can use to create and share documents that contain live code, equations, visualizations, and text. The people maintain Jupyter Notebook at Project Jupyter.

Jupyter Notebooks are a spin-off project from the IPython project, which used to have an IPython Notebook project. Jupyter comes from the core supported programming languages it supports: Julia, Python, and R. Jupyter ships with the IPython kernel, allowing you to write your programs in Python. Still, there are currently over 100 other kernels that you can also use.”

Source: https://realpython.com/jupyter-notebook-introduction/

Preparation

You will need to install and configure a few things on your machine. I started a Windows Sandbox VM without any of the components to get a good list of things that you will need:

Visual Studio Code

Of course, you need to install Visual Studio Code. You can download and install it from here. Download Visual Studio Code – Mac, Linux, Windows (Supported for Windows, Linux, and Mac in multiple versions). After installing it, you can start it and go through the initial setup screen and choose Mark Done to get started.

PowerShell 7

I tested this with PowerShell 7 if you haven’t installed it already on your machine… Follow these steps:

Jupyter Notebook support in Visual Studio Code

When you’re in VSCode and have gone through the first setup, you can now follow these steps to install the Jupyter Notebook support in VSCode:

  • Press CTRL-Shift-P or click View and Command Palette from the menu bar.
  • Type Notebook and select New Jupyter Notebook from the list.
  • A pop-up will appear on the bottom right, asking you to install the recommended extensions for Python. Select Install to install these.
  • Select Allow Access if prompted by the Windows Defender Firewall.
  • Choose Select a Python Interpreter on the left screen of VSCode
  • Click on Select Python Interpreter which will open the Command Palette.
  • Select the version you want to use. f you don’t have Python installed, download and install it from here https://www.python.org/getit/. Select Download Python 3.10.6 and follow the installation steps (Choose Linux, Mac OS or Other if needed). Select Add Python to PATH on the first page of the setup and close the installer when done.
  • Close all tabs insideVSCode and close VSCode itself.

Installing Jupyter with PowerShell kernel

To add the PowerShell support and install Jupyter, you must first clone (or download as ZIP) the files from https://github.com/vors/jupyter-powershell. After cloning or extracting the downloaded ZIP, open a command prompt as Administrator and change the directory to the location where the files are. (In my case c:\Jupyter) Follow these steps to install Jupyter and the PowerShell kernel:

  • pip install jupyter (Ignore the possible pip update notice)
  • pip install powershell_kernel
  • python -m powershell_kernel.install –powershell-command pwsh

Using Jupyter Notebooks in Visual Studio Code

Now that the preparations are done, you can start Visual Studio Code and open a folder in which you want to save the Jupyter Notebooks, select Trust the authors of all files in the parent folder… and select Yes, I trust the authors. Press CTRL-Shift-P or click View and Command Palette from the menu bar, type Notebook, and select Create: New Jupyter Notebook. You will now see an editor window for an Untitled-1.pynb file:

Default the language in the right bottom will be Python, click on Python and select PowerShell from the drop-down menu. If you don’t have PowerShell installed in Visual Studio Code, it will show a prompt with the question, “Do you want to install the recommended extensions for PowerShell? Select Install to add the extension to your Visual Studio Code. Close the Extension tab to get back to your file.

In the screenshot below, I first removed the PowerShell code block by clicking on the trashcan/delete button, and I added some text and a PowerShell snippet using the buttons in the button bar on the top) The text is in MarkDown language, which makes it great to write in, my blogs are also in MarkDown format 🙂

To run the PowerShell snippet, I pressed Play on the left and chose PowerShell as the kernel. You will have to do a few steps more now if VSCode detects that .NET parts are missing:

  • A pop-up will appear asking to install the .Net Interactive Notebooks Preview extension. Select Yes to install it. (You have to press Install on the next screen again too)
  • Afterward, VSCode will show an error message if the .NET SDK version 6 is not installed on your system. If needed, you can install it using this link https://dotnet.microsoft.com/en-us/download. (I chose the x64, there are other versions for different types of Operating Systems)
  • Close and restart VSCode to complete the installation of the .NET components.

You’re still working on an unsaved document now. Press CTRL-S to save it, and name it somename.ipynb (Change Save as type to All Files) After saving, you can press Run all to see the output/format in MarkDown and the output of the Code part:

Nice, but… What can I use it for?

You can use it for documentation using the MarkDown language support and put code snippets or complete scripts to run it from within the Jupyter Notebook. This way, you can read and start procedures and tests without switching. For example:

The helpdesk user can see what he/she should do and press Play on every cell (PowerShell snippet) to follow a procedure. The output is saved in the Notebook when running the scripts, making it easier to show someone if things went okay and what the outcome was. In this example, the first two steps are successful:

But it’s also great as documentation for yourself. Saving and running procedures from it are easy to do. A possible downside could be that you have scripts in a Jupyter notebook that are not the same version as in your GitHub repository.

It’s used mainly by data-scientists. There’s an excellent article about that here: https://towardsdatascience.com/why-data-scientists-should-use-jupyter-notebooks-with-moderation-808900a69eff.

4 thoughts on “Jupyter notebooks in VSCode with PowerShell support

    • Yes, just install PowerShell 7 on it. (They can be installed next to each other) Did the same thing for this blogpost, download link is in the article. I specified pwsh as shell and that’s PowerShell 7 🙂

      But you can probably also use 5.1, replace pwsh for powershell.

  1. Pingback: install – private

  2. Pingback: Security Engineering in Azure: Azure Key Vault Overview - ProData Engineering

Leave a Reply to Harm VeenstraCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.