Jupyter Notebooks in VS Code
Jupyter (formerly IPython Notebook) is an open-source project that lets you easily combine Markdown text and executable Python source code on one canvas called a notebook. Visual Studio Code supports working with Jupyter Notebooks natively, and through Python code files. This topic covers the native support available for Jupyter Notebooks and demonstrates how to:
- Create, open, and save Jupyter Notebooks
- Work with Jupyter code cells
- View, inspect, and filter variables using the Variable Explorer and Data Viewer
- Connect to a remote Jupyter server
- Debug a Jupyter Notebook
Setting up your environment
To work with Python in Jupyter Notebooks, you must activate an Anaconda environment in VS Code, or another Python environment in which you've installed the Jupyter package. To select an environment, use the Python: Select Interpreter command from the Command Palette (Ctrl+Shift+P
).
Once the appropriate environment is activated, you can create and open a Jupyter Notebook, connect to a remote Jupyter server for running code cells, and export a Jupyter Notebook as a Python file.
Workspace Trust
When getting started with Jupyter Notebooks, you'll want to make sure that you are working in a trusted workspace. Harmful code can be embedded in notebooks and the Workspace Trust feature allows you to indicate which folders and their contents should allow or restrict automatic code execution.
If you attempt to open a notebook when VS Code is in an untrusted workspace running Restricted Mode, you will not be able to execute cells and rich outputs will be hidden.
Create or open a Jupyter Notebook
You can create a Jupyter Notebook by running the Create: New Jupyter Notebook command from the Command Palette (Ctrl+Shift+P
) or by creating a new .ipynb
file in your workspace.
Next, select a kernel using the kernel picker in the top right.
After selecting a kernel, the language picker located in the bottom right of each code cell will automatically update to the language supported by the kernel.
If you have an existing Jupyter Notebook, you can open it by right-clicking on the file and opening with VS Code, or through the VS Code File Explorer.
Running cells
Run a single code cell
Once your code is added, you can run a cell using the Run icon to the left of the cell and the output will be displayed below the code cell.
To run a selected code cell, you can also use keyboard shortcuts in both command and edit mode. Ctrl+Enter
runs the currently selected cell. Shift+Enter
runs the currently selected cell and inserts a new cell immediately below (focus moves to new cell). Alt+Enter
runs the currently selected cell and inserts a new cell immediately below (focus remains on current cell).
Run multiple code cells
Running multiple code cells can be accomplished in many ways. You can use the double arrow in the main toolbar of the Notebook Editor to run all cells within the Notebook or by selecting Run All, Run All Above, or Run All Below above or below the current code cell.
Run cells in section
To more easily run related cells in a notebook, you can run cells that are grouped together by a markdown section header with the Run Cells in Section action. This action is available on the notebook Outline view and for Sticky Scroll elements.
Within Sticky Scroll elements, right-click the header of your choice, and run the section via the action in the context menu. Within the Outline view, select the toolbar icon that appears on hover or selection, and then run a single cell or a section of cells via the presented actions.
Save your Jupyter Notebook
You can save your Jupyter Notebook using the keyboard shortcut Ctrl+S
or File > Save.
Export your Jupyter Notebook
You can export a Jupyter Notebook as a Python file (.py
), a PDF, or an HTML file. To export, select ... > Export on the main toolbar. You're then presented with a dropdown of file format options.
Note: For PDF export, you must have TeX installed. If you don't, you will be notified that you need to install it when you select the PDF option. Also, be aware that if you have SVG-only output in your Notebook, they will not be displayed in the PDF. To have SVG graphics in a PDF, either ensure that your output includes a non-SVG image format or else you can first export to HTML and then save as PDF using your browser.
Work with code cells in the Notebook Editor
The Notebook Editor makes it easy to create, edit, and run code cells within your Jupyter Notebook.
Create a code cell
By default, a blank notebook will have an empty code cell for you to start with and an existing notebook will place one at the bottom. Add your code to the empty code cell to get started.
msg = "Hello world"
print(msg)
Code cell modes
While working with code cells, a cell can be in three states: unselected, command mode, and edit mode. A vertical bar to the left of a code cell and editor border shows the current state of a cell. When no bar is visible, the cell is unselected. When a cell is selected, it can be in command mode or in edit mode.
In command mode, a solid vertical bar will appear to the left of the cell. The cell can be operated on and accepts keyboard commands.
In edit mode, a solid vertical bar is joined by a border around the cell editor. The cell's contents (code or Markdown) can be modified.
To switch modes, you can use your keyboard or mouse. On your keyboard, press the Enter
key to move to edit mode or the Esc
key to move to command mode. With your mouse, click the vertical bar to the left of the cell or out of the code/Markdown region in the code cell.
Add additional code cells
You can add code cells using the main toolbar, a cell's add cell toolbar (visible with hover), and through keyboard commands.
To add a new cell below the currently selected cell, use the plus icon in the main toolbar or a cell's hover toolbar.
When a code cell is in command mode, use the A
key to add a cell above and the B
key to add a cell below the selected cell.
Select a code cell
You can change a selected code cell using the mouse or the up/down arrow keys on the keyboard. When a code cell is in command mode, you can also use the J
key (down) and K
key (up).
Select multiple code cells
To select multiple cells, start with one cell in selected mode. A filled background indicates selected cells. To select consecutive cells, hold down the Shift
key and click the last cell you want to select. To select any group of cells, hold down the Ctrl
key and click the cells you'd like to add to your selection.
Move a code cell
You can move cells up or down within a notebook via dragging and dropping. For code cells, the drag and drop area is to the left of the cell editor as indicated below. For rendered Markdown cells, you may click anywhere to drag and drop cells.
To move multiple cells, you can use the same drag and drop areas in any cell included in the selection.
The keyboard shortcut Alt+Arrow
also moves one or multiple selected cells.
Delete a code cell
To delete code, you can use the Delete icon in the code cell toolbar. When the selected code cell is in command mode, you can use the keyboard shortcut dd
.
Undo your last change
You can use the z
key to undo your previous change, for example, if you've made an accidental edit, you can undo it to the previous correct state, or if you've deleted a cell accidentally, you can recover it.
Switch between code and Markdown
The Notebook Editor allows you to easily change code cells between Markdown and code. Selecting the language picker in the bottom right of a cell will allow you to switch between Markdown and, if applicable, any other language supported by the selected kernel.
You can also use the keyboard to change the cell type. When a cell is selected and in command mode, the M
key switches the cell type to Markdown and the Y
key switches the cell type to code.
Once Markdown is set, you can enter Markdown formatted content to the code cell.
To render Markdown cells, you can select the check mark in the cell toolbar, or use the keyboard shortcuts Ctrl+Enter
and Shift+Enter
.
Clear output or restart/interrupt the kernel
If you'd like to clear all code cell outputs or restart/interrupt the kernel, you can accomplish that using the main Notebook Editor toolbar.