gregsexton / ob-ipython

org-babel integration with Jupyter for evaluation of (Python by default) code blocks
739 stars 109 forks source link

Removing ob-ipython-auto-configure-kernels from org mode hook #161

Open alastairdb opened 6 years ago

alastairdb commented 6 years ago

I think it is a bit sneaky to modify a global variable like org-mode-hook when loading ob-ipython.el This will then get called in all org-mode buffers, even if they contain no ob-ipython src blocks, let alone execute them.

In addition, I think it is reasonable to use ob-ipython with a virtualenv and specify the virtualenv by setting a file local value for ob-ipython-command. This technique does not work when ob-ipython-auto-configure-kernels is called in the major mode hook, which is run before setting the file local variables. I suggest making the call to ob-ipython-auto-configure-kernels elsewhere. Here is a patch that does it in org-babel-execute:ipython:

diff --git a/ob-ipython.el b/ob-ipython.el
index d043434..fe522a1 100644
--- a/ob-ipython.el
+++ b/ob-ipython.el
@@ -493,7 +493,6 @@ a new kernel will be started."
 ;; babel framework

 (add-to-list 'org-src-lang-modes '("ipython" . python))
-(add-hook 'org-mode-hook 'ob-ipython-auto-configure-kernels)

 (defvar ob-ipython-configured-kernels nil)

@@ -569,6 +568,7 @@ have previously been configured."
 (defun org-babel-execute:ipython (body params)
   "Execute a block of IPython code with Babel.
 This function is called by `org-babel-execute-src-block'."
+  (ob-ipython-auto-configure-kernels)
   (ob-ipython--clear-output-buffer)
   (if (cdr (assoc :async params))
       (ob-ipython--execute-async body params)
vapniks commented 6 years ago

Also, when you have ob-ipython-command set to "ipython" then ob-ipython-auto-configure-kernels wont work since "ipython kernelspec list --json" emits warnings which json-read-from-string cant handle in ob-ipython--get-kernels. This means that every time you open an org file you get an error. Very annoying.