Skip to content
This repository was archived by the owner on Nov 7, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions rplugin/python/nvim_ipy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from functools import partial, wraps
from collections import deque
import os, sys
import time, datetime
import json
import re
import neovim
Expand Down Expand Up @@ -119,7 +120,6 @@ def __call__(self, msg):
self.is_active = False

@neovim.plugin

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why remove this? Note that decoding already is default in python3, so this ensures consistent behaviour also in python2.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This got in by mistake and i thought i had deleted it.

@neovim.encoding(True)
class IPythonPlugin(object):
def __init__(self, vim):
self.vim = vim
Expand Down Expand Up @@ -155,7 +155,8 @@ def create_outbuf(self):
buf = vim.current.buffer
buf.options["swapfile"] = False
buf.options["buftype"] = "nofile"
buf.name = "[jupyter]"
self.base_buf_name = "[jupyter]"
buf.name = self.base_buf_name
vim.current.window = w0
self.buf = buf

Expand Down Expand Up @@ -258,14 +259,17 @@ def ipy_run(self, args):
self.km.restart_kernel(True)
return

start_time = time.time()
self.buf.name = self.base_buf_name + "[*]({})".format(datetime.datetime.now().strftime('%H:%M:%S'))

reply = self.waitfor(self.kc.execute(code))
content = reply['content']
payload = content['payload']
for p in payload:
if p.get("source") == "page":
# TODO: if this is long, open separate window
self.append_outbuf(p['text'])

self.buf.name = self.base_buf_name + '({}s elapsed)'.format(time.time()-start_time)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't this be done by using %time ( or corresponding macro in other lang) ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it should be possible to have execution time in there. Specifically in a REPL setting would allow to quickly find identify the hotspots in the code by running code line by line without having to modify the code. But maybe all that is needed is to be able to "decorate" each call to the kernel and display the output somewhere. This way it would be possible to wrap each command with code to compute a timedelta.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using kernel-side %time is more reliable, because if one enqueues several commands (i e sends more requests before the last one is done) the timing will still be correct

But maybe all that is needed is to be able to "decorate" each call to the kernel and display the output somewhere.

That should be possible with something like
nnoremap <Leader>t :call IPyRun('%time '.getline('.'))<cr>

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it is pretty easy once you come up with the idea. It this also works nicely for visual mode:

let g:ipy_vprefix = "%%time \n"
vnoremap <Plug>(IPy-Run) :<c-u>call IPyRun(g:ipy_vprefix . <SID>get_visual_selection())<cr>

I think I'd be nice to have such a feature out-of-the-box, as not everybody using the plugin should come up with their own a solution. The problem with this solution is that it is python specific.

using kernel-side %time is more reliable, because if one enqueues several commands (i e >sends more requests before the last one is done) the timing will still be correct

Good point. The only reason to do it on the client side is that it would work for all kernels and the output could be put anywhere (e.g. airline), avoiding the pollution of the output buffer. If the user is interested in the timing she should not enqueue commands.

@neovim.function("IPyComplete")
@ipy_events
def ipy_complete(self,args):
Expand Down