Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
83 lines (55 sloc) 2.14 KB
#!/usr/bin/env python
"""Executes expression on kernel using ZMQ messaging. Requires the
security file to start connection to an exisiting kernel (in
How to run:
1. Run ipython kernel. Kernel is started automatically when running
notebook or qtconsole.
For example:
> ipython qtconsole
2. Copy the name of kernel security file.
The name of this file is printed to stdout when running ipython.
For example, the previous command should have printed:
[IPKernelApp] To connect another client to this kernel, use:
[IPKernelApp] --existing kernel-52706.json
kernel-XXXX.json is what we are looking for (the name may slightly
differ for notebook interface).
3. Run this script with the security file as an argument.
The security file is normally located in the
$HOME/.ipython/profile_default/secruity directory. Therefore you
should run this script via:
> python $HOME/.ipython/profile_default/secruity/kernel-XXXX.json
Where kernel-XXXX.json is the file name of the previous step.
4. Check whether the expression was executed in the ipyhton fronted
(see `expr` variable below).
In [1]: print my_funny_variable
Out[1]: 1
Messaging in IPython:
expr = "my_funny_variable = 1"
import json
from IPython.zmq import session
import zmq
import sys
_, kernel_conf = sys.argv
with open(kernel_conf) as f:
kernel_data = json.load(f)
print kernel_data
key = kernel_data['key']
url = "tcp://{ip}:{shell_port}".format(**kernel_data)
zmq.DEALER = zmq.XREQ
c = zmq.Context()
request_socket = c.socket(zmq.DEALER)
sess = session.Session(key=key)
msg_content = dict(code=expr,
sess.send(request_socket, 'execute_request',msg_content)
Something went wrong with that request. Please try again.