Skip to content

Instantly share code, notes, and snippets.

@pjbull
Last active November 13, 2023 20:17
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save pjbull/221685a8e03a01baaf1e to your computer and use it in GitHub Desktop.
Save pjbull/221685a8e03a01baaf1e to your computer and use it in GitHub Desktop.
Create .py and .html on save of Jupyter notebook
import os
import re
from nbconvert.nbconvertapp import NbConvertApp
from nbconvert.postprocessors.base import PostProcessorBase
class CopyToSubfolderPostProcessor(PostProcessorBase):
def __init__(self, subfolder=None):
self.subfolder = subfolder
super(CopyToSubfolderPostProcessor, self).__init__()
def postprocess(self, input):
""" Save converted file to a separate directory. """
if self.subfolder is None:
return
dirname, filename = os.path.split(input)
new_dir = os.path.join(dirname, self.subfolder)
new_path = os.path.join(new_dir, filename)
if not os.path.exists(new_dir):
os.mkdir(new_dir)
with open(input, 'r') as f:
text = f.read()
with open(new_path, 'w') as f:
f.write(re.sub(r'\n#\sIn\[(([0-9]+)|(\s))\]:\n{2}', '', text))
os.remove(input)
SAVE_PROGRESS_INDICATOR_FILE = '.ipynb_saveprogress'
def post_save(model, os_path, contents_manager):
"""post-save hook for converting notebooks to .py scripts and html
in a separate folder with the same name
"""
# only do this for notebooks
if model['type'] != 'notebook':
return
# only do this if we've added the special indicator file to the working directory
cwd = os.path.dirname(os_path)
save_progress_indicator = os.path.join(cwd, SAVE_PROGRESS_INDICATOR_FILE)
should_convert = os.path.exists(save_progress_indicator)
if should_convert:
d, fname = os.path.split(os_path)
subfolder = os.path.splitext(fname)[0]
converter = NbConvertApp()
converter.postprocessor = CopyToSubfolderPostProcessor(subfolder=subfolder)
converter.export_format = 'script'
converter.initialize(argv=[])
converter.notebooks = [os_path]
converter.convert_notebooks()
c.FileContentsManager.post_save_hook = post_save
@hydrosquall
Copy link

Hey Peter, what's the recommended way to use this file? Found the link from your slides, but unfortunately wasn't able to attend the accompanying talk.

@aleks-mariusz
Copy link

from the looks of it, I think you would just add these contents to your regular ~/.jupyter/jupyter_notebook_config.py

@ejm714
Copy link

ejm714 commented Jan 26, 2019

Adding this to the end of the postprocess function (instead of lines 25 and 26) will remove the cell numbers from the .py file 🎉

with open(input, 'r') as f:
    text = f.read()

with open(new_path, 'w') as f:
    f.write(re.sub(r'\n#\sIn\[(([0-9]+)|(\s))\]:\n{2}', '', text))

os.remove(input)

@pjbull
Copy link
Author

pjbull commented Jan 26, 2019

Thanks @ejm714, I updated with that fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment