IPython Notebook Javascript:从JavaScript变量中检索内容 [英] IPython Notebook Javascript: retrieve content from JavaScript variables

查看:90
本文介绍了IPython Notebook Javascript:从JavaScript变量中检索内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有办法让函数(由IPython Notebook单元调用)检索JavaScript变量的内容(例如 IPython.notebook.notebook_path 哪个包含当前笔记本的路径??

Is there a way for a function (called by an IPython Notebook cell) to retrieve the content of a JavaScript variable (for example IPython.notebook.notebook_path which contains the path of the current notebook)?

以下在单元格中直接编写时效果很好(例如,基于这个问题及其评论):

The following works well when written directly within a cell (for example, based on this question and its comments):

from IPython.display import display,Javascript
Javascript('IPython.notebook.kernel.execute("mypath = " + "\'"+IPython.notebook.notebook_path+"\'");')

但如果我尝试将其放入函数中,那就会崩溃:

But that falls apart if I try to put it in a function:

# this doesn't work
from IPython.display import display,Javascript
def getname():
    my_js = """
    IPython.notebook.kernel.execute("mypath = " + "\'"+IPython.notebook.notebook_path+"\'");
    """
    Javascript(my_js)
    return mypath

(是的,我试图让 global mypath 变量,都来自 my_js 脚本和从功能内部。另请注意:不要被先前命令中变量中可能的剩余值所欺骗;确保使用 mypath = None; del mypath 在调用函数之前重置变量,或重新启动内核。)

(And yes, I've tried to make global the mypath variable, both from within the my_js script and from within the function. Also note: don't be fooled by possible leftover values in variables from previous commands; to make sure, use mypath = None; del mypath to reset the variable before calling the function, or restart the kernel.)

制定问题的另一种方法是:什么是由设置的变量的范围(时间和地点)IPython.notebook.kernel.execute()

Another way to formulate the question is: "what's the scope (time and place) of a variable set by IPython.notebook.kernel.execute()"?

我认为这不是一个无关紧要的问题,可能与IPython用来控制其内核及其变量的机制有关,而且我对此并不了解。以下实验说明了该机制的某些方面。以下工作在两个单独的单元格中完成,但如果两个单元格合并则不起作用:

I think it isn't an innocuous question, and is probably related to the mechanism that IPython uses to control its kernels and their variables and that I don't know much about. The following experiment illustrate some aspect of that mechanism. The following works when done in two separate cells, but doesn't work if the two cells are merged:

单元格[1]

my_out = None
del my_out
my_js = """
IPython.notebook.kernel.execute("my_out = 'hello world'");
"""
Javascript(my_js)

单元格[2]

print(my_out)

这可以生成预期的 hello world 。但是如果合并两个单元格,它就不起作用( NameError:name'my_out'未定义)。

This works and produces the expected hello world. But if you merge the two cells, it doesn't work (NameError: name 'my_out' is not defined).

推荐答案

我认为这个问题与Javascript是asynchronus有关,而python则没有。通常,您会认为执行了Javascript(python cmd)命令,然后您的打印声明应该按预期正常工作。但是,Javascript命令被触发但未执行。最可能的是它在单元格1执行完全完成后执行。

I think the problem is related with Javascript being asynchronus while python is not. Normally you would think that the Javascript(""" python cmd """) command is executed, and then your print statment should work properly as expected. However, the Javascript command is fired but not executed. Most pobably it is executed after the cell 1 execution is fully completed.

我尝试了睡眠功能的例子。没有帮助。

I tried your example with sleep function. Did not help.

通过在my_js中添加alert语句,但在kernel.execute行之前,可以看到asnyc问题。即使在尝试执行python命令之前,也应该触发警报。

The asnyc problem can esaily be seen by adding an alert statement within my_js, but before kernel.execute line. The alert should be fired even before trying a python command execution.

但是当单元格1中存在print(my_out)语句时,您将再次收到相同的错误而没有任何警报。如果你取出打印线,你会看到单元格1中弹出警告。但是后来设置了变量my_out。

But at the presence of print (my_out) statement within cell 1, you will again get the same error without any alerts. If you take the print line out, you will see the alert poping out within cell 1. But the varibale my_out is set afterwards.

my_out = None
del my_out
my_js = """
**alert ("about to execute python comand");**
IPython.notebook.kernel.execute("my_out = 'hello world'");
"""
Javascript(my_js)

笔记本中还有其他javascript实用程序,如IPython.display.display_xxx,从显示视频到文本对象不等,但即使文本对象选项也不起作用。

There are other javascript utilities within notebook like IPython.display.display_xxx which varies from displaying video to text object, but even the text object option does not work.

有趣的是,我用我的webgl canvas应用程序对它进行了测试,该应用程序在HTML5画布上显示对象; display.display_javascript(javascript object)工作正常(这是一个looong html5文档),而输出的两个单词没有出现?!也许我应该将输出嵌入到某个地方的canvas应用程序中,所以它显示在画布上:)

Funny enough, I tested this with my webgl canvas application which displays objects on the HTML5 canvas; display.display_javascript(javascript object) works fine ( which is a looong html5 document) while the two pieces of words of output does not show up?! Maybe I should embed the output into canvas application somewhere, so it s displayed on the canvas :)

这篇关于IPython Notebook Javascript:从JavaScript变量中检索内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆