Jupyter for Visual Studio Code

repository·main·Indexed 23 days ago

https://github.com/microsoft/vscode-jupyter

Notebook support for various language kernels in VS Code, enabling users to create, edit, and run .ipynb files. Features include IntelliSense, debugging, native notebook UI, and support for Python, Julia, R, and C#. Includes a Jupyter API for extension developers via the @vscode/jupyter-extension package and documentation on implementing notebook export methods and ipywidgets integration.

Tokens
16.8K
Snippets
33
Records
88
Agent score
81%

What's inside vscode-jupyter

  1. Overview of Jupyter Extension Features

    main

    The Jupyter extension provides a rich notebook experience integrated with VS Code's core editor features:

    • IntelliSense: Provides auto-completion, code navigation, and syntax checking.
    • Notebook UI: Built on VS Code's native notebook API, supporting features like hot exit, find & replace, and code folding.
    • Editor Integration: Supports standard editor extensions like VIM, bracket colorization, and linters within cells.
    • Output Rendering: Includes built-in renderers for common MIME types (latex, plotly, vega, etc.) and supports custom installable renderers via the marketplace.
    • Notebook Diffing: Includes a specialized diff tool for comparing code cells, outputs, and metadata.
  2. Julia documentation format for completion documentation

    main
    The Jupyter extension supports Julia completion documentation. Documentation for Julia functions follows a specific format: a fenced code block containing the function signature, followed by a description, and optionally additional details or 'See also' references. This documentation is rendered in the VS Code IntelliSense interface to assist developers during coding.
  3. Requirements for hosting ipywidgets in non-notebook contexts

    main

    To display ipywidgets in a context that is not a standard Jupyter Notebook, the following three components are required:

    1. requirejs: For module loading.
    2. HTML Manager: To manage the widget lifecycle and rendering.
    3. Live Kernel: The widget manager must plug directly into kernel messages to read/write and build data for display.

    Note that because the kernel connection is only available in the backend (extension code), a proxy kernel connection must be implemented in the frontend (React layer) to bridge the HTML manager with the actual kernel.

  4. How the ipywidgets proxy kernel connection works

    main

    Since the HTML manager cannot access the backend kernel connection directly, the extension implements a proxy kernel connection in the React layer. This architecture uses a postoffice to facilitate communication:

    • Extension to React Layer: Kernel messages from the extension are sent to the React layer via the postoffice.
    • React Layer to Actual Kernel: Messages sent from the HTML manager via the kernel are routed back to the actual kernel via the postoffice.

    The implementation involves complex message processing logic, borrowing heavily from @jupyterlab/services (specifically comm.js, default.js, future.js, kernel.js, and manager.js) to ensure kernel messages are correctly sequenced and massaged.

  5. Handle R package conflicts

    main

    Conflicts occur when a function in a new package masks a function in an existing package. Conflict handling is governed by the conflicts.policy option.

    Conflict Policies

    • Default: If conflicts.policy is not set, conflicts result in warnings if warn.conflicts = TRUE.
    • "strict": All unresolved conflicts signal errors.
    • "depends.ok": Conflicts resulting from attaching declared dependencies will not produce errors, but other conflicts will. This is recommended for most users.

    Customizing the Policy

    You can pass a named list to options(conflicts.policy = ...) to fine-tune behavior:

    • error: logical; if TRUE treat unresolved conflicts as errors.
    • warn: logical; if TRUE issue a warning message when conflicts are found.
    • generics.ok: logical; if TRUE ignore conflicts created by defining S4 generics.
    • depends.ok: logical; if TRUE do not treat conflicts with required packages as errors.
    • can.mask: character vector of names of packages allowed to be masked (e.g., base packages).
  6. Quick Start with Python in Jupyter

    main

    To use Jupyter notebooks with Python in VS Code, follow these steps:

    1. Install VS Code.
    2. Set up a Python environment: Install Anaconda, Miniconda, or another Python environment that has the jupyter package installed.
    3. Install Extensions: Install both the Jupyter Extension and the Python Extension.
    4. Create a Notebook: Open the Command Palette (Ctrl+Shift+P) and select Jupyter: Create New Jupyter Notebook.
    5. Select a Kernel: Click the kernel picker in the top right of the notebook or run the Notebook: Select Notebook Kernel command to choose your Python environment.
  7. How to file issues and get help

    main

    To report bugs or request new features for the Jupyter extension, use GitHub Issues. It is recommended to search existing issues before filing a new one to prevent duplicates.

    For general questions, help, or discussions regarding the project, use the GitHub Discussions feature.

  8. Time Python execution with %timeit and %%timeit

    main

    You can measure the execution time of Python statements or expressions using the timeit magic command. It is available in two modes:

    • Line mode (%timeit): Used to time a single-line statement. Multiple statements can be chained using semicolons.
    • Cell mode (%%timeit): Used to time the entire body of a cell. The first line of the cell is treated as setup_code (executed but not timed), and the subsequent lines are the code to be timed. The timed body has access to variables defined in the setup code.
  9. Detach or unload R packages

    main

    To remove a package from the search list, use the detach() function.

    Detaching by Name

    If you attached a package normally, you can detach it using its position in the search list:

    detach("package:splines")

    Detaching from a Character Vector

    If the package name is in a variable, you must find its position in the search list using match():

    pkg <- "splines"
    detach(pos = match(paste("package", pkg, sep = ":"), search()))

    Reloading Packages

    library() and require() do not reload a namespace that is already loaded. To reload a package, you must first call detach(unload = TRUE) or unloadNamespace.

    # If the package name is in a character vector, use
    pkg <- "splines"
    library(pkg, character.only = TRUE)
    detach(pos = match(paste("package", pkg, sep = ":"), search()))