jkitchin / scimax

An emacs starterkit for scientists and engineers
Other
1.03k stars 123 forks source link

Autocomplete and Function helper within org babel #203

Closed shrysr closed 1 year ago

shrysr commented 6 years ago

Is there any way to have a pop-up or reference with an overview of the function's arguments or help contents, from within an org-babel code block? I would use it to quickly refer to the structure and arguments that can be passed in.

I see that a separate .py file provides auto-completion of code, but within org-babel source blocks, I just get 'word' suggestions. I suppose an option is using C-c ' within a code block. However I still don't have a 'helper' or pop up with information of the function or method even with the .py file.

For example: I mean the pop up available in a Jupyter notebook using 'shift-tab'. I've seen this in some example videos. In particular, I am looking at this for Python and R.

This SO discussion seems to suggest there is no solution within org-babel.

jkitchin commented 6 years ago

With ob-ipython, you can run ob-ipython-inspect (with scimax) in an org-buffer to get a buffer that has the pydoc output for the thing at your point. With C-c ', I get a minibuffer signature of the function that is at point.

In commit 78eda40 I just added something that kind of simulates this feature for ob-ipython using an idle timer. You could check it out and see if it is kind of close. It does not work on things the current kernel doesn't know about, e.g. you haven't executed the block already, or added an import that hasn't been executed already. it seems ok though.

shrysr commented 6 years ago

I have updated to the latest commit and do not see any particular changes. I tried ob-ipython-inspect, after executing a cell, but found the message 'No Documentation found. Have you run the the cell?'

The simple code block is below:

#+BEGIN_SRC ipython :session :results output org drawer
import pandas as pd
import numpy as np

labels = ['a','b','c']
my_list = [10,20,30]
arr = np.array([10,20,30])
d = {'a':100,'b':45,'c':"Hololo"}

#basic series with automatic indexing
print(pd.Series(data = my_list))

#Adding a specific index.
ser1 = pd.Series(my_list,labels)

#I can use a normal list or a np array
print(pd.Series(arr,labels))

#+END_SRC

I'm starting to doubt if it's an insufficient configuration on my part and would like to clarify the same. I use the following:

(require 'scimax-org-babel-ipython-upstream)
(require 'ob-ipython)
(load "~/<path>/ox-ipynb.el")

Another question: Isn't Jedi supposed to do what I am looking for?

shrysr commented 6 years ago

I have updated to the latest commit. Still get the same error regarding the documentation not being found. I have taken care to add (add-to-list 'company-backends 'company-ob-ipython) to my init.

The new commit appears to have introduced a great deal of lag in my 8 year old macbook, when my my cursor is within a code block!! The above documentation error message is shown repeatedly. While I can manage with using an external reference or C-c ' , the lag will not let me work.

jkitchin commented 6 years ago

I have pushed another set of improvements today. With these you should be able to do the following:

  1. Inspect from an org-buffer. If you type M-x ob-ipython-inspect with your point on something in an ipython src block, it should open an inspect buffer with information about the thing at point. This only works if the kernel already knows about the thing at point, e.g. you have already imported the functions, or defined the objects and run the cell.

  2. eldoc like signatures should appear in the minibuffer when the point is at a place where a signature can be figured out. This might be causing things to be slow. By default, it is now turned off. you can turn it on with (scimax-ob-ipython-turn-on-eldoc) and off with (scimax-ob-ipython-turn-off-eldoc)

  3. You can turn on company-mode and get completion in org-buffers. The completion seems to work after a . I find it slow though, and tend not to use it.

you might need this for the setup

+BEGIN_SRC emacs-lisp

(add-to-list 'company-backends 'company-ob-ipython) (company-mode)

+END_SRC

  1. As an alternative to company completion, try scimax-ob-ipython-complete-ivy, which you can call on demand.

jedi is something separate from ob-ipython. I think it works

This is a little slow, but it seems to work ok.

jkitchin commented 6 years ago

commit f98f865 should speed up the eldoc and completion.

jkitchin commented 6 years ago

jedi is how elpy does completion, which I think is via some static analysis and communication with an rpc server that elpy sets up. This is different I think, here we are communicating with the kernel that ob-ipython is running instead, and getting documentation/completions from it.

shrysr commented 6 years ago

The attached screenshot is exactly what I would love to have, which is activated with the emacs-lisp mode. screen shot 2018-05-11 at 8 01 47 pm

shrysr commented 6 years ago

M-x ob-ipython-inspect in an org buffer within the source code block shown earlier gives me the message "Can't guess python-indent-offset, using defaults: 4". This message is repeated several times when I am within the buffer. The above is with scimax-ob-ipython-turn-on-eldoc.

Yes, company mode seems to work after a '.'.

I was receiving the mini-buffer summary if I C-c ' and edit the code, and this is only with the cursor at a () function. However, now the mini-buffer stays silent. Is it possible to set up something like the screenshot above when I C-c'?

I tried calling scimax-ob-ipython-complete-ivy : however, I am not clear on how it should be deployed? What kind of patterns can I search?

It appears that eldoc

screen shot 2018-05-11 at 8 30 02 pm

jkitchin commented 6 years ago

"Can't guess python-indent-offset, using defaults: 4". is an annoying warning. You can make it go away with:

(setq python-indent-guess-indent-offset nil) in your init file.

For scimax-ob-ipython-complete-ivy you should see something like this: screen shot 2018-05-11 at 5 37 12 pm

Your point has to be right after something that can be completed. In your example, it doesn't look like there is anything that could be completed.

For the signature, you should see something like this:

screen shot 2018-05-11 at 5 39 42 pm

It seems to work best in (point somewhere between arg parens )

For completion, what I see is:

screen shot 2018-05-11 at 5 47 24 pm

It isn't as fancy as the elisp version.

shrysr commented 6 years ago

Thank you for the tip on the indent setting and the explanation.

All the above works as you have described in a dedicated buffer with a major mode, or even when I use C-c'.

In an org buffer : I do not see the signature, though I've followed your example as it is. There are no messages after running the inspect command from within the org buffer.

Note: scimax-ob-ipython-complete-ivy does work in an org buffer as described.

shrysr commented 6 years ago

After using this somewhat intensively for awhile - the update is that I installed jedi as well. I'm satisfied with the autocomplete that is available when I use C-c'. However, the issue is that when I create a new ipython block - emacs hangs and there is lag (as it tries to auto-complete?). I have to hit C-g repeatedly for an exit and then the C-c' edit window opens up and it continues to hang as every character is typed. I have to git C-g repeatedly again. The above behavior stops the minute I execute that code block and then I can continue working. I have eldoc switched off with scimax-ob-ipython-turn-off-eldoc. The problem seems instigated everytime a new code block / org document is created.

One other problem being faced is that when I use separate code blocks and sometimes need to reference a variable from the previous code block (referring to the C-c' mode), this does not appear in the autocomplete options.

;; setting up jedi
(require 'jedi)
(setq jedi-config:use-system-python t)

(setq jedi:server-args
      '("--virtual-env" "/Users/shreyas/anaconda_install/anaconda3/envs/airflow-tutorial"
        "--virtual-env" "/Users/shreyas/anaconda_install/anaconda3/envs/mydashenvsr"))

(add-hook 'python-mode-hook 'jedi:setup)

(add-to-list 'load-path "~/.local/bin/")
methuselah-0 commented 3 years ago

I had the same issue, and have created a pull request for jedi.el here: https://github.com/tkf/emacs-jedi/pull/350/commits/2bc1055ea1e79e1b20562b7bb342c104d015785c

If you want to fix it directly, you can do the same edit as in the commit directly in ~/.emacs.d/elpa/jedi-core-something/jedi-core.el

then run M-x byte-compile-file RET ~/.emacs.d/elpa/jedi-core-something/jedi-core.el

then add the following in your init.el and restart emacs:

(add-hook 'org-mode-hook 'jedi:setup)

This gives me the normal auto-complete pop-up that includes the doc-string and function args when typing something in both an org-file src-block and in an Org Src buffer - provided that you reference the module functions with themodule.thefunction and not just thefunction - or you do it with import themodule as tm \<newline> tm.thefunction.