Connecting VSCode to a Jupyter Notebook

Connecting VSCode to a Jupyter Notebook

I have been working a lot in Jupyter notebooks; however, as a coding environment, it lacks many of the bells and whistles that you get with an IDE.

With the latest version of the VSCode Python plugin, you work directly with the Jupyter Notebook from within the IDE. The extension provides Intellisene support and the ability to run the Python code within the page.

For example, here is the previous code I used to demonstrate RDKit running in my Jupyter environment running within VSCode.

To connect you VSCode environment to Jupyter, first install VSCode, from the following URL:

https://code.visualstudio.com/

Once installed, open the VSCode Extensions view and search for the Python extension (I am using the latest version, 2020.4.74986). The Python extension adds various commands to the pallet, which will allow us to connect to the Jupyter server.

Ensure that the Jupyter server has started, I am reusing the Jupyter server I created for the previous example, so I start the Jupyter Notebook my-rdkit-env from my start menu shortcut.

Starting the Jupyter server will open a command prompt you will need to copy the URL for the server (including the token) as we need to paste this URL into VSCode to connect to the running server. 

Copy highlighter url from Jupyter startup

If the URL does not include the token or the token has expired, you will get a 403 error when you try and connect to the Jupyter server.

Copy the URL from the command prompt.

In the VSCode editor View > Command Pallet and type the following command:

Python: Specify Jupyter server URI

Command Pallet option to specify Jupyter server url.

Paste the command into the resulting dialog box.

Paste the Jupyter server url into VS Code.

VSCode connects to the Jupyter environment, if the connection fails, check that have you copied and pasted the entire token.

At this point, you can create a new Notebook, or as I have an existing notebook that I want to reuse, I use the “File > Open File” menu to navigate to and open the .ipynb file for the NotebookNotebook I wish to open.

Open existing Jupyter Notebook in VS Code.

With the Notebook open and the connection to the Jupyter environment, I can run my RDKit example code and view the output directly within VSCode.

Run the code Jupyter Notebook in VSCode.

Leave a Reply

Your email address will not be published.