hanschen/vim-ipython-cell

Integration with qtconsole

randomgeek78 opened this issue · 3 comments

Hi,

I love this plugin. It works well with ipython and tmux - which has been its primary stated goal.

But I love the navigation features and general workflow and would like to make it work with jupyter qtconsole. jupyter qtconsole and jupyter console provides several features that as missing in ipython. I have been able to get neovim working with jupyter qtconsole using the nvim-ipy plugin. But I miss the workflow that this plugin provides, esp. jumping between cells, running a cell and jumping to the next cell, etc.

I am thinking of taking parts of vim-ipython-cell and make it work with nvim-ipy. Do you have any thoughts or suggestions?

Thanks.

Happy to hear that you like this plugin. :) I haven't used nvim-ipy, but it seems like it also supports cells? https://github.com/bfredl/nvim-ipy#cells
It doesn't seem to provide a command to jump between cells currently, so you could use this plugin for just this functionality for now. You should also be able to create a custom mapping to execute a cell (using nvim-ipy) and jumping to the next cell (using this plugin).

Other than that I completely support your idea of taking parts of this plugin to make it work for your workflow. The author of nvim-ipy mentions that they may implement cells as text objects later (which I think is a good idea), so they would probably be happy to see a pull request for that.

If you have any questions about the code feel free to ask here. I'll close this issue as, as you mentioned, this plugin focuses on integrating vim with targets supported by vim-slime, and more advanced integrations with e.g. qtconsole is out of scope. If there's an easy way to achieve the latter, feel free to add a comment and re-open the issue.

Hi @hanschen,

The minimal changes to integrate with nvim-ipy is actually very simple:

diff --git c/plugin/ipython-cell.vim w/plugin/ipython-cell.vim
index 73bad56..260c670 100644
--- c/plugin/ipython-cell.vim
+++ w/plugin/ipython-cell.vim
@@ -15,6 +15,7 @@ endif
 let g:ipython_cell_delimit_cells_by = get(g:, 'ipython_cell_delimit_cells_by', 'tags')
 let g:ipython_cell_tag = get(g:, 'ipython_cell_tag', ['# %%', '#%%', '# <codecell>', '##'])
 let g:ipython_cell_valid_marks = get(g:, 'ipython_cell_valid_marks', 'abcdefghijklmnopqrstuvqxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
+let g:ipython_cell_use_nvim_ipy = get(g:, 'ipython_cell_use_nvim_ipy', 0)

 function! s:UsingPython3()
   if has('python3')
diff --git c/python/ipython_cell.py w/python/ipython_cell.py
index a2900bd..bfc49c0 100644
--- c/python/ipython_cell.py
+++ w/python/ipython_cell.py
@@ -42,10 +42,8 @@ def execute_cell(use_cpaste=False):
         else:
             _slimesend("# empty cell")
     else:
-        try:
-            slime_python_ipython = vim.eval('g:slime_python_ipython')
-        except vim.error:
-            slime_python_ipython = False
+        slime_python_ipython = vim.vars.get('slime_python_ipython', 0) \
+                | vim.vars.get('ipython_cell_use_nvim_ipy', 0)

         if slime_python_ipython:
             _slimesend(cell)
@@ -338,8 +336,18 @@ def _slimesend(string):
     if not string:
         return

+    if vim.vars.get('ipython_cell_use_nvim_ipy', 0):
+        return _nvimipysend(string)
+
     try:
         vim.command('SlimeSend1 ' + CTRL_U + '{}'.format(string))
     except vim.error:
         _error("Could not execute SlimeSend1 command, make sure vim-slime is "
                "installed")
+
+def _nvimipysend(string):
+    try:
+        vim.command('IPyRun1 {}'.format(string))
+    except vim.error:
+        _error("Could not execute IPyRun1 command, make sure nvim-ipy setup "
+               "properly")

Finally, the current interface to IPyRun is very hard to properly execute. I have requested a small change to make it similar to the SlimeSend1 API nvim-ipy issue:

command! -nargs=+ IPyRun1 :call IPyRun(<q-args> . "\r")

With this, most of the functionality can be recovered. The transactions specific to ipython like timing etc don't work with jupyter as it has other commands but otherwise, it works great including cell jumps, etc.

@randomgeek78: Thanks, that looks reasonable. However, one thing I don't understand is why you don't use <Plug>(IPy-RunCell) from nvim-ipy to run cells. If you want the "run and jump to the next cell" feature, you should be able to do something like this (I haven't tried it):

 nmap <Leader>C <Plug>(IPy-RunCell) :IPythonCellNextCell<CR>

So to me it makes more sense to use nvim-ipy for Qtconsole integration and eventually add the navigation features to that plugin.