7

Can someone point me to a working setup for using babel with IPython?

I'm witnessing strange indentation behavior when I try to define functions. This happens whether or not I specify the --classic flag in org-babel-python-command. My current command (running linux with Emacs 24.4):

(setq org-babel-python-command 
      "ipython --pylab=qt4 --pdb --nosep --classic --no-banner --no-confirm-exit")

This doesn't work with function definitions.

#+BEGIN_SRC python :session *Broken IPython* :results output pp
  def g(x, lst):
      if isinstance(x, float):
          x = x + 0
      return [item + x for item in lst]
#+END_SRC

screenshot of my ipython buffer

This doesn't seem related to --classic either as I get the same error:

screenshot of my modern ipython buffer

Bonus points if you have a working setup that doesn't force me to use --classic as I do like the formatting of "modern" interface.

Update

Using the magic command lets me define functions and evaluate a session, but I can't get a value output without one. Instead, I get:

#+BEGIN_SRC python :results output
   def f(x):
       return x + 2

   f(2)
#+END_SRC

#+RESULTS:
: >>> ... ... >>> 4
: >>> 

#+BEGIN_SRC python :results value
   def f(x):
       return x + 2

   f(2)
#+END_SRC

#+RESULTS:

No results are returned, seemingly because of all the >>> in the output not being parsed correctly.

wdkrnls
  • 3,657
  • 2
  • 27
  • 46
  • The second block of your "Update" will never work since `:results value` requires you to modify your python code by either adding an explicit `return` statement or by assigning to a variable `foo` and adding `:return foo` to the header. But even with those fixes it won't work in ipython - see my second comment to my answer below. – deprecated Dec 02 '14 at 17:45

1 Answers1

2

The problem is that you cannot directly paste code into an ipython session. A post on the Org mailing list by Arun Persaud gives a workaround that employs the %cpaste magic command. Here is a slightly simplified version:

#+BEGIN_SRC emacs-lisp
; use %cpaste to paste code into ipython in org mode
(defadvice org-babel-python-evaluate-session
  (around org-python-use-cpaste
         (session body &optional result-type result-params) activate)
  "add a %cpaste and '--' to the body, so that ipython does the right thing."
  (setq body (concat "%cpaste -q \n" body "\n--"))
  ad-do-it
  )
#+END_SRC

After evaluating the above defadvice your example works for me. I have to ask though, what is the benefit of using ipython for the inferior shell? If you are using babel, then surely you won't be interacting with the shell directly.

deprecated
  • 2,775
  • 14
  • 15
  • I just realised that you are using pylab for interactive graphics, so I guess ipython does make sense for that. – deprecated Nov 26 '14 at 06:19
  • 1
    this fixes the example session, which is great, but it doesn't work without the session. See the updated question. – wdkrnls Dec 01 '14 at 18:48
  • @wdkmls Hmmm, but without the session then the motivation for using IPython seems even weaker. That would mean starting a separate IPython process for every code block evaluation, which is very slow. If you really do want to go down that route, then it would be necessary to wrap an advice around `org-babel-python-evaluate-external-process` to clean up the output. Do you really need this? – deprecated Dec 02 '14 at 17:39
  • Not right now, I suppose. I would like to eventually use python functions with the Emacs spreadsheet, but I suppose I can wrap the desired functionality up in shell scripts for now. – wdkrnls Dec 03 '14 at 19:13