YouTip LogoYouTip

Jupyter Notebook Vscode

Using Jupyter Notebook in VS Code

VS Code (short for Visual Studio Code) is a free, open-source, cross-platform code editor developed by Microsoft.

You can install extensions in VS Code to use Jupyter Notebook. VS Code's powerful code completion features combined with Jupyter's interactive experience create a perfect match.

If you haven't installed VS Code yet, please visit its official website to download and install it. The installation process is very simpleβ€”just keep clicking "Next".

If you're not familiar with VS Code, you can refer to our VSCode Tutorial.

Open VS Code, click the Extensions icon in the left activity bar (shortcut Ctrl+Shift+X), search for and install the "Jupyter" extension (provided by Microsoft). Installing this extension will automatically install the Python extension.

Image 1

Core Features and Operation Guide

Create Your First Jupyter Notebook

Now, let's create a brand new Jupyter Notebook file (.ipynb).

New File: Simply create a new file with the .ipynb extension.

Image 2

You can also create it through the command palette:

  1. In VS Code, press Ctrl+Shift+P to open the Command Palette (this is a universal search box).
  2. Type Jupyter: Create New Jupyter Notebook and select it.
  3. VS Code will automatically create a new file named Untitled-1.ipynb and open it in the notebook interface. You'll see the first cell is already ready.

Image 3

Configure Runtime Environment (Kernel)

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

  • Click "Select Kernel" in the top-right corner of the editing interface. Image 4
  • In the popup list, select your installed Python version or Anaconda environment (e.g., Python 3.x.x or base (conda)). Image 5
  • Note: If this is your first time running, VS Code may prompt you to install the ipykernel packageβ€”just click "Install".

Then, you can start writing and running code:

Image 6


Cell Execution and Management

Running Code Cells

  • Single cell execution: Click the play icon (β–Ά) on the left side of the cell.
  • Keyboard shortcuts:
    • Ctrl + Enter: Run the current cell.
    • Shift + Enter: Run the current cell and jump/select the cell below.
    • Alt + Enter: Run the current cell and insert a new cell below.

Image 7

  • Multiple cell execution: Use the "double arrow" icon Run All in the top toolbar to run all cells; or at a specific cell, select Run Above (run all above) or Run Below (run all below).
  • Section-based execution: In the "Outline" view, you can click the button next to a section title to run the entire group of cells under that Markdown heading.

Image 8

Cell Modes

Cells in VS Code have three states, identified by the vertical bar on the left:

  1. Unselected: No vertical bar.
  2. Command Mode: Solid vertical bar, where you can execute keyboard commands (like delete, copy).
    • Enter enters edit mode; Esc returns to command mode.
    • A inserts a cell above; B inserts below.
    • D, D (press twice) deletes cell; Z undoes deletion.
  3. Edit Mode: Solid vertical bar with cell border, where you can input code.

Format Switching

  • In Command Mode: Press M to switch to Markdown (documentation), press Y to switch to Code.

File Operations and Navigation

  • Save: Ctrl + S.
  • Export: Click ... > Export in the toolbar. Supports exporting to .py script, HTML, or PDF (note: PDF export requires TeX environment installation).
  • Outline navigation: Quickly jump through the Outline view in the sidebar. By default, only Markdown headings are displayed; you can enable Notebook > Outline: Show Code Cells in settings to show code blocks.
  • Line number control: In command mode, press L to toggle line numbers for a single cell, press Shift + L to toggle line numbers for the entire document.

Image 9


Data Science Enhancement Tools

Variable Explorer and Data Viewer

Click the Variables icon in the toolbar to open the variable panel at the bottom.

  • View details: Double-click a variable or click the icon next to it to enter the Data Viewer.
  • Filter data: Enter content in the text box at the top of a column to search; enter = for exact match; supports regex filtering.

Image 10

Image 11

Plot Saving

Hover over a generated chart (such as matplotlib output), click the Save icon in the top-right corner to save it as an image.

Image 12


Debugging and Advanced Features

Debugging Features

  • Run by Line: Click the icon in the cell toolbar to step through code execution without interruption. Image 13
  • Full debugging: Set a breakpoint on the left side of a cell, select Debug Cell next to the Run button. Image 14

Remote Connection

If you need to use computing power from a remote server:

  1. Click the Kernel Picker in the top-right corner. Image 15
  2. Select Existing Jupyter Server. Image 16
  3. Enter the server URL with ?token= to connect. Image 17

Diff Comparison

Since .ipynb is essentially JSON, VS Code provides a visual comparison tool. You can clearly see specific changes in inputs, outputs, or metadata without having to read messy raw code.

Image 18

← Vue3 TaskhubJupyter Notebook Chinese β†’