Notebooks Support
Relevant source files
The following files were used as context for generating this wiki page:
Notebook support in Visual Studio Code is a modular system driven by extensions that contribute notebook types, serializers, and renderers. The core functionality allows for rich, interactive document experiences, with specific extensions providing support for formats like Jupyter Notebooks (.ipynb) and custom notebook types for testing. The rendering of notebook output, particularly for Markdown and rich media, is handled by a flexible system of notebook renderers that can be extended by other extensions.
This system is primarily defined through package.json contribution points. Key extensions like ipynb register to handle .ipynb files, while notebook-renderers provides a set of default renderers for common MIME types. The markdown-language-features extension is crucial, offering a powerful, extensible renderer for Markdown content within notebook cells.
Notebook Registration
Extensions can register to handle specific notebook types using the notebooks contribution point in their package.json. This registration associates a type with a displayName and a selector that matches file patterns.
The following diagram illustrates the registration flow for different notebook types found in the codebase.
This diagram shows how extensions register different notebook types with the core Notebook Service.
Sources: extensions/ipynb/package.json:84-93, extensions/vscode-api-tests/package.json:181-207
Registered Notebook Types
| Notebook Type | Display Name | Filename Pattern | Contributing Extension |
|---|---|---|---|
jupyter-notebook | Jupyter Notebook | *.ipynb | ipynb |
notebookCoreTest | Notebook Core Test | *.vsctestnb | vscode-api-tests |
notebook.nbdtest | notebook.nbdtest | **/*.nbdtest | vscode-api-tests |
notebook.nbdserializer | notebook.nbdserializer | **/*.nbdserializer | vscode-api-tests |
| Sources: extensions/ipynb/package.json:84-93, extensions/vscode-api-tests/package.json:181-207 |
Notebook Renderers
The notebookRenderer contribution point allows extensions to provide renderers for specific MIME types within notebook outputs. This is how content like images, HTML, and formatted text is displayed. A renderer is defined by an id, a displayName, an entrypoint script, and a list of mimeTypes it can handle.
Renderer Architecture
The system allows for renderer extension. A base renderer, like the vscode.markdown-it-renderer, can be extended by other renderers to add functionality. For example, the markdown-math extension extends the base Markdown renderer to add support for KaTeX.
This diagram shows the relationship between different renderers and the MIME types they handle.
Key Renderers
| Renderer ID | Display Name | Handled MIME Types (Partial List) | Contributing Extension |
|---|---|---|---|
vscode.builtin-renderer | VS Code Builtin Notebook Output Renderer | image/png, image/jpeg, text/html, application/vnd.code.notebook.error | notebook-renderers |
vscode.markdown-it-renderer | Markdown it renderer | text/markdown, text/x-python, application/json | markdown-language-features |
vscode.markdown-it-katex-extension | Markdown it KaTeX renderer | Extends vscode.markdown-it-renderer | markdown-math |
vscode.markdown-it-cell-attachment-renderer | Markdown Cell Attachment Renderer | Extends vscode.markdown-it-renderer | ipynb |
| Sources: extensions/notebook-renderers/package.json:16-34, extensions/markdown-language-features/package.json:34-100, extensions/markdown-math/package.json:65-72, extensions/ipynb/package.json:95-102 |
The vscode.builtin-renderer handles a wide variety of common web and text formats, as well as special VS Code-specific types for streams and errors.
The vscode.markdown-it-renderer is notable for its extensive list of supported MIME types, which includes not only text/markdown but also many programming language formats, which it likely renders as syntax-highlighted code blocks.
Sources: extensions/markdown-language-features/package.json:38-99, extensions/notebook-renderers/package.json:20-33
Jupyter Notebook (.ipynb) Integration
The ipynb extension is the primary provider for Jupyter Notebook support. It activates when a jupyter-notebook type is opened and contributes commands, configurations, and a specific renderer for handling cell attachments.
Key Features
- Notebook Type: Registers the
jupyter-notebooktype for*.ipynbfiles. - Activation: Activates on
onNotebook:jupyter-notebook. - Commands: Provides commands for creating new notebooks (
ipynb.newUntitledIpynb) and interacting with cell outputs (notebook.cellOutput.copy). - Renderer: Contributes the
vscode.markdown-it-cell-attachment-rendererto handle embedded images in Markdown cells. - Configuration: Allows users to configure image pasting behavior via
ipynb.pasteImagesAsAttachments.enabled.
Sources: extensions/ipynb/package.json:13, extensions/ipynb/package.json:30-35, extensions/ipynb/package.json:42-67, extensions/ipynb/package.json:84-93, extensions/ipynb/package.json:95-102
Commands
The ipynb extension adds several commands to the command palette and context menus for a seamless user experience.
| Command ID | Title | UI Placement |
|---|---|---|
ipynb.newUntitledIpynb | New Jupyter Notebook | file/newFile, Command Palette |
notebook.cellOutput.copy | Copy | Command Palette, Cell Output Context Menu |
notebook.cellOutput.addToChat | Add to Chat | Cell Output Context Menu |
notebook.cellOutput.openInTextEditor | Open in Text Editor | Cell Output Context Menu |
| Sources: extensions/ipynb/package.json:42-67, extensions/ipynb/package.json:104-138 |
Configuration
| Setting | Type | Default | Description |
|---|---|---|---|
ipynb.pasteImagesAsAttachments.enabled | boolean | true | Enable/disable pasting images as attachments in .ipynb notebooks. |
ipynb.experimental.serialization | boolean | true | Enable/disable experimental serialization for .ipynb files. |
| Sources: extensions/ipynb/package.json:30-40 |