How do I convert a IPython Notebook into a Python file via commandline?

后端 未结 10 2529
一生所求
一生所求 2020-11-29 14:42

I\'m looking at using the *.ipynb files as the source of truth and programmatically \'compiling\' them into .py files for scheduled jobs/tasks.

The

相关标签:
10条回答
  • 2020-11-29 14:49

    Jupytext is nice to have in your toolchain for such conversions. It allows not only conversion from a notebook to a script, but you can go back again from the script to notebook as well. And even have that notebook produced in executed form.

    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 
    
    0 讨论(0)
  • 2020-11-29 14:50

    If you don't want to output a Python script every time you save, or you don't want to restart the IPython kernel:

    On the command line, you can use nbconvert:

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

    As a bit of a hack, you can even call the above command in an IPython notebook by pre-pending ! (used for any command line argument). Inside a notebook:

    !jupyter nbconvert --to script config_template.ipynb
    

    Before --to script was added, the option was --to python or --to=python, but it was renamed in the move toward a language-agnostic notebook system.

    0 讨论(0)
  • 2020-11-29 14:55

    Following the previous example but with the new nbformat lib version :

    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'))
    
    0 讨论(0)
  • 2020-11-29 14:57

    The following example turns an Iron Python Notebook called a_notebook.ipynb into a python script called a_python_script.py leaving out the cells tagged with the keyword remove, which I add manually to the cells that I don't want to end up in the script, leaving out visualizations and other steps that once I am done with the notebook I don't need to be executed by the script.

    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)
    
    0 讨论(0)
  • 2020-11-29 14:59

    If you want to convert all *.ipynb files from current directory to python script, you can run the command like this:

    jupyter nbconvert --to script *.ipynb
    
    0 讨论(0)
  • 2020-11-29 15:00

    You can do this from the 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)
    
    0 讨论(0)
提交回复
热议问题