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 together with the PowerShell preview extension. All the How-To’s I found showed me that way of getting it installed/configured. So I played around a little bit with Jupyter and the PowerShell kernel installation and got it working in VSCode, in this blogpost 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. Jupyter Notebook is maintained by the people at Project Jupyter.
Jupyter Notebooks are a spin-off project from the IPython project, which used to have an IPython Notebook project itself. The name, Jupyter, comes from the core supported programming languages that it supports: Julia, Python, and R. Jupyter ships with the IPython kernel, which allows you to write your programs in Python, but there are currently over 100 other kernels that you can also use.”
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.
I tested this with PowerShell 7, if you haven’t installed it already on your machine… Follow these steps:
- Go to https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.2
- Select the download you want, the link above is for Windows but other Operating Systems are listed on the left
- Download and install PowerShell 7, the MSI is the easiest installation method for Windows
Jupyter Notebook support in Visual Studio Code
When you’re in VSCode and went 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 Interpretor on the left screen of VSCode
- Click on Select Python Interpretor 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 go through the installation steps (Choose Linux , Mac OS or Other if needed). Select Add Python to PATH in 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
For adding the PowerShell support and installing 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 directory to the location in which 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 windows 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 choose PowerShell as 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 un-saved 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 in it 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, which makes it easier to show someone if things went ok or not 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 anymore as in your GitHub repository.
It’s mostly used by data-scientist, nice article about that here: https://towardsdatascience.com/why-data-scientists-should-use-jupyter-notebooks-with-moderation-808900a69eff