3

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

plalanne
  • 1,010
  • 2
  • 13
  • 30

1 Answers1

5

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!

plalanne
  • 1,010
  • 2
  • 13
  • 30
Madhup Srivastava
  • 446
  • 1
  • 6
  • 18
  • This seems correct to me : some more details about this little config script can be found here : https://stackoverflow.com/questions/29329667/ipython-notebook-script-deprecated-how-to-replace-with-post-save-hook And for curious minds, here is the original issue topic on github : https://github.com/ipython/ipython/issues/8009 – plalanne Jul 11 '17 at 07:54
  • 2
    In new versions of jupyter you will get warning: _"Subcommand 'ipython nbconvert' is deprecated and will be removed in future versions. You likely want to use 'jupyter nbconvert' in the future."_ Thus substitute `ipython` for `jupyter` in the code. See https://stackoverflow.com/a/29351623/286795 – matthiash Jan 30 '18 at 12:19
  • jupytext works. https://towardsdatascience.com/introducing-jupytext-9234fdff6c57 – Shivam Anand Dec 10 '21 at 19:52