转换为(而不是)ipython Notebook格式 [英] Converting to (not from) ipython Notebook format

查看:112
本文介绍了转换为(而不是)ipython Notebook格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IPython Notebook附带 nbconvert ,可以笔记本导出为其他格式。但是如何以相反的方向转换文本?我问,因为我已经拥有不同格式的材料和良好的工作流程,但我想利用Notebook的互动环境。

IPython Notebook comes with nbconvert, which can export notebooks to other formats. But how do I convert text in the opposite direction? I ask because I already have materials, and a good workflow, in a different format, but I would like to take advantage of Notebook's interactive environment.

一个可能的解决方案:A可以通过导入 .py 文件来创建笔记本,并且文档说明当 nbconvert 将笔记本导出为python时脚本,它在可用于重新创建笔记本的注释中嵌入指令。但该信息附带免责声明这种方法的局限性,并且在我能找到的任何地方都没有记录可接受的格式。 (奇怪的是,在描述笔记本 JSON格式)。任何人都可以提供更多信息或更好的选择吗?

A likely solution: A notebook can be created by importing a .py file, and the documentation states that when nbconvert exports a notebook as a python script, it embeds directives in comments that can be used to recreate the notebook. But the information comes with a disclaimer about the limitations of this method, and the accepted format is not documented anywhere that I could find. (A sample is shown, oddly enough, in the section describing notebook's JSON format). Can anyone provide more information, or a better alternative?

编辑(2016年3月1日):接受的答案不再有效,因为对于某些人来说因为Notebook API的第4版不支持此输入格式。 我添加了自我答案 ,展示如何使用当前导入笔记本电脑(v4) API。 (我不是不接受当前的答案,因为它解决了我当时的问题并指出了我在自我回答中使用的资源。)

Edit (1 March 2016): The accepted answer no longer works, because for some reason this input format is not supported by version 4 of the Notebook API. I have added a self-answer showing how to import a notebook with the current (v4) API. (I am not un-accepting the current answer, since it solved my problem at the time and pointed me to the resources I used in my self-answer.)

推荐答案

以下适用于IPython 3,但不适用于IPython 4.

The following works for IPython 3, but not IPython 4.

IPython API具有读写笔记本文件的功能。您应该使用此API而不是直接创建JSON。例如,以下代码段将脚本 test.py 转换为笔记本 test.ipynb

The IPython API has functions for reading and writing notebook files. You should use this API and not create JSON directly. For example, the following code snippet converts a script test.py into a notebook test.ipynb.

import IPython.nbformat.current as nbf
nb = nbf.read(open('test.py', 'r'), 'py')
nbf.write(nb, open('test.ipynb', 'w'), 'ipynb')

关于nbf.read理解的.py文件格式,最好只查看解析器类IPython.nbformat.v3.nbpy.PyReader。代码可以在这里找到(它不是很大):

Regarding the format of the .py file understood by nbf.read it is best to simply look into the parser class IPython.nbformat.v3.nbpy.PyReader. The code can be found here (it is not very large):

https://github.com/ipython/ipython/blob/master/jupyter_nbformat/v3/nbpy.py

https://github.com/ipython/ipython/blob/master/jupyter_nbformat/v3/nbpy.py

编辑:这个答案最初是为IPyhton 3编写的。我不知道如何正确使用IPython 4.这是一个更新的上面链接的版本,指向IPython 3.2.1版本中 nbpy.py 的版本:

This answer was originally written for IPyhton 3. I don't know how to do this properly with IPython 4. Here is an updated version of the link above, pointing to the version of nbpy.py from the IPython 3.2.1 release:

https://github.com/ ipython / ipython / blob / rel-3.2.1 / IPython / nbformat / v3 / nbpy.py

基本上你使用特殊评论,例如#< codecell> #< markdowncell> 分隔各个单元格。查看 PyReader.to_notebook line.startswith 语句的完整列表。

Basically you use special comments such as # <codecell> or # <markdowncell> to separate the individual cells. Look at the line.startswith statements in PyReader.to_notebook for a complete list.

这篇关于转换为(而不是)ipython Notebook格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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