简体   繁体   English

Javascript Jupyter Notebook 如何获取代码单元格内容?

[英]Javascript Jupyter Notebook How to get code cell content?

(There is a similar question here , but it is about using python code to read markdown cells. I want to use JavaScript (for example in a Jupyter Notebook front end extension) to read the source code in code cells.) (有一个类似的问题在这里,但它是关于使用Python代码阅读降价细胞。我想使用JavaScript(例如在Jupyter笔记本前端延伸)读取码单元的源代码。)

I want to perform an analysis on the code.我想对代码进行分析。 However, if I simply inspect the DOM of a Jupyter Notebook, it turns out to be a true DOM nightmare of nested div s (probably half of them redundant):但是,如果我简单地检查 Jupyter Notebook 的 DOM,结果是嵌套div的真正 DOM 噩梦(可能其中一半是多余的):

在此处输入图片说明

As we can see here, each character of the source code is in its own element.正如我们在这里看到的,源代码的每个字符都在它自己的元素中。 Naturally I am not very keen to pull all that stuff out of its tags and concat it again just to get the code of a code cell.自然地,我不是很热衷于将所有这些东西从它的标签中提取出来并再次连接它来获取代码单元的代码。 Is there some easy way to get the source code of a cell?有没有一些简单的方法来获取单元格的源代码?

Maybe some Jupyter JS API function?也许一些 Jupyter JS API 函数? (How does the notebook itself actually get the code it sends to the kernel? There should be something already doing this job.) Or some little piece of jQuery code, which is very clever about getting all the contents? (笔记本本身如何实际获取它发送到内核的代码?应该有一些东西已经在做这项工作。)或者一些jQuery 代码,它非常聪明地获取所有内容?

One alternative I can think of is to somehow intercept the code cells content before the kernel evaluates it in the backend, but I also don't know how to do that yet and it would require outputting HTML, which contains JS with an immediately executed function in the output of the code cell, when it is run.我能想到的一种替代方法是在内核在后端对其进行评估之前以某种方式拦截代码单元格内容,但我也不知道如何做到这一点,它需要输出 HTML,其中包含具有立即执行功能的 JS在代码单元的输出中,当它运行时。 This could also get complicated, if I want the actual code's output and the output of the code analysis in the output of the code cell.如果我想要实际代码的输出代码单元输出中的代码分析的输出,这也可能会变得复杂。 Maybe it is less complicated than I think, but so far it seems better to grab the code on the JS side, if there was some easy way to get it...也许它没有我想象的那么复杂,但到目前为止,如果有一些简单的方法来获取它,那么在 JS 端获取代码似乎更好......

Take a look at the Juypter cell.js file here - it lists the functions Jupyter itself uses to interact with cells.此处查看 Juypter cell.js文件 - 它列出了 Jupyter 本身用于与单元格交互的函数。 In particular, the function get_text() will return the content of a cell.特别是,函数get_text()将返回单元格的内容。 (this works on all types of cells - take a look at the codecell.js file for functions specific to code cells only) (这适用于所有类型的单元格 - 查看codecell.js文件以了解仅特定于代码单元格的功能)

A quick way to test this out in the browser - access the global Jupyter object's notebook instance to get the first cell of the notebook:在浏览器中进行测试的一种快速方法 - 访问全局Jupyter对象的notebook实例以获取notebook的第一个单元格:

var cell = Jupyter.notebook.get_cell(0);

Now that we have the cell - we can read the code within it!现在我们有了单元格 - 我们可以读取其中的代码! Use:用:

var code = cell.get_text();

EDIT: you can save this result to a Python variable using the Javascript function to execute Javascript in a Python cell and IPython.notebook.kernel.execute to execute Python in Javascript - this is used to set the variable with the code name.编辑:您可以使用Javascript函数将此结果保存到 Python 变量中以在 Python 单元格中执行 Javascript,并使用IPython.notebook.kernel.execute以在 Javascript 中执行 Python - 这用于设置具有代码名称的变量。 Note you have to escape characters like quotes to I use encodeURI to perform URI encoding/decoding.请注意,您必须将引号之类的字符转义为我使用 encodeURI 来执行 URI 编码/解码。

from IPython.display import Javascript
import urllib.parse

Javascript("const code = Jupyter.notebook.get_cell(0).get_text(); IPython.notebook.kernel.execute(`myvar = '${encodeURI(code)}'`);")
urllib.parse.unquote(myvar)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM