Executing Python Code in IPython Kernel

偶尔善良 提交于 2019-12-24 23:44:35

问题


I want to duplicate ipython notebook capability in Emacs / Pymacs; and I need some direction for a simple code that can 1) send python / "magics" code to a ipython kernel 2) receive the display output, as a string. I found this comment by minrk, the "ipython kernel" example did not work, it gave "ImportError: No module named zmq.blockingkernelmanager".

I had better luck with one his other pointers, finally I landed at ipython-1.1.0/IPython/kernel/inprocess/tests/test_kernel.py, I ripped out a minimal part, and coded an Emacs extension called pytexipy-notebook. It's on Github

goo.gl/kQzJW1

If anyone knows of better examples, such as connecting to an existing (out of process), I'd like to hear about these.

Thanks in advance,


回答1:


Here is a sample for ipython 3.0.

from IPython.testing.globalipapp import get_ipython
from IPython.utils.io import capture_output
ip = get_ipython()

def run_cell(cmd):
    with capture_output() as io:
        res = ip.run_cell(content)
        print 'suc', res.success
        print 'res', res.result
    res_out = io.stdout
    print 'res out', res_out

content = "print (111+222)"
run_cell(content)
content = "alsdkjflajksf"
run_cell(content)

I will soon update

https://github.com/burakbayramli/emacs-ipython



来源:https://stackoverflow.com/questions/20301819/executing-python-code-in-ipython-kernel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!