How to Run Jupyter Notebook in Visual Studio Code (VS Code)


Do you want to run Jupyter notebooks in Visual Studio Code? While you can run Jupyter notebooks in Colab and locally in a browser,  running your notebooks in VScode offers you many benefits including:

  1. An integrated development environment for editing, debugging and version control.
  2. Local development (unlike Google Colab) which can be beneficial for privacy, security and compliance purposes.
  3. A familiar environment. You probably already do most of your coding in VScode so running your notebook within it feels like working from home.

In this post, I'll walk you through the process of setting up and running Jupyter notebooks in VSCode. Let's go!

Prerequisites

Before we begin, ensure you have the following installed on your system:

  • Python (version 3.6 or later). Download here
  • Visual Studio Code. Download here
  • Optionally, a virtual environment for managing dependencies. I'll use Python3's built-in virtualenv tool in this tutorial.

Step 1: Create a Virtual Environment

First, we will create and activate a virtual environment to isolate our project dependencies:

For Linux users:

python3 virtualenv venv
source venv/bin/activate

If you are on windows: 

venv\Scripts\activate

Step 2: Install Jupyter Notebook

Next, we will install Jupyter Notebook using pip. Jupyter Notebook comes bundled with ipykernel, which allows us to run Jupyter kernels within VSCode.

pip install jupyter

Confirm that Jupyter Notebook is installed by running:

pip freeze

You should see Jupyter on the list

Step 3: Start the Jupyter Kernel

To start a Jupyter kernel, run the following command:

jupyter notebook

By default, the Jupyter server runs on port 8888. You'll see a message in your terminal similar to this:

[I 15:37:12.123 NotebookApp] The Jupyter Notebook is running at:
[I 15:37:12.123 NotebookApp] http://localhost:8888/?token=abcd1234

Note the token (abcd1234) in the URL. This token is used for authentication when connecting to the Jupyter server.

You can customize the behavior of the Jupyter server using flags and options. For example, to specify a custom token and prevent the browser from opening, you can use:

jupyter notebook --NotebookApp.token='your_token' --no-browser

Step 4: Connect VSCode to the Jupyter Server

Now that the Jupyter server is running, let's connect VSCode to the server. Open an existing notebook or create a new one in VSCode. When you run a cell, you'll be prompted to connect to a server. Enter the URL (http://localhost:8888) and the token you noted earlier.

If you are prompted to install some extensions such as those in the above screenshot. Please do so.

jupyter commands in vs code

Step 5: Open a Notebook

You can either create a ipynb file in the VS Code explorer panel or type Ctrl +Shift + P and select "create a new Jupyter notebook" from the dropdown.

The commands palette contains useful commands that will be hinted in the next section.


Pro-tips on Running Jupyter Notebooks in VSCode

  1. Use a custom port: You can specify a custom port using the --port flag, e.g., --port 8080.
  2. Set a custom token: You can specify a custom token using the --NotebookApp.token flag, e.g., --NotebookApp.token='test_token'.
  3. Prevent Opening Browser: Use the --no-browser flag to prevent the browser from opening when starting the server.
  4. Exporting Notebooks: VSCode allows you to export Jupyter notebooks to HTML, PDF, or Python script formats.
  5. Variable State: Explore the state and values of variables in your notebook using VSCode's debugging features.