如何通过命令行将IPython Notebook转换为Python文件?


258

我正在寻找使用* .ipynb文件作为事实来源,并以编程方式将其“编译”为计划作业/任务的.py文件。

我了解的唯一方法是通过GUI。有办法通过命令行吗?


1
您所说的“真理之源”是什么意思?IPython笔记本只是json文件。您可以加载它们并作为Python字典进行操作。对于源代码,您应该inputcell_type等于“代码”的地方迭代键。看看这个方案
theta

1
好吧,我想将.ipynb存储在存储库中,而不是.py文件中。因此,作为“构建步骤”,我将把.ipynb转换为.py文件,以供自动化系统实际使用。没错,我可以只加载json并仅输出代码单元,但是我想知道是否已经有东西对我
有用

1
@StefanKrawczyk您能否将一个请求者标记为已接受?我会推荐wwwilliam的礼物
pedram bashiri

Answers:


412

如果不想在每次保存时都输出Python脚本,或者不想重新启动IPython内核:

命令行上,您可以使用nbconvert

$ jupyter nbconvert --to script [YOUR_NOTEBOOK].ipynb

有点技巧,您甚至可以通过预先挂起!(用于任何命令行参数) IPython笔记本中调用上述命令。在笔记本内:

!jupyter nbconvert --to script config_template.ipynb

之前--to script加入,可以选择是--to python--to=python,但它改名朝一个语言无关的笔记本电脑系统的举动。


8
如果您确实想每次保存一次,则jupyter可以nbconvert通过保存前或保存后钩子ContentsManager.pre_save_hookabd 触发FileContentsManager.post_save_hook。您将添加一个保存后的钩子jupyter nbconvert --to script [notebook]
jaimedash '16

3
有没有一种方法可以做到相反,即从python脚本转换为笔记本。是否曾经有一些专门的文档字符串被解析为单元格?
Sujen Shah

3
转换文件夹中的所有笔记本jupyter nbconvert --to script /path/to/notebooks/*.ipynb
openwonk

8
谢谢,它有效!但是,如果我不想# In[ ]:在脚本中输入类型,我希望它是干净的。有什么办法吗?
Rishabh Agrahari '18

1
@RishabhAgrahari在这里签出,您可以仅定制短绒布jupyter-notebook.readthedocs.io/en/stable/extending/…–
MichaelChirico


19

这是一种无需使用ipython即可从V3或V4 ipynb中提取代码的快捷方法。它不检查单元格类型等。

import sys,json

f = open(sys.argv[1], 'r') #input.ipynb
j = json.load(f)
of = open(sys.argv[2], 'w') #output.py
if j["nbformat"] >=4:
        for i,cell in enumerate(j["cells"]):
                of.write("#cell "+str(i)+"\n")
                for line in cell["source"]:
                        of.write(line)
                of.write('\n\n')
else:
        for i,cell in enumerate(j["worksheets"][0]["cells"]):
                of.write("#cell "+str(i)+"\n")
                for line in cell["input"]:
                        of.write(line)
                of.write('\n\n')

of.close()

1
如果您不想安装任何Jupyter工具,则为最佳答案。
dacracot

1
我喜欢这个。但是我发现当我从Jupyter笔记本下载.py格式时,即使我在Windows上,它也使用UNIX行尾。要生成相同的内容,请newlines='\n'在打开的输出文件调用中将as作为第三个参数添加。(Python 3.x)
RufusVS

16

遵循前面的示例,但具有新的nbformat lib版本

import nbformat
from nbconvert import PythonExporter

def convertNotebook(notebookPath, modulePath):

  with open(notebookPath) as fh:
    nb = nbformat.reads(fh.read(), nbformat.NO_CONVERT)

  exporter = PythonExporter()
  source, meta = exporter.from_notebook_node(nb)

  with open(modulePath, 'w+') as fh:
    fh.writelines(source.encode('utf-8'))

Fh.writelines(source)的最后一行代码fh.writelines(source.encode('utf-8'))给出了“ TypeError:write()参数必须为str,而不是int”。
BarryC

6

您可以从IPython API执行此操作。

from IPython.nbformat import current as nbformat
from IPython.nbconvert import PythonExporter

filepath = 'path/to/my_notebook.ipynb'
export_path = 'path/to/my_notebook.py'

with open(filepath) as fh:
    nb = nbformat.reads_json(fh.read())

exporter = PythonExporter()

# source is a tuple of python source code
# meta contains metadata
source, meta = exporter.from_notebook_node(nb)

with open(export_path, 'w+') as fh:
    fh.writelines(source)

4

Jupytext非常适合在您的工具链中进行此类转换。它不仅允许从笔记本转换为脚本,而且还可以再次从脚本转换为笔记本。甚至还有以执行形式生产的笔记本。

jupytext --to py notebook.ipynb                 # convert notebook.ipynb to a .py file
jupytext --to notebook notebook.py              # convert notebook.py to an .ipynb file with no outputs
jupytext --to notebook --execute notebook.py    # convert notebook.py to an .ipynb file and run it 

显然还有ipynb-py-convert,请参阅此处
韦恩

'jupytext'不被识别为内部或外部命令,可操作程序或批处理文件。
阿米·查迪

您是否已安装@AmineChadi。有关如何执行此操作,请参见此处。如果通过笔记本使用它作为命令行界面,则可以%pip install jupytext在笔记本中运行。
韦恩

3

要将当前目录中的所有* .ipynb格式文件递归转换为python脚本:

for i in *.ipynb **/*.ipynb; do 
    echo "$i"
    jupyter nbconvert  "$i" "$i"
done

3
我必须添加--to script参数以避免Jupiter 4.4.0中的默认HTML输出。
trojjer

0

我遇到了这个问题,并试图在线找到解决方案。尽管我找到了一些解决方案,但是它们仍然存在一些问题,例如,Untitled.txt当您从仪表板启动新笔记本时,恼人的自动创建。

所以最终我写了自己的解决方案

import io
import os
import re
from nbconvert.exporters.script import ScriptExporter
from notebook.utils import to_api_path


def script_post_save(model, os_path, contents_manager, **kwargs):
    """Save a copy of notebook to the corresponding language source script.

    For example, when you save a `foo.ipynb` file, a corresponding `foo.py`
    python script will also be saved in the same directory.

    However, existing config files I found online (including the one written in
    the official documentation), will also create an `Untitile.txt` file when
    you create a new notebook, even if you have not pressed the "save" button.
    This is annoying because we usually will rename the notebook with a more
    meaningful name later, and now we have to rename the generated script file,
    too!

    Therefore we make a change here to filter out the newly created notebooks
    by checking their names. For a notebook which has not been given a name,
    i.e., its name is `Untitled.*`, the corresponding source script will not be
    saved. Note that the behavior also applies even if you manually save an
    "Untitled" notebook. The rationale is that we usually do not want to save
    scripts with the useless "Untitled" names.
    """
    # only process for notebooks
    if model["type"] != "notebook":
        return

    script_exporter = ScriptExporter(parent=contents_manager)
    base, __ = os.path.splitext(os_path)

    # do nothing if the notebook name ends with `Untitled[0-9]*`
    regex = re.compile(r"Untitled[0-9]*$")
    if regex.search(base):
        return

    script, resources = script_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')

    log = contents_manager.log
    log.info("Saving script at /%s",
             to_api_path(script_fname, contents_manager.root_dir))

    with io.open(script_fname, "w", encoding="utf-8") as f:
        f.write(script)

c.FileContentsManager.post_save_hook = script_post_save

要使用此脚本,您可以将其添加到~/.jupyter/jupyter_notebook_config.py:)

请注意,您可能需要重新启动jupyter笔记本/实验室才能正常工作。


0

有一个非常不错的软件包nb_dev,用于在Jupyter Notebooks中编写Python软件包。像nbconvert,它可以将笔记本变成.py文件一样,但它更灵活,功能更强大,因为它具有许多不错的附加创作功能,可帮助您在PyPI上开发测试,文档和注册程序包。它是由fast.ai人开发的。

它有一些学习曲线,但是文档很好,总体上也不难。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.