我正在考虑使用*。Ipynb文件作为真相的来源,并以编程方式将它们“编译”为.py文件,用于计划的作业/任务。

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


当前回答

如果你想转换所有*。Ipynb文件从当前目录转移到python脚本,可以运行如下命令:

jupyter nbconvert --to script *.ipynb

其他回答

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

在命令行中,你可以使用nbconvert:

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

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

!jupyter nbconvert --to script config_template.ipynb

在添加——to脚本之前,选项是——to python或——to=python,但在转向语言无关的笔记本系统时,它被重命名了。

我发现有两种方法可以用命令行将Jupyter Notebook转换为普通的Python脚本。下面是Jupyter笔记本的示例和两个工具的输出。

1. 使用nbconvert

nbconvert是在Jupyter Notebook用户界面下载中作为功能使用的工具。它可以用作命令行工具:

jupyter nbconvert --to python notebook.ipynb

Python脚本示例:

2. 使用jupytext

jupytext是一个保持.ipynb文件与.py文件同步的包。它还可以用于在命令行中转换.ipynb文件。它支持几种类型的转换:

转换为轻格式的Python脚本

jupytext --to py notebook.ipynb             

python脚本示例:

转换为百分比格式的Python脚本

jupytext --to py:percent notebook.ipynb 

Python脚本示例:

我遇到了这个问题,并试图在网上找到解决方案。虽然我找到了一些解决方案,但它们仍然存在一些问题,例如,当你从仪表板启动一个新笔记本时,烦人的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笔记本/实验室才能工作。

没有文件/目录错误

在我的mint [ubuntu]系统上,尽管已经安装了jupyter,笔记本电脑也可以工作,但jupyter nbconvert——to脚本给出了错误,没有文件/目录,直到我做了一个单独的

sudo apt-get install jupyter-nbconvert

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

在你的工具链中有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