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

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


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

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

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

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

!jupyter nbconvert --to script config_template.ipynb

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


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

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

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

用于转换所有*。Ipynb将当前目录下的文件递归格式化为python脚本:

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

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

jupyter nbconvert --to script *.ipynb

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


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

有一个非常好的包叫做nb_dev,它是为在Jupyter notebook中编写Python包而设计的。与nbconvert一样,它可以将笔记本转换为.py文件,但它更灵活、更强大,因为它有许多不错的额外创作功能,可以帮助您在PyPI上开发测试、文档和注册包。它是快速发展起来的。人工智能的人。

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


下面的示例将一个名为a_notebook的Iron Python Notebook转换为。ipynb转换到一个名为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', encoding='utf-8') as f:
    f.writelines(the_python_script)

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

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

通过以下步骤,我能够得到.py文件

安装pip Install ipynb-py-convert 通过命令提示符进入ipynb文件所在目录 输入命令

ipynb-py-convert你的文件名。ipynb YourFilename.py

如:。ipynb-py-convert getting-started-with-kaggle-titanic-problem。ipynb getting-started-with-kaggle-titanic-problem.py

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


没有文件/目录错误

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

sudo apt-get install jupyter-nbconvert

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


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

更多信息输入%笔记本?


使用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

给出的解决方案仅适用于转换单个.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测试代码的不同部分作为单元格等等。我希望它能帮助更多的人。


下面是一个jq解决方案,可能在某些情况下有用。记住,笔记本只是json。

jq -r '.cells[] | select(.cell_type  == "code") | .source[] | rtrimstr("\n")' $filename

我发现有两种方法可以用命令行将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脚本示例:


我已经构建了一个函数来实现这一点。用户不需要安装任何东西就可以使用它。

#!/usr/bin/python


# A short routine to convert a Jupyter Notebook to a Python file

import json

def ipynb_to_py(input_ipynb_file,output_py_file=None):
    """
    Generate a Python script (.py) that includes all source code from the input Jupyter notebook (.ipynb).
    
    The user can input a Jupyter Notebook file from the current working directory or from a path.
    
    If the name for output Python file is not specified, 
    the output file name will copy the file name of the input Jupyter Notebook, 
    but the file exention will be changed from ".ipynb" chanegd to ".py".
    And the output Python file will be saved at the same directory of the input Jupyter Notebook.
    For example:
    ipynb_to_py("test-jupyternotebook.ipynb")
    ipynb_to_py("./test-input-dir/test-jupyternotebook.ipynb")
    
    The user can also specify an output file name that ends with ".py".
    If the output file name is provided, but no path to output file is added, 
    the file will be saved at the current working directory.
    For example:
    ipynb_to_py("test-jupyternotebook.ipynb","test1.py")
    ipynb_to_py("./test-input-dir/test-jupyternotebook.ipynb","test2.py")
        
    The user can save out the file at a target directory by adding a path to the output file.
    For example: 
    ipynb_to_py("test-jupyternotebook.ipynb","./test-outputdir/test3.py")
    ipynb_to_py("./test-input-dir/test-jupyternotebook.ipynb","./test-output-dir/test4.py")
    
    This function does not edit or delete the original input Jupyter Notebook file.
    
    Args:
    -----
        input_ipynb_file: The file name string for the Jupyter Notebook (ends with ".ipynb")
        output_py_file (optional): The file name for Python file to be created (ends with ".py"). 
    
    Returns:
    --------
        A Python file containing all source code in the Jupyter Notebook.
        
    Example usages:
    ---------------
        ipynb_to_py("test-jupyternotebook.ipynb")
        ipynb_to_py("./test-input-dir/test-jupyternotebook.ipynb")
        ipynb_to_py("test-jupyternotebook.ipynb","test1.py")
        ipynb_to_py("test-jupyternotebook.ipynb","./test-outputdir/test2.py")
        ipynb_to_py("test-jupyternotebook.ipynb","./test-outputdir/test3.py")
        ipynb_to_py("./test-input-dir/test-jupyternotebook.ipynb","./test-output-dir/test4.py")
             
    """
    # Check if the input file is a Jupyter Notebook
    if input_ipynb_file.endswith(".ipynb"):
        
        # Open the input Jupyter Notebook file
        notebook = open(input_ipynb_file)
        
        # Read its content in the json format
        notebook_content = json.load(notebook)

        # Only extract the source code snippet from each cell in the input Jupyter Notebook
        source_code_snippets = [cell['source'] for cell in notebook_content['cells']]
        
        # If the name for output Python file is not specified,
        # The name of input Jupyter Notebook will be used after changing ".ipynb" to ".py".
        if output_py_file == None:
            output_py_file = input_ipynb_file.split('.ipynb')[0]+".py"
        else:
            pass

        # Create a Python script to save out all the extracted source code snippets
        output_file = open(output_py_file,'w')

        # Print out each line in each source code snippet to the output file
        for snippet in source_code_snippets:
            for line in snippet:
                # Use end='' to avoid creating unwanted gaps between lines
                print(line,end = '',file = output_file)
            # At end of each snippet, move to the next line before printing the next one
            print('',sep = '\n',file=output_file)

        # Close the output file
        output_file.close()
        print("The path to output file:",output_py_file)
        
    else:
        print("The input file must be a Jupyter Notebook (in .ipynb format)!")
        
def main():
    pass

if __name__ == "__main__":
    main()