How can I create and execute a cell in Jupyter Notebook server from a separate process (e.g., an editor, vim)?

Viewed 499

I want to call an api on a running jupyter notebook to create or edit an existing cell, and to run it. When this is done, the browser page should also update, to show both the contents of the cell, and the output.

The intended use case for this api is a plugin for the vim editor that will take highlighted lines and execute them. The motivation for using jupyter notebook is that will allow visualization of plots and images using matplotlib, even though both vim and the notebook will be running on a remote server. The user would have a browser page open to the notebook, and separately a terminal open potentially that is ssh'ed to the machine and editing python code.

VSCode does exactly this in its python interactive mode, but I prefer to use the vim editor natively than switch to vscode simply to be able to do this.

3 Answers

There are several plugins being actively developed for this, and without having used them, it seems only vimpyter interacts with jupyter on the browser. Others like jupyter-vim and vim-ipython open the notebook inside a vim buffer.

If you can live without inline plots I find confortable having a neoterm terminal buffer where I can send lines to and from IPython.

You could use vscode's built in jupyter notebook support to convert the notebook into a python file

this is not an exact answer, but I've written a couple Jupyter plugins that may be helpful to you in this endeavor, JupyterGraffiti and JupyterTerminals. Graffiti has an API you can call to execute some of its functions, and it supports inline terminals you can control. JupyterTerminals provides only those inline terminals, but also has buttons you can add to control them. You could start and run vim inside these terminals. (If it's useful an API could be added to the JupyterTerminals plugin like Graffiti has.)

enter image description here

Related