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

查看:22
本文介绍了转换为(不是来自)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.

一个可能的解决方案:可以通过导入一个 .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) 导入笔记本应用程序接口.(我不是不接受当前的答案,因为它解决了我当时的问题,并指出了我在自我回答中使用的资源.)

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

此答案最初是为 IPyhton 3 编写的.我不知道如何使用 IPython 4 正确执行此操作.这是上面链接的更新版本,指向nbpy.py 来自 IPython 3.2.1 版本:

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

基本上,您使用诸如 # # 之类的特殊注释来分隔各个单元格.查看 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天全站免登陆