-
-
Save hwartig/c363f6d8bb836acc4f19884ed0e2c29a to your computer and use it in GitHub Desktop.
Create .py on save of Jupyter notebook
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Copy this file to ~/.jupyter/jupyter_notebook_config.py | |
| import os | |
| import shutil | |
| from nbconvert.nbconvertapp import NbConvertApp | |
| SAVE_PROGRESS_INDICATOR_FILE_PY = '.ipynb_save_py' | |
| def post_save(model, os_path, contents_manager): | |
| """ post-save hook for converting notebooks to .py scripts """ | |
| # 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_PY) | |
| should_convert_py = os.path.exists(save_progress_indicator) | |
| if should_convert_py: | |
| d, fname = os.path.split(os_path) | |
| converter = NbConvertApp() | |
| converter.export_format = 'script' | |
| converter.initialize() | |
| converter.notebooks = [os_path] | |
| converter.convert_notebooks() | |
| c.FileContentsManager.post_save_hook = post_save |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment