Edit Jupyter notebooks with AI in VS Code
Visual Studio Code supports working with Jupyter notebooks natively, and through Python 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. Use natural language to provide details about what functionality you want to add and which libraries you want to use.
To create a new notebook with AI, choose either of these options:
-
Type the
/newNotebook
slash command in the chat input box, followed by the details of the notebook to create.Example prompts:
/newNotebook use pandas and seaborn to read and visualize the titanic dataset. Show key information from the dataset.
/newNotebook /newNotebook analyze the data in the housing.csv file
-
Switch to agent mode, and type a natural language prompt asking to create a new notebook.
Example prompts:
Create a notebook to read data from #housing.csv and plot the distribution of prices
Create a Jupyter notebook to read data from #housing.csv. Run all cells.
The following screenshot shows how the output from agent mode to the prompt Create a Jupyter notebook to read data from #housing.csv (you can get this dataset from Kaggle):
Notice that a new .ipynb
file is created, 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 notebook cells
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, Linux Ctrl+I) or right-click in the cell and select Generate Code > 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 named df
, 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 can Accept the changes and decide to Accept and Run the cell changes.
To generate a new cell with AI, select the Generate button in the notebook view, or don't focus on a cell and press ⌘I (Windows, Linux Ctrl+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 switch to agent mode in the Chat view. Provide a prompt to request changes to the notebook and agent mode will iterate through the tasks to implement the changes.
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.
-
Select
...
next to the graph, and select Add Cell Output to Chat to add the chart as context to your chat request. -
Enter the prompt Explain 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:
-
Open agent 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.