问题
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