按照博客中所说,javascript可以与ipython 实现双向通信,但是其给出的代码运行后,js中定义变量可以实现,但是ipython的kernel 返回无法实现。有同学们在研究这个吗?
http://www.tuicool.com/articles/naqIza
from IPython.display import HTML
from math import pi, sin
#
input_form = """
Code:
Result:
Execute
Result:
Execute
"""
javascript = """
function handle_output(out_type, out){ console.log(out_type); console.log(out); var res = null; // if output is a print statement if(out_type == "stream"){ res = out.data; } // if output is a python object else if(out_type === "pyout"){ res = out.data["text/plain"]; } // if output is a python error else if(out_type == "pyerr"){ res = out.ename + ": " + out.evalue; } // if output is something we haven't thought of else{ res = "[out type not implemented]"; } document.getElementById("result_output").value = res; } function exec_code(){ var code_input = document.getElementById('code_input').value; var kernel = IPython.notebook.kernel; var callbacks = {'output' : handle_output}; document.getElementById("result_output").value = ""; // clear output box var msg_id = kernel.execute(code_input, callbacks, {silent:false}); console.log("button pressed"); }"""
HTML(input_form + javascript)