Using Jupyter Notebook in VS Code
Jupyter Notebook is an open-source interactive computing environment that allows you to combine Markdown documents and executable code. VS Code natively supports Jupyter Notebook.
Environment Preparation
Install Dependencies
- Python Environment: Ensure Python is installed.
- Jupyter Package: Execute
pip install jupyterin the terminal. - VS Code Extensions: Install the Python and Jupyter extensions.
Select Interpreter
- Press
Ctrl+Shift+Pto open the Command Palette. - Type "Python: Select Interpreter".
- Select your installed Python environment.
Creating and Opening Notebooks
Create a New Notebook
Method 1: Using the Command Palette
- Press
Ctrl+Shift+P. - Type "Create: New Jupyter Notebook".
- Press Enter.
Method 2: Creating a file directly
- Create a file with the
.ipynbextension.
Select Kernel
Click the kernel selector in the top right corner and select the Python kernel.
Running Code Cells
Common Shortcuts
| Shortcut | Function |
|---|---|
Ctrl+Enter | Run current cell |
Shift+Enter | Run current cell and jump to next |
Alt+Enter | Run current cell and insert new one below |
Batch Running
- Run All: Run all cells.
- Run All Above: Run all cells above the current one.
- Run All Below: Run all cells below the current one.
Cell Operations
Cell Modes
| Mode | Characteristics | How to Enter |
|---|---|---|
| Command Mode | Blue bar on left; execute shortcut commands. | Press Esc |
| Edit Mode | Green bar on left; edit content. | Press Enter |
Adding Cells
- Click the + button between cells.
- In Command Mode, press
Ato add above,Bto add below.
Deleting Cells
- Click the Delete icon in the cell toolbar.
- In Command Mode, press
dd.
Switching Cell Type
In Command Mode:
- Press
Mto switch to Markdown. - Press
Yto switch to Code.
Variable Explorer
- Run a code cell containing variables.
- Click the "Variables" icon in the toolbar.
- View all variables in the current session.
Saving and Exporting
- Use
Ctrl+Sto save. - Export to Python (.py), HTML, or PDF via the Export button.
Debugging
Run by Line
Click "Run by Line" button for step-by-step execution.
Full Debugging
- Set breakpoints by clicking the left margin.
- Click "Debug Cell" to start debugging.
- Use the Debug toolbar to control execution.
Search
Press Ctrl+F to search. Use the funnel icon to filter search scope.