Export a notebook (.ipynb) as a .py file upon saving

孤人 提交于 2020-08-08 10:46:26

问题


I'm currently working with Jupyter IPython Notebook.I would like to put my notebook under version control.

That's why, when I save and checkpoint a Notebook (.ipynb file), I would like the changes to also be saved and synchronized in the corresponding python script (.py file) in the same folder. (see picture below)

my_files

Does it have something to do with the version of Jupyter I am using? Or do I have to edit a config_file?

Thanks


回答1:


You need to create jupyter_notebook_config.py in your config-dir.

if doesn't exists, execute below command from home directory : ./jupyter notebook --generate-config

Add paste following code in this file :

import os
from subprocess import check_call

c = get_config()

def post_save(model, os_path, contents_manager):
    """post-save hook for converting notebooks to .py and .html files."""
    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)
    check_call(['ipython', 'nbconvert', '--to', 'html', fname], cwd=d)

c.FileContentsManager.post_save_hook = post_save

You will then need to restart your jupyter server and there you go!



来源:https://stackoverflow.com/questions/45011012/export-a-notebook-ipynb-as-a-py-file-upon-saving

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!