This is a a variation of a quite popular chunk of code that allows for saving HTML version of the notebook.
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(['ipython', 'nbconvert', '--to', 'script', fname], cwd=d)
c.FileContentsManager.post_save_hook = post_save
When combining it with jupytext lines
c.NotebookApp.contents_manager_class = "jupytext.TextFileContentsManager"
c.ContentsManager.default_jupytext_formats = "ipynb,py:percent"
It throws an error:
HTTP 500: Internal Server Error (Unexpected error while running post hook save: Command '['jupyter', 'nbconvert', '--to', 'html', 'notebook.py']' returned non-zero exit status 1.)
how to modify it? Or how to setup jupytext that it will also save HTML version of the notebook along py file?
Hello @danieltomasz , thanks for sharing this.
I think the issue here is that you are running jupyter nbconvert on a .py document.
Would you like to change your post_save hook to something like the below (i.e. test the extension, not the model type)?
def post_save(model, os_path, contents_manager):
"""post-save hook for converting notebooks to .py scripts"""
if not os_path.endswith('.ipynb'):
return # only do this for notebooks
d, fname = os.path.split(os_path)
check_call(['ipython', 'nbconvert', '--to', 'script', fname], cwd=d)
Thanks for the fast answer! It was my fault, somehow I overlooked that the nbconvert options instead of saving HTML tried to save again py file (like in the pre-jupytext times)
this line works
check_call(['ipython', 'nbconvert','--to', 'html', fname], cwd=d)
(BTW when jupytext was saving a file in light format there were no conflict, only when I switched to
spyder format conflict arose)
Oh I see... I also missed that 馃槃
So if we wrap it up, a correct post-save hook could be
def post_save(model, os_path, contents_manager):
"""post-save hook for converting notebooks to html files"""
if not os_path.endswith('.ipynb'):
return # only do this for notebooks
d, fname = os.path.split(os_path)
check_call(['jupyter', 'nbconvert','--to', 'html', fname], cwd=d)
Most helpful comment
Oh I see... I also missed that 馃槃
So if we wrap it up, a correct post-save hook could be