Git Product home page Git Product logo

jupyter-vim's Introduction

Jupyter-Vim

A two-way integration between Vim and Jupyter. Develop code on a Jupyter notebooke without leaving the terminal. Send lines from Vim to a jupyter qtconsole. Have a MATLAB-like "cell-mode".

Currently Python and Julia kernels are supported, and more languages are on the way.

Video demo

Installation of the plugin

To install this plugin, you should use one of the following methods.

On Windows, replace in the next sections the Unix directories with the following:

  • On Vim:

    • ~/.vim -> %USERPROFILE%\vimfiles
    • ~/.vimrc -> %USERPROFILE%\_vimrc
  • On Nvim:

    • ~/.local/share/nvim/site -> %USERPROFILE%\AppData\Local\nvim

Or other directories if you have configured/installed Vim/Nvim differently.

Vim 8 package manager

mkdir -p ~/.vim/pack/git-plugins/start
git clone --depth 1 https://github.com/jupyter-vim/jupyter-vim.git ~/.vim/pack/git-plugins/start/jupyter-vim

NeoVim

mkdir -p ~/.local/share/nvim/site/pack/git-plugins/start
git clone --depth 1 https://github.com/jupyter-vim/jupyter-vim.git ~/.local/share/nvim/site/pack/git-plugins/start/jupyter-vim

Pathogen

cd ~/.vim/bundle
git clone https://github.com/jupyter-vim/jupyter-vim.git

Vundle

Plugin 'jupyter-vim/jupyter-vim'

Vim-Plug

Plug 'jupyter-vim/jupyter-vim'

Vim+Python configuration

In order for this plugin to work, you must have Jupyter installed in the Python environment that vim's pythonx command uses. There are several possible strategies here.

Install jupyter into system python

When Vim is using your system python (the default on linux), you can just install Jupyter using a package manager. For example, on Ubuntu 18.04 and later:

$ sudo apt install jupyter jupyter-core

Alternatively, you can use pip:

$ sudo pip install jupyter

Dynamic mode

When Vim is compiled with Python in dynamic mode (+python3/dyn), you can point to the Python interpreter you wish to use in your .vimrc:

if has('nvim')
    let g:python3_host_prog = '/path/to/python/bin/python3'
else
    set pyxversion=3

    " OSX
    set pythonthreedll=/Library/Frameworks/Python.framework/Versions/3.6/Python

    " Windows
    set pythonthreedll=python37.dll
    set pythonthreehome=C:\Python37
endif

Make sure to point it to a Python installation that has Jupyter installed.

Virtual environments

If either:

  • you use a Python environment manager such as virtualenv, and thus need Jupyter to be present no matter which environment is loaded from the shell you open vim from, or
  • you only use one Python environment but you don't want to install Jupyter system-wide for whatever reason,

then the easiest way to meet the Jupyter requirement is to configure vim to load a designated virtualenv at startup. This is just to allow vim to call the Jupyter client; you can run your Jupyter server in whatever Python environment you want. From Vim, run:

:pythonx import sys; print(sys.version)

This will tell you whether pythonx is using Python 2 or Python 3. (Or, see :help python_x if you'd like to tweak your pythonx settings.) Create a virtualenv with that python version, for example

$ virtualenv -p /usr/bin/python2.7 /path/to/my/new/vim_virtualenv

or

$ virtualenv -p /usr/bin/python3 /path/to/my/new/vim_virtualenv

and then install Jupyter in that environment:

$ source /path/to/my/new/vim_virtualenv/bin/activate
$ pip install jupyter

Finally, tell vim to load this virtualenv at startup by adding these lines to your vimrc:

" Always use the same virtualenv for vim, regardless of what Python
" environment is loaded in the shell from which vim is launched
let g:vim_virtualenv_path = '/path/to/my/new/vim_virtualenv'
if exists('g:vim_virtualenv_path')
    pythonx import os; import vim
    pythonx activate_this = os.path.join(vim.eval('g:vim_virtualenv_path'), 'bin/activate_this.py')
    pythonx with open(activate_this) as f: exec(f.read(), {'__file__': activate_this})
endif

Jupyter configuration

First, we need to configure the jupyter console and qtconsole clients to display output from other clients.

The config files can be found in in ~/.jupyter, if they don't exist yet you can generate them with:

$ jupyter console --generate-config
$ jupyter qtconsole --generate-config

Now you need to uncomment and change the following config options to True.

For qtconsole:

c.ConsoleWidget.include_other_output = True

For console:

c.ZMQTerminalInteractiveShell.include_other_output = True

Usage

To begin a session:

$ jupyter qtconsole &  # open a jupyter console window
$ vim <your_script>.py

In vim: :JupyterConnect

Then, use :JupyterRunFile, or :[range]JupyterSendRange to execute lines of code!

Code will be sent and executed as expected in the graphical jupyter qtconsole. However, in the console version jupyter console, the result will only show after you press the Enter key.

By default, the following keybindings are defined:

" Run current file
nnoremap <buffer> <silent> <localleader>R :JupyterRunFile<CR>
nnoremap <buffer> <silent> <localleader>I :PythonImportThisFile<CR>

" Change to directory of current file
nnoremap <buffer> <silent> <localleader>d :JupyterCd %:p:h<CR>

" Send a selection of lines
nnoremap <buffer> <silent> <localleader>X :JupyterSendCell<CR>
nnoremap <buffer> <silent> <localleader>E :JupyterSendRange<CR>
nmap     <buffer> <silent> <localleader>e <Plug>JupyterRunTextObj
vmap     <buffer> <silent> <localleader>e <Plug>JupyterRunVisual

nnoremap <buffer> <silent> <localleader>U :JupyterUpdateShell<CR>

" Debugging maps
nnoremap <buffer> <silent> <localleader>b :PythonSetBreak<CR>

Set let g:jupyter_mapkeys = 0 in your .vimrc to prevent the default keybindings from being made.

Info

Once we fell in love with Vim, we couldn't bear having to jump back and forth between the ipython/jupyter console and editor anymore. We modeled this simple interface off of the ideas in vim-ipython, but have pared down many of the features, like the replication of the Jupyter console in a vim buffer, to make the plugin much more 'lightweight'.

Still a work in progress!

Troubleshooting

  • Make sure that you are running Vim 8 or higher with Python 3 support.
  • When on windows, you are probably running 64-bit Python. Make sure you also run a 64-bit version of Vim (the default install is 32-bit!).

Contributing

Please feel free to raise issues and pull requests on the github repository.

Credits

We owe significant thanks to the original developer of this plugin: Paul Ivanov. It is far easier to update something that already works well than to forge a new path from scratch.

jupyter-vim's People

Contributors

arnaldorusso avatar broesler avatar bzinberg avatar derweh avatar dragly avatar drestebon avatar dstahlke avatar enzbang avatar galou avatar infokiller avatar ivanov avatar jjhelmus avatar khirurgia avatar kimborgen avatar koepsell avatar luispedro avatar minrk avatar nonameentername avatar ogrisel avatar pag avatar pielgrzym avatar rjandrade avatar saddikulla-vishnu avatar tinmarino avatar wilywampa avatar wmvanvliet avatar zeekay avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

jupyter-vim's Issues

Jupyter-vim should check jupyter runtime path for json files

My Jupyter installation seems to put the connection json files in ~/Library/Jupyter/runtime/. I'm on Mac, so perhaps that explains the difference? Anyway, I'd like to make jupyter-vim check that path, in the event that the connection json files aren't found in the current directory. I notice that other places do it like this: https://github.com/jupyter/jupyter_client/blob/34f96d7293e55364690c1dbdff8647ffb84854a3/jupyter_client/consoleapp.py#L168 so I think something like that is the right fix.

Should g:jupyter_monitor_console=1 be the default?

This one might be a bit controversial: Given that you need configuration in order to see output in jupyter console/qtconsole, I'd recommend making g:jupyter_monitor_console=1 the default. This would make jupyter-vim have a less steep learning curve. It's a lot easier to disable the console once you have the tool working than to be running the tool and be frustrated that you don't see anything happening. We can even output a message to the console explaining how to disable it. Thoughts?

broesler's version of README.md missing useful information that exists in wmvanvliet's version

In particular, I found the section on "First, we need to configure the jupyter console and qtconsole clients to display output from other clients" useful and I struggled to figure out how to do this. Later, I discovered that it was already documented in wmvanvliet's version.

This is sorta implicitly documented in https://github.com/broesler/jupyter-vim/blob/master/doc/jupyter-vim.txt (in the "JUPYTER-VIM VS. VIM-IPYTHON" section), but I think it should be much more prominent.

Can't change console theme

I'm running nvim on Ubuntu 20.04, when I run jupyter qtconsole, the console opens up but I want to change the colorscheme which isn't happening and I'm getting the following error in the terminal

TypeError: <lambda>() missing 1 required positional argument: 'v'

Python version 3.9.5

Shall we create a "jupyter-vim" organization?

At some point, I forked from ivanov/vim-ipython, then this repo was forked from me. You cleaned everything up, doing such a good job that my branch now tracks this repo. People have raised issues and PRs on both my repo and this one. It's a bit of a mess.

How do you feel about creating a "jupyter-vim" github organization and work together in one repo?

`block` is not a valid parameter to iopub_channel get_msgs

https://github.com/broesler/jupyter-vim/blob/ca4296f/pythonx/jupyter_vim.py#L273 is
msgs = kc.iopub_channel.get_msgs(block=False)

This didn't work for me. According to https://github.com/jupyter/jupyter_client/blob/master/jupyter_client/blocking/channels.py#L59 get_msgs doesn't take any parameters (aside from self), and setting block to False is unnecessary since it only returns msgs that are currently ready.

block=True is a valid parameter to get_msg (singular) and it's what get_msgs uses internally.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.