We have a collaborative project where the product is IpythonJupyter Notebooks, and we've use an approach for the last six months that is working great: we activate saving the .py
files automatically and track both .ipynb
files and the .py
files.
For ipython 2Jupyter
notebook servers, this can be accomplished by starting the server using:
ipython notebook --script
or by adding the linelines
c.FileNotebookManager.save_script = True
import os
from subprocess import check_call
def post_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(['jupyter', 'nbconvert', '--to', 'script', fname], cwd=d)
c.FileContentsManager.post_save_hook = post_save
to the ipython_notebook_configjupyter_notebook_config.py
file and restarting the notebook server.
If you aren't sure in which directory to find your jupyter_notebook_config.py
file, you can type jupyter --config-dir
, and if you don't find the file there, you can create it by typing jupyter notebook --generate-config
.
For ipythonIpython 3
notebook servers, this can be accomplished by adding the lines
to the ipython_notebook_config.py
file and restarting the notebook server. These lines are from a github issues answer @minrk provided and @dror includes them in his SO answer as well.
For Ipython 2
notebook servers, this can be accomplished by starting the server using:
ipython notebook --script
or by adding the line
c.FileNotebookManager.save_script = True
to the ipython_notebook_config.py
file and restarting the notebook server.