Jupiter(formerly IPython Notebook) is an open source project that makes it easy to put Markdown text and executable Python source code in a screen calleddiary. Visual Studio Code supports working natively and end-to-end with Jupyter NotebooksPython code files. This topic covers the native support available for Jupyter Notebooks and shows how to:
- Create, open, and save Jupyter Notebooks
- Work with Jupyter code cells
- View, explore, and filter variables with the Variable Browser and Data Viewer
- Connect to a remote Jupyter server
- Debug a Jupyter notebook
Set up your environment
To work with Python in Jupyter Notebooks, you need to launch an Anaconda environment in VS Code or any other Python environment you have installedpacote jupyter. To select an environment, use thePython: Select interpretercommand palette command (⇧⌘S(Windows, LinuxCtrl+Shift+P)).
Once the appropriate environment is enabled, you can create and open a Jupyter Notebook, connect to a remote Jupyter server to run code cells, and export a Jupyter Notebook as a Python file.
confidence in the workplace
When you start using notebooks, you need to make sure you're working in a trusted workspace. Malicious code can be embedded in notebooks and theconfidence in the workplaceThe feature allows you to specify which folders and their contents should allow or restrict execution of the autorun code.
If you try to open a notebook while VS Code is in an untrusted workspace and runningrestricted mode, you cannot run cells and the rich search results are hidden.
Create or open a Jupyter Notebook
You can create a Jupyter Notebook by using theCreate: New Jupyter notebookcommand palette command (⇧⌘S(Windows, LinuxCtrl+Shift+P)) or create a new one
.ipynbfile on your desktop.
Then select a kernel using the kernel selector in the top right corner.
After selecting a kernel, the language switcher in the lower-right corner of each code cell is automatically updated to the language supported by the kernel.
If you have an existing Jupyter Notebook, you can open it by right-clicking the file and opening it with VS Code or via VS Code File Explorer.
Once you have a notebook, you can run a code cell with itRunto the left of the cell and the output appears just below the code cell.
You can also use keyboard shortcuts to run code. Use in command or edit modeCtrl+Introductionto run the current cell orShift+Enterto run the current cell and advance to the next.
You can run multiple cells by selecting themall run,follow all the above steps, ÖComplete all below.
Save your Jupyter notebook
You can save your Jupyter Notebook with a keyboard shortcutControl + SÖarchive>Save on computer.
Export to Jupyter Notebook
You can export a Jupyter Notebook as a Python file (
.py), a PDF or HTML file. To export, select theExportAction on the main toolbar. You'll see a drop-down list of file format options.
Monitoring:To export PDF you must haveTeX installed. If not, you will be notified that you need to install it when you select the PDF option. Also keep in mind that if you only have SVG output on your laptop, they will not show up in the PDF. To have SVG graphics in a PDF file, make sure your output contains an image format other than SVG, or you can also export it to HTML first and then save it as a PDF using your browser.
Work with code cells in the notebook editor
The notebook editor makes it easy to create, edit, and run code cels in your Jupyter notebook.
Create a code cell
By default, a blank notebook has an empty code cell to get you started, and an existing notebook places one at the bottom. Paste your code into the empty code cell to get started.
message ="Hello World"press(News)
Encode cell modes
When working with code cells, a cell can be in three states: unselected, command mode, and edit mode. The current state of a cell is indicated by a vertical bar to the left of a code cell and the editor border. If no bars are visible, the cell is not selected.
When a cell is selected it can be in two different modes. It can be in command mode or edit mode. When the cell is in command mode, it can be operated and accept keyboard commands. When the cell is in edit mode, the content of the cell (code or markdown) can be modified.
When a cell is in command mode, a solid vertical bar appears to the left of the cell.
In edit mode, the solid vertical bar is joined by a border around the cell editor.
To switch from edit mode to command mode, press the keyEscButton. To switch from command mode to edit mode, press the keyTapButton. You can also use your mousechange modeby clicking the vertical bar to the left of the cell or outside the code/markup area in the code cell.
Add more code cells
Code cells can be added to a notebook using the main toolbar, a cell's add cell toolbar (visible on hover), and keyboard shortcuts.
Using the plus icons on a cell's main toolbar and pop-up toolbar adds a new cell directly below the currently selected cell.
When a code cell is in command mode, theAcan be used to add a cell above and theBcan be used to add a cell below the selected cell.
Select a code cell
The selected code cell can be changed using the mouse, the up/down arrow keys on the keyboard and theJ(unten uk(high). To use the keyboard, the cell must be in command mode.
Select multiple code cells
To select multiple cells, start with a cell in selected mode. If you want to select consecutive cells, hold downChangeand click on the last cell you want to select. If you want to select any group of cells, hold downcontroland click on the cells you want to add to your selection.
Selected cells are shown with a filled background.
Run a single code cell
Once your code has been added, you can run a cell withRunto the left of the cell and the result is displayed below the code cell.
You can also use keyboard shortcuts to run a selected code cell.Ctrl+Introductionexecute the currently selected cell,Shift+Enterexecutes the currently selected cell and inserts a new cell directly below (focus moves to the new cell) andAlt+Introductionexecutes the currently selected cell and inserts a new cell directly below it (focus stays on the current cell). These keyboard shortcuts can be used in command and edit mode.
Run multiple code cells
Executing multiple code cells can be accomplished in a number of ways. You can use the double arrow on the main notebook editor toolbar to run all cells within the notebook or notebookRunDirection arrow icons on the cell toolbar to run all cells above or below the current code cell.
move a code cell
Cells can be moved up or down in a notebook using drag and drop. For code cells, the drag-and-drop area is on the left of the cell editor, as shown below. With rendered Markdown cells, you can click anywhere to drag and drop the cells.
To move multiple cells, you can use the same drag and drop areas for each cell included in the selection.
You can also use keyboard shortcutsAlt+Configureto move one or more selected cells.
Delete a code cell
A code cell can be deleted withExtinguishicon on the code cell toolbar or via a keyboard shortcutddwhen the selected code cell is in command mode.
Undo your last change
you can use the...zTo undo the previous change, you can, for example, undo an accidentally made change to the previous correct state or restore an accidentally deleted cell.
Switch between code and markdown
The notebook editor lets you easily switch code cells between markdown and code. If you select the language selector in the lower right corner of a cell, you can switch between Markdown and any other language supported by the selected kernel, if applicable.
You can also use the keyboard to change the cell type. When a cell is selected and in command mode, theMETROThe button changes the cell type to Markdown and theYThe button changes the cell type to Code.
Once Markdown is set up, you can paste the Markdown formatted content into the code cell.
To render markdown cells, you can select the check mark in the cell toolbar or use theCtrl+IntroductionmiShift+Enterkeyboard shortcuts.
Clear output or restart/stop kernel
If you want to clear all code cell output or restart/stop the kernel, you can do it from the main notebook editor toolbar.
Enable/disable line numbers
In command mode, you can enable or disable line numbering within a single code cell withUETaste.
Use to toggle line numbering for the entire notebookShift+Lwhen in command mode on any cell.
To explore your laptop, open File Explorer from the activity bar. Then open theOutlineTab in the sidebar.
Monitoring:By default, the outline only displays Markdown. To show code cells, enable the following settings:Notebook > Outline: Show Code Cells.
Compatible with IntelliSense and Jupyter Notebook Editor
The Python Jupyter Notebook editor window has full IntelliSense: code completion, item lists, method accelerators, and parameter hints. You can type just as productively in the notebook editor window as you do in the code editor.
Variable Explorer and Data Viewer
In a Python notebook, you can view, explore, sort, and filter the variables in your current Jupyter session. By selecting thevariablesIn the main toolbar, after running the code and cells, you will see a list of the current variables, which will update automatically as the variables are used in the code. The variable field opens at the bottom of the notebook.
You can also double-click a row or use the to get additional information about your variablesShow variable in data viewernext to the variable to get a more detailed view of a variable in the data display.
Row filtering in the data display can be done by typing in the text box at the top of each column. Enter a string you want to search for and any row containing that string in the column will be found:
If you want to find an exact match, precede your filter with an '=':
More complex filtering can be done by writing aregular sentence:
To save a chart to your notebook, simply hover over the output and select itSave on computericon in the upper right corner.
Monitoring:Render graphics created withmatplotlibmialtaïr.
Custom notebook difference
Under the hood, Jupyter notebooks are JSON files. Segments in a JSON file are represented as cells made up of three components: input, output, and metadata. Comparing changes made to a notebook with line-based diffs is difficult and difficult to analyze. The rich diff editor for notebooks allows you to easily see the changes of each component of a cell.
You can even customize the types of changes you want to show in your diff view. In the top-right corner, select the toolbar flyout menu item to customize which cell components you want to include. Input differences are always displayed.
To learn more about Git integration in VS Code, visitSource control in VS Code.
Debug a Jupyter notebook
There are two different ways to debug a Jupyter notebook: a simpler method called "run per line" and the full debug mode.
Monitoring:Both features require ipykernel 6+. To seethis wiki pagefor details on installing or updating ipykernel.
run per line
Run by Row allows you to run a cell row by row without being distracted by other VS Code debugging features. First select therun per lineCell toolbar button:
Use the same key to advance an instruction. You can select the cellFusesto stop early, or the buttonKeep goingon the toolbar to continue to the end of the cell.
If you want to use all the debugging features supported by VS Code, e.g. B. breakpoints and the ability to access other cells and modules, you can use the full VS Code debugger.
- First set the necessary breakpoints by clicking on the left edge of a notebook cell.
- Then select theDebug cellbutton in the menu next to theRunButton. This will run the cell in a debugging session and break your breakpoints in any code you run, even if it's in a different cell or in a different cell.
- You can use the Debug View, the Debug Console, and all the buttons on the Debug toolbar as usual in VS Code.
Search in Notepad
You can search kbstyle (Ctrl/Cmd + F) through an entire notebook, or you can search for parts of your notebook by filtering the search options. While browsing your notebook, click the filter option (funnel icon) to search for:
- cell entry discount (discount source)
- discount cell power (Markdown rendered)
- Enter the code cell (code cell source)
- Output of code cell (cell exit)
Notebook searches default to filtered cell input only.
Connect to a remote Jupyter server
You can download CPU-intensive Jupyter notebooks to other computers by connecting to a remote Jupyter server. Once connected, the code cells will run on the remote server instead of the local machine.
To connect to a remote Jupyter server:
Open the kernel selector button in the top right corner of the notebook (or run theNotebook: Select the core of the notebookcommand from the command palette).
ChooseExisting Jupyter serverOption to connect to an existing Jupyter server.
To connect to an existing server for the first time, selectEnter the URL of the running Jupyter server.
if askedEnter the URL of the running Jupyter server, specify the URI of the server (hostname) with the authentication token included in a
?token=URL parameters. (Typically, if you start the server in the VS Code terminal with the authentication token enabled, the URL with the token will appear in the terminal output, from where you can copy it.) Alternatively, you can provide a username and password after typing the URI have stated.(Video) Setting up Jupyter Notebook in Visual Studio Code
Monitoring:For added security, Microsoft recommends configuring your Jupyter server with safeguards such as SSL and token support. This ensures that requests sent to the Jupyter server are authenticated and connections to the remote server are encrypted. For instructions on securing a portable server, seeJupyter Documentation.