Try agent mode in VS Code!
Dismiss this update
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.
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 the chat mode and enter a prompt in the Chat view.
With ask 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:
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 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.
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 Copilot > 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.
To make larger edits, across multiple cells, you can use the chat interface in edit mode or agent mode.
Example prompts:
Notice that you can use the overlay controls to navigate between the different edit suggestions, and to keep or undo the changes.
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 to ask mode in chat.
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.
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.