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

时间:2013-06-13 00:23:02

标签: ipython ipython-notebook

我正在考虑使用 * .ipynb 文件作为事实的来源,并以编程方式将它们“编译”为.py文件以用于预定的作业/任务。

我理解的唯一方法是通过GUI。有没有办法通过命令行来实现?

14 个答案:

答案 0 :(得分:297)

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

命令行上,您可以使用nbconvert

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

作为一个黑客,您甚至可以通过预先挂起的!在 IPython笔记本中调用上述命令(用于任何命令行)参数)。在笔记本内:

!jupyter nbconvert --to script config_template.ipynb

--to script added之前,选项为--to python--to=python,但在向语言无关的笔记本系统转移时renamed

答案 1 :(得分:32)

如果要将所有*.ipynb文件从当前目录转换为python脚本,可以运行如下命令:

jupyter nbconvert --to script *.ipynb

答案 2 :(得分:13)

这是一种快速而肮脏的方法,可以在不使用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()

答案 3 :(得分:11)

按照上一个示例,但使用新的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'))

答案 4 :(得分:6)

@ Spawnrider的最后一行代码,

fh.writelines(source.encode('utf-8'))

给出' TypeError:write()参数必须是str,而不是int'

fh.writelines(source) 

虽然有效。

答案 5 :(得分:5)

您可以从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)

答案 6 :(得分: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 

答案 7 :(得分:3)

我知道这是一个旧线程。我遇到了同样的问题,想通过命令行将 .pynb 文件转换为 .py 文件。

我的搜索将我带到了 ipynb-py-convert

按照以下步骤,我能够获得 .py 文件

  1. 安装“pip install ipynb-py-convert”
  2. 通过命令提示符进入保存ipynb文件的目录
  3. 输入命令

> ipynb-py-convert YourFileName.ipynb YourFilename.py

例如:。 ipynb-py-convert 开始使用-kaggle-titanic-problem.ipynb 开始使用-kaggle-titanic-problem.py

以上命令将创建一个名为“YourFileName.py”的python脚本,根据我们的示例,它将创建getting-started-with-kaggle-titanic-problem.py文件

答案 8 :(得分:2)

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

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

答案 9 :(得分:1)

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

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

答案 10 :(得分:0)

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

所以最终我写了my own solution

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笔记本/实验室才能使其正常工作。

答案 11 :(得分:0)

以下示例将一个名为a_notebook.ipynb的Iron Python Notebook转换为一个名为a_python_script.py的python脚本,而忽略了标记有关键字remove的单元格,我将它们手动添加到了我所创建的单元格中不想以脚本结尾,省去了可视化和其他步骤,一旦我完成了笔记本操作,就不需要脚本执行。

import nbformat as nbf
from nbconvert.exporters import PythonExporter
from nbconvert.preprocessors import TagRemovePreprocessor

with open("a_notebook.ipynb", 'r', encoding='utf-8') as f:
    the_notebook_nodes = nbf.read(f, as_version = 4)

trp = TagRemovePreprocessor()

trp.remove_cell_tags = ("remove",)

pexp = PythonExporter()

pexp.register_preprocessor(trp, enabled= True)

the_python_script, meta = pexp.from_notebook_node(the_notebook_nodes)

with open("a_python_script.py", 'w') as f:
    f.writelines(the_python_script)

答案 12 :(得分:0)

在我工作的 mint [ubuntu] 系统上,即使 jupyter 已经安装并且笔记本工作,jupyter nbconvert --to script 仍然给出错误没有文件/目录,直到我单独做了一个

sudo apt-get install jupyter-nbconvert

然后转换一切正常。我只是想添加这个以防万一有人遇到同样的错误(对我来说这很令人困惑,因为我认为没有文件错误指的是笔记本,它肯定存在于本地目录中,我花了一段时间才意识到子命令不是已安装)。

答案 13 :(得分:0)

%notebook foo.ipynb 魔术命令会将当前的 IPython 导出到“foo.ipynb”。

通过输入 %notebook?

获得更多信息