Edit Jupyter notebooks with AI in VS Code
Visual Studio Code supports working withJupyter notebooks natively, and throughPython code files. The AI features in VS Code can help you in creating and editing notebooks, as well as analyzing and visualizing data. In this article, you learn how to use the AI features in VS Code to work with Jupyter notebooks.
Scaffold a new notebook
To accelerate getting started with a new notebook, you can use the AI features in VS Code to scaffold a new notebook. You can provide details about what functionality you want to add and which libraries you want to use.
To create a new notebook with AI, choose thechat mode and enter a prompt in the Chat view.
Withask mode in chat, use the/newNotebook
command in the chat input field to indicate that you want to scaffold a new notebook.
Agent mode provides a more autonomous experience, where it can make changes to the notebook, run the cells, and monitor and resolve potential run-time issues.
Example prompts:
- Create a Jupyter notebook to read data from #housing.csv
- Create a Jupyter notebook to read data from #housing.csv, add a step to clean the data
- Create a notebook to read data from #housing.csv and plot the distribution of prices
- /newNotebook using pandas and seaborn to read and visualize the titanic dataset. Show key information from the dataset. (ask mode)
- Create a Jupyter notebook to read data from #housing.csv. Run all cells. (agent mode)
The following screenshot shows how the output from agent mode to the promptCreate a Jupyter notebook to read data from #housing.csv (you can get this dataset fromKaggle):
Notice that a new.ipynb
file is created in the workspace, which contains Markdown and code cells for reading the CSV file and displaying the first few rows of the data.
You can now further edit the notebook manually, or use AI to make inline edits or send follow-up chat requests to modify the notebook.
Make inline edits in notebooks
If you already have a notebook and want to make some inline changes in a cell, you can use inline chat, like you would in a code file.
To make inline edits in a cell, press⌘I (Windows, LinuxCtrl+I) or right-click in the cell and selectCopilot >Editor Inline Chat. This opens the inline chat view, where you can enter your prompt.
You can reference kernel variables in your chat prompt. Type#
followed by the variable name to reference it. For example, if you have a variable nameddf
, you can type#df
in your chat prompt to reference it.
When the response is generated, notice that the code is updated in the notebook cell. You canAccept the changes and decide toAccept and Run the cell changes.
To generate a new cell with AI, select theGenerate button in the notebook view, or don't focus on a cell and press⌘I (Windows, LinuxCtrl+I) to open the inline chat view for a new cell.
Make edits across multiple cells
To make larger edits, across multiple cells, you can use the chat interface inedit mode oragent mode.
Example prompts:
- Plot a graph of the price distribution
- Make sure the data is cleaned before visualizing and processing it
- Show the correlation between different features in the dataset
- Use matplotlib instead of seaborn to plot the data
- Remove the display of dataset information
Notice that you can use the overlay controls to navigate between the different edit suggestions, and to keep or undo the changes.
Ask questions about notebook content
You can use the chat interface to ask questions about the content of your notebook. This is useful for getting explanations of code, data, or visualizations. You can add extra context to your chat request, such as the cell output, graphs, or errors.
The following example shows how to ask questions about a visualization in a notebook.
Change toask mode in chat.
Select
...
next to the graph, and selectAdd Cell Output to Chat to add the chart as context to your chat request.Enter the promptExplain this chart in the chat input field.
Notice that you get a detailed explanation of the chart.
Perform data analysis and visualization
You can do a full data analysis and visualization notebook of a dataset by using agent mode in chat. Agent mode analyzes the dataset, and then scaffolds a new notebook, implements the code for performing the data analysis, and runs the cells to process and visualize the data. As needed, agent mode invokes relevant tools and terminal commands to complete its tasks.
For example, to perform a data analysis of the housing dataset:
Openagent mode in chat.
Enter the following prompt in the chat input field:Perform data analysis of the data in #housing.csv.
Notice that agent mode iterates through the different tasks. When needed, approve the tool and command invocations.
The result is a new notebook with a complete data analysis of the dataset, including data cleaning, data visualization, and statistical analysis.
You can now further edit the notebook manually, or use AI to make inline edits or send follow-up chat requests to modify the notebook.
Next steps
- Learn more about Jupyter notebooks in VS Code
- Learn more about the AI features in VS Code
- Learn more about chat in VS Code