Work with Jupyter notebooks using AI in VS Code
Use AI throughout your notebook workflow. An agent can create a Jupyter notebook, edit and run cells, inspect outputs, and iterate on errors. For focused changes, use inline chat directly in a notebook cell.
This guide shows how to use AI for data analysis in a Jupyter notebook while you review the generated code and control when it runs.
Prerequisites
-
Set up GitHub Copilot in VS Code.
-
Install the Jupyter extension. To learn how to configure a Python environment and select a kernel, see Jupyter notebooks in VS Code.
-
Open a workspace that you trust. Running a notebook cell executes code in the selected kernel and can access workspace files.
Create a notebook with an agent
Start with the outcome you want instead of specifying individual cells. The agent plans the notebook, creates Markdown and code cells, and can run the code to validate the result.
-
Open the Chat view (⌃⌘I (Windows, Linux Ctrl+Alt+I)) and start a Local agent session.
-
Add your data file as context, then describe the analysis and output you want.
Create a Jupyter notebook that uses pandas and seaborn to analyze #housing.csv. Clean the data, summarize key metrics, and visualize the price distribution. -
Review any requested tool and command approvals before you continue. If the notebook does not have a kernel, follow the prompts to select or create a Python environment.
The agent creates an .ipynb file that you can continue to refine in the same session. You can also start your prompt with /newNotebook to explicitly request a new Jupyter notebook.
Iterate by editing and running cells
Agents have notebook-specific tools for understanding the cell structure, editing cells, running code, and reading cell outputs. This edit-run loop lets the agent validate the analysis and respond to runtime errors instead of only generating a static notebook.
Ask the agent to continue from the current notebook:
Run the notebook from top to bottom. Fix any errors you find, then explain the main result.
You can also request a specific revision:
Add a section that identifies outliers, explains how they affect the analysis, and compares the results before and after removing them.
Keep the notebook editor open to review cell changes and outputs as the agent works. Configure agent permissions to control which tool and command invocations require approval. Learn more about agent approvals.
Add notebook context to a prompt
Give the agent notebook-specific context when a request depends on the current kernel state or a cell result.
Add a kernel variable
With the notebook active, type # in the chat input and select a kernel variable from the suggestions. The selected variable becomes available as context for your request.
For example, add the df variable and enter:
Check this DataFrame for missing values and recommend a cleaning strategy.
Add a cell output
From the cell output toolbar, select Add Cell Output to Chat to attach a supported output. You can then ask the agent to explain a chart, diagnose an error, or update the notebook based on the result.
Explain the outliers in this chart and update the analysis to investigate them.
Make focused cell changes with inline chat
Use inline chat when you want to change one cell without starting a broader agent workflow.
-
Place the cursor in a cell or select the code you want to change.
-
Press ⌘I (Windows, Linux Ctrl+I) and describe the change.
-
Review the suggested diff. Select Accept and Run to apply the change and execute the cell in one step.
To generate a new cell, select Generate between cells or press ⌘I (Windows, Linux Ctrl+I) when a cell editor is not focused. The Generate action is experimental and is controlled by the notebook.experimental.generate setting.
Follow cell execution (Experimental)
To keep the cell that an agent is running in view, enable the github.copilot.chat.notebook.followCellExecution.enabled setting. After the agent starts to run cells, use the pin action in the notebook toolbar to pause or resume following the current execution.