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

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


当前回答

没有文件/目录错误

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

sudo apt-get install jupyter-nbconvert

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

其他回答

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

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

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

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

#!/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()

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

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