Skip to content

Latest commit

 

History

History
216 lines (168 loc) · 7.24 KB

ipython-babel.org

File metadata and controls

216 lines (168 loc) · 7.24 KB

ipython-babel

1 Version information

(princ (concat
        (format "Emacs version: %s\n"
                (emacs-version))
        (format "org version: %s\n"
                (org-version))
        ;; (format "ob-ipython version: %s\n"
        ;; (pkg-info-version-info 'ob-ipython))
        ))        
Emacs version: GNU Emacs 25.0.95.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.10.8)
 of 2016-06-12
org version: 8.3.4

The examples also use the ob-ipython package from MELPA

2 Session setup

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

print "Finished imports"
Finished imports

3 Results as value and as output

def foo(x):
    return x + 9

[foo(x) + 7 for x in range(7)]
[16, 17, 18, 19, 20, 21, 22]
print "this is an echo"

4 Producing a graph

plt.hist(np.random.randn(20000), bins=200)

ipython-babel_att/image.png

5 Opening another session

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

print "Finished imports for session ip2"

6 Examples using ipython magic commands

%timeit 1+1
%%timeit
lst = range(1,1000)
while len(lst) > 0:
    lst.pop()

7 Troubleshooting

7.1 Some tips

  • M-x ob-ipython-interrupt-kernel: interrupt a busy kernel
  • M-x ob-ipython-kill-kernel: kill the kernel process. A new one will be restarted upon the next src block evaluation.

7.2 Race condition due to security cookie not yet available

I got an error message in ob-ipython-debug

HTTP/1.1 500 Internal Server Error
Date: Sun, 27 Dec 2015 13:35:32 GMT
Content-Length: 93
Content-Type: text/html; charset=UTF-8
Server: TornadoServer/4.2

<html><title>500: Internal Server Error</title><body>500: Internal Server Error</body></html>

Further investigation in the buffer ob-ipython-ob-ipython-driver associated with the ipython driver process.

ERROR:tornado.application:Uncaught exception POST /execute/ip2 (127.0.0.1)
HTTPServerRequest(protocol='http', host='localhost:9988', method='POST', uri='/execute/ip2', version='HTTP/1.1', remote_ip='127.0.0.1', headers={'Content-Length': '111', 'Accept-Encoding': 'gzip', 'Extension': 'Security/Digest Security/SSL', 'Connection': 'keep-alive', 'Accept': '*/*', 'User-Agent': 'URL/Emacs', 'Host': 'localhost:9988', 'Mime-Version': '1.0'})
Traceback (most recent call last):
  File "/opt/anaconda/python2.7/lib/python2.7/site-packages/tornado/web.py", line 1369, in _stack_context_handle_exception
    raise_exc_info((type, value, traceback))
  File "/opt/anaconda/python2.7/lib/python2.7/site-packages/tornado/web.py", line 1572, in wrapper
    result = method(self, *args, **kwargs)
  File "/home/dfeich/.emacs.d/elpa/ob-ipython-20151010.307/driver.py", line 75, in post
    c = get_client(name)
  File "/home/dfeich/.emacs.d/elpa/ob-ipython-20151010.307/driver.py", line 58, in get_client
    clients[name] = create_client(name)
  File "/home/dfeich/.emacs.d/elpa/ob-ipython-20151010.307/driver.py", line 46, in create_client
    cf = find_connection_file('emacs-' + name)
  File "/opt/anaconda/python2.7/lib/python2.7/site-packages/IPython/kernel/connect.py", line 224, in find_connection_file
    raise IOError("Could not find %r in %r" % (filename, security_dir))
IOError: Could not find u'emacs-ip2' in u'/home/dfeich/.ipython/profile_default/security'
ERROR:tornado.access:500 POST /execute/ip2 (127.0.0.1) 9.24ms

The file exists. Possibly a race condition?

ls -l /home/dfeich/.ipython/profile_default/security/emacs-ip2*

7.3 server fails to start due to port taken

This happens when shutting down and restarting emacs too fast. The socket is still not freed. In the kernel buffer one sees

...
/opt/anaconda/python2.7/lib/python2.7/site-packages/zmq/backend/cython/checkrc.pxd in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:7055)()
     23         else:
     24             from zmq.error import ZMQError
---> 25             raise ZMQError(errno)
        global ZMQError = <class 'zmq.error.ZMQError'>
        global errno = undefined
     26     return 0

ZMQError: Address already in use

Process kernel-ip1 exited abnormally with code 1
   

8 Older method for running ipython (mostly obsolete)

This chapter described workarounds to execute ipython as the python shell using the standard ob-python babel implementation before I started using ob-ipython.

ipython babel works best for testing interactive magic commands, but there are problems when code blocks are copied into the babel session (in ipython these snippets need to get pasted using %paste or %cpaste).

there is a discussion and a defadvice in this discussion http://emacs.stackexchange.com/questions/3859/working-setup-for-ipython-in-babel

; 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
  )