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

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


当前回答

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

其他回答

使用nbconvert 6.07和jupyter client 6.1.12:

转换jupyter笔记本到python脚本

$ jupyter nbconvert mynotebook.ipynb --to python

转换jupyter笔记本到python脚本指定输出文件名

$ jupyter nbconvert mynotebook.ipnb --to python --output myscript.py

%notebook foo。ipynb magic命令将当前IPython导出到“foo.ipynb”。

更多信息输入%笔记本?

遵循前面的例子,但是使用了新的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'))

给出的解决方案仅适用于转换单个.py文件。下面是一个转换目录及其子目录中的所有.py文件的解决方案。

首先,您需要安装一次只转换一个文件的工具,如ipynb-py-convert

PIP安装ipynb-py-convert

然后cd到你的文件夹。py文件和目录的位置。然后我们在目录和子目录中的所有文件上递归地运行这个工具:

powershell:

foreach ($f在Get-ChildItem "."过滤器*。ipynb-py-convert $f。FullName " $(美元f.FullName.Substring (0, f.FullName.Length-6美元)). py”}

现在,如果你想用批处理转换从。ipynb转换到。py,你可以运行:

foreach ($f在Get-ChildItem "."-Filter *.py -递归){ipynb-py-convert $f。FullName " $(美元f.FullName.Substring (0, f.FullName.Length-3)美元).ipynb "}

这在我探索.py文件时给了我很大帮助。我做了一个项目的副本,运行这段代码,并快速在Jupiter测试代码的不同部分作为单元格等等。我希望它能帮助更多的人。

下面是一种不使用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()