Working With Jupyter Notebooks In Visual Studio Code

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 (through the Jupyter extension), 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 (⇧⌘P (Windows, Linux 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.

Environment variables

Environment variables are loaded from a .env file. See that section of the Python environments documentation.

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 (⇧⌘P (Windows, Linux Ctrl+Shift+P)) or by creating a new .ipynb file in your workspace.

Blank Jupyter Notebook

Next, select a kernel using the kernel picker in the top right.

Kernel Picker

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.

Language Picker

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.

Run Jupyter 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 multiple code cells

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.

Convert Jupyter Notebook to Python file

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.

Tag » How To Open Jupyter Notebook