Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Using Jupyter Notebook in VS Code

Using Jupyter Notebook in VS Code

VS Code (full name Visual Studio Code) is a free, open-source, cross-platform code editor released by Microsoft.

Install an extension to use Jupyter Notebook in VS Code; VS Code's powerful code completion combines perfectly with Jupyter's interactive experience.

If you have not installed VS Code yet, go to itsOfficial Websiteto download and install it; the installation is very simple, just keep clicking "Next".

If you are not familiar with VS Code, see our VSCode Tutorial.

Open VS Code, click the Extensions icon in the activity bar on the left (shortcut Ctrl+Shift+X), search for and install the "Jupyter" extension (provided by Microsoft); installing it also installs the Python extension automatically.

Core features and operation guide

Create your first Jupyter Notebook

Now let us create a brand-new Jupyter Notebook file (.ipynb).

**New file:** simply create a file whose name ends in .ipynb.

You can also create it from the Command Palette:

  1. In VS Code, press Ctrl+Shift+P OpenCommand Palette(this is a universal search box).
  2. Input Jupyter: Create New Jupyter Notebook and select it.
  3. VS Code automatically creates a file named Untitled-1.ipynb and opens it in the notebook interface. You will see the first cell is ready.

Configure the runtime environment (Kernel)

Before running code, you need to tell VS Code which Python interpreter to use:

Then you can start writing and running code:


Running and managing cells

Run a code cell

Cell modes

Cells in VS Code have three states, marked by thevertical baron the left:

  1. Not selected: no vertical bar.
  2. Command Mode: the vertical bar is solid, and keyboard commands (such as delete and copy) are available.
  1. Edit Mode: the vertical bar is solid and the cell has a border, so you can type code.

Format switching


File operations and navigation


Data science enhancement tools

Variable explorer and data viewer

Click the Variables icon on the toolbar to open the variables panel at the bottom.

Saving charts

Hover over a generated chart (for example one produced by matplotlib) and click the Save icon in the upper right to save it as an image.


Debugging and advanced features

Debugging

Remote connection

If you need the computing power of a remote server:

  1. click the Kernel Picker(kernel selector) in the upper right.

  2. Choice Existing Jupyter Server.

  3. Enter a server URL containing ?token= to connect.

Diff comparison

Because .ipynb is essentially JSON, and VS Code provides avisual comparison tool. You can clearly see the exact changes to inputs, outputs or metadata without reading messy raw code.