Skip to content Skip to sidebar Skip to footer

Automatically Convert Jupyter Notebook To .py

I know there have been a few questions about this but I have not found anything robust enough. Currently I am using, from terminal, a command that creates .py, then moves them to a

Solution 1:

Another way would be to use Jupytext as extension for your jupyter installation (can be easily pip installed).

Jupytext Description (see github page)

Have you always wished Jupyter notebooks were plain text documents? Wished you could edit them in your favorite IDE? And get clear and meaningful diffs when doing version control? Then... Jupytext may well be the tool you're looking for!

It will keep paired notebooks in sync with .py files. You then just need to move your .py files or gitignore the notebooks for example as possible workflows.

Solution 2:

You can add the following code in the last cell in your notebook file.

!jupyter nbconvert --to script mycode.ipynb
with open('mycode.py', 'r') as f:
    lines = f.readlines()
with open('mycode.py', 'w') as f:
    for line inlines:
        if'nbconvert --to script'in line:
            breakelse:
            f.write(line)

It will generate the .py file and then remove this very code from it. You will end up with a clean script that will not call !jupyter nbconvert anymore.

Solution 3:

This is the closest I have found to what I had in mind, but I have yet to try and implement it:

# A post-save hook to make a script equivalent whenever the notebook is saved (replacing the --script option in older versions of the notebook):import io
import os
from notebook.utils import to_api_path

_script_exporter = Nonedefscript_post_save(model, os_path, contents_manager, **kwargs):
    """convert notebooks to Python script after save with nbconvert

    replaces `jupyter notebook --script`
    """from nbconvert.exporters.script import ScriptExporter

    if model['type'] != 'notebook':
        returnglobal _script_exporter

    if _script_exporter isNone:
        _script_exporter = ScriptExporter(parent=contents_manager)

    log = contents_manager.log

    base, ext = os.path.splitext(os_path)
    script, resources = _script_exporter.from_filename(os_path)
    script_fname = base + resources.get('output_extension', '.txt')
    log.info("Saving script /%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

Additionally, this looks like it has worked to some user on github, so I put it here for reference:

import os
from subprocess import check_call

defpost_save(model, os_path, contents_manager):
    """post-save hook for converting notebooks to .py scripts"""if model['type'] != 'notebook':
        return# only do this for notebooks
    d, fname = os.path.split(os_path)
    check_call(['ipython', 'nbconvert', '--to', 'script', fname], cwd=d)

Post a Comment for "Automatically Convert Jupyter Notebook To .py"