Javascript Jupyter Notebook如何获取代码单元格内容? [英] Javascript Jupyter Notebook How to get code cell content?

查看:589
本文介绍了Javascript Jupyter Notebook如何获取代码单元格内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



正如我们在这里看到的,源码的每个字符代码在它自己的元素中。当然,我并不是非常希望从标签中提取所有内容并再次连接它以获取代码单元的代码。有没有简单的方法来获取单元格的源代码?



也许有些Jupyter JS API函数? (笔记本本身如何实际获取它发送给内核的代码?应该有一些东西已经完成这项工作。)或者一些jQuery代码,它非常聪明地获取所有内容?



我能想到的一个替代方案是在内核在后端评估它之前以某种方式拦截代码单元格内容,但我还不知道如何做到这一点,它需要输出HTML ,它包含JS,在运行时在代码单元的输出中具有立即执行的函数。如果我希望实际代码的输出代码单元输出中的代码分析输出,这也可能变得复杂。也许它不像我想的那么复杂,但到目前为止,在JS方面获取代码似乎更好,如果有一些简单的方法来获取它...

解决方案

查看Juypter cell.js 文件这里 - 它列出了Jupyter本身用于与单元格交互的函数。特别是,函数 get_text()将返回单元格的内容。 (这适用于所有类型的单元格 - 只查看 codecell.js 文件,以获取特定于代码单元格的函数)



在浏览器中测试这个的快速方法 - 访问全局 Jupyter 对象的笔记本实例以获取笔记本的第一个单元格:



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



现在我们有了单元格 - 我们可以读取其中的代码!使用:



var code = cell.get_text();


(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.)

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 divs (probably half of them redundant):

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? (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?

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. 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...

解决方案

Take a look at the Juypter cell.js file here - it lists the functions Jupyter itself uses to interact with cells. In particular, the function get_text() will return the content of a cell. (this works on all types of cells - take a look at the codecell.js file for functions specific to code cells only)

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:

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();

这篇关于Javascript Jupyter Notebook如何获取代码单元格内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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