avataralpha2phi

Summary

This article explores several possible ways to use Jupyter with Vim/Neovim code editing experience.

Abstract

The article discusses the use of Jupyter Ascending, Firenvim + Jupyter, and Magma-nvim to integrate Jupyter with Vim/Neovim code editing experience. Jupyter Ascending lets you edit Jupyter notebooks from your favorite editor, then instantly sync and execute that code in the Jupyter notebook running in your browser. Firenvim turns the browser into a Neovim client, and Magma-nvim uses Jupyter Client to communicate with the Jupyter kernel. The article also mentions other plugins that take a console or terminal-based approach, such as jupyter-vim, vim-slime, iron.nvim, sniprun, and neoterm.

Opinions

  • The author prefers using Jupyter with Vim/Neovim code editing experience for data science projects.
  • The author mentions that other plugins which integrate with Jupyter do not provide the desired code editing experience.
  • The author provides a non-comprehensive list of other plugins that integrate either with Jupyter or Python REPL.
  • The author suggests that Neovim's extensibility using Lua should lead to better integration between Jupyter and Neovim in the future.
  • The author mentions that VSCode has the best-integrated development experience with Jupyter.

Jupyter Notebook + Vim/Neovim

Vim/Neovim code editing experience for Jupyter.

Vim/Neovim + Jupyter

Overview

For data science projects, normally I use Jupyter with vim bindings. In some of the previous articles, I also mentioned several plugins which integrate with Jupyter but the experience is not quite what I am looking for.

In this article, let’s explore several possible ways that we can truly use Jupyter with Vim/Neovim code editing experience.

Table of Content

· Overview · Table of Content · Jupyter AscendingInstall Jupyter AscendingCreate a Notebook and Python FileVim PluginStart Jupyter NotebookTesting · Firenvim + JupyterPluginBrowser AddonTesting · MagmaInstallationTesting · Console based Plugins · Other Plugins

Jupyter Ascending

Jupyter Ascending lets you edit Jupyter notebooks from your favorite editor, then instantly sync and execute that code in the Jupyter notebook running in your browser.

Install Jupyter Ascending

Run this command to install Jupyter Ascending and the extensions.

pip install jupyter_ascending && \
jupyter nbextension install jupyter_ascending --sys-prefix --py && \
jupyter nbextension enable jupyter_ascending --sys-prefix --py && \
jupyter serverextension enable jupyter_ascending --sys-prefix --py

Validate the installation status.

jupyter nbextension     list
jupyter serverextension list
Jupyter Ascending Installation

Create a Notebook and Python File

Now let’s create a notebook and the corresponding Python file.

python -m jupyter_ascending.scripts.make_pair --base 01_data_prep
Create Notebook and Python File

In my case, I create a notebook called 01_data_prep.sync.ipynb and Python file called 01_data_prep.sync.py.

Vim Plugin

Install the Vim plugin for Jupyter Ascending. In my case, I install it using packer.nvim.

use { "untitled-ai/jupyter_ascending.vim" }

Start Jupyter Notebook

Start the Jupyter notebook instance.

jupyter notebook 01_data_prep.sync.ipynb
Start Jupyter Notebook
Jupyter Notebook

Testing

From within Vim/Neovim, I use #%% marker to separate the cells. By default, the following mappings are defined.

nmap <space><space>x <Plug>JupyterExecute
nmap <space><space>X <Plug>JupyterExecuteAll
Jupyter Ascending Testing

Compared to other plugins which use a console or terminal-based approach, now you still have the notebook with the output. However, this plugin does not support JupyterLab currently.

On the contrary, if you prefer a notebook-based approach, check out nbdev.

Firenvim + Jupyter

I mentioned Firenvim in a previous article. Firenvim turns the browser into a Neovim client.

Plugin

Install the plugin using packer.nvim.

use {
  "glacambre/firenvim",
  run = function()
    vim.fn["firenvim#install"](0)
  end,
}

Browser Addon

Depending on your browser, you need to install the Firefox or Chromium-based browser add-on.

Testing

Start JupyterLab and start to have fun!

Firenvim + JupyterLab
  • Click on any text area and it will be immediately replaced by an instance of Firenvim.
  • To set the content of the now hidden text area to the content of the Neovim instance, simply :w. If you want to close the Firenvim overlay and return to the text area, use :qor :x. If you selected an element where you expected the Firenvim frame to appear and it didn't, try pressing <C-e>.
  • Tips: Use set lines=80 and set columns=100 to have a larger frame for editing.

Magma

In the previous 2 approaches,

  • we need the Jupyter notebook instance running
  • we still have the advantage of having the notebook with the output

Magma-nvim is taking a different approach. It uses Jupyter Client to communicate with the Jupyter kernel.

Installation

This plugin works in Linux since it requires certain dependencies to be installed, e.g. ueberzug.

  • pynvim (for the Remote Plugin API)
  • jupyter_client (for interacting with Jupyter)
  • ueberzug (for displaying images)
  • Pillow (also for displaying images, should be installed with ueberzug)
  • cairosvg (for displaying SVG images)
  • pnglatex (for displaying TeX formulas)
  • plotly and kaleido (for displaying Plotly figures)

I use packer.nvim to install the plugin.

use { 'dccsillag/magma-nvim', run = ':UpdateRemotePlugins' }

Testing

With the plugin installed, now I can interact with a Jupyter kernel interactively from within Neovim.

Magma

Console based Plugins

There are other plugins that take a console or terminal-based approach, e.g. jupyter-vim, vim-slime, iron.nvim, sniprun or neoterm. Check out the following articles!

Other Plugins

Every developer has his or her unique development workflow and preferences. Here is a non-comprehensive list of other plugins you can explore to see if they fit your requirements. They integrate either with Juptyer or Python REPL.

VSCode so far has the best-integrated development experience with Jupyter. With Neovim's extensibility using Lua, we should expect better integration between Jupyter and Neovim in the future.

If you are not a Medium member yet and want to become one, click here. (A part of your subscription fee will be used to support alpha2phi.)

Jupyter Notebook
Coding
Programming
Data Science
Hacking
Recommended from ReadMedium