defscrub_output_pre_save(model, **kwargs): """scrub output before saving notebooks""" # only run on notebooks if model['type'] != 'notebook': return # only run on nbformat v4 if model['content']['nbformat'] != 4: return
for cell in model['content']['cells']: if cell['cell_type'] != 'code': continue cell['outputs'] = [] cell['execution_count'] = None
import io import os from notebook.utils import to_api_path
_script_exporter = None
defscript_post_save(model, os_path, contents_manager, **kwargs): """convert notebooks to Python script after save with nbconvert replaces `ipython notebook --script` """ from nbconvert.exporters.script import ScriptExporter
if model['type'] != 'notebook': return
global _script_exporter
if _script_exporter isNone: _script_exporter = ScriptExporter(parent=contents_manager)