2015-08-25 02:42:39 +08:00
|
|
|
File save hooks
|
|
|
|
===============
|
|
|
|
|
|
|
|
You can configure functions that are run whenever a file is saved. There are
|
|
|
|
two hooks available:
|
|
|
|
|
2016-10-03 02:59:18 +08:00
|
|
|
* ``ContentsManager.pre_save_hook`` runs on the API path and model with
|
|
|
|
content. This can be used for things like stripping output that people don't
|
|
|
|
like adding to VCS noise.
|
2015-08-25 02:42:39 +08:00
|
|
|
* ``FileContentsManager.post_save_hook`` runs on the filesystem path and model
|
|
|
|
without content. This could be used to commit changes after every save, for
|
|
|
|
instance.
|
|
|
|
|
2021-08-09 22:47:08 +08:00
|
|
|
They are both called with keyword arguments:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2015-08-25 02:42:39 +08:00
|
|
|
|
|
|
|
pre_save_hook(model=model, path=path, contents_manager=cm)
|
|
|
|
post_save_hook(model=model, os_path=os_path, contents_manager=cm)
|
|
|
|
|
|
|
|
Examples
|
|
|
|
--------
|
|
|
|
|
|
|
|
These can both be added to :file:`jupyter_notebook_config.py`.
|
|
|
|
|
2021-08-09 22:47:08 +08:00
|
|
|
A pre-save hook for stripping output:
|
|
|
|
|
|
|
|
.. code-block:: python
|
2015-08-25 02:42:39 +08:00
|
|
|
|
|
|
|
def scrub_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
|
|
|
|
|
|
|
|
c.FileContentsManager.pre_save_hook = scrub_output_pre_save
|
|
|
|
|
|
|
|
A post-save hook to make a script equivalent whenever the notebook is saved
|
2016-11-29 07:13:11 +08:00
|
|
|
(replacing the ``--script`` option in older versions of the notebook):
|
2015-08-25 02:42:39 +08:00
|
|
|
|
2016-11-29 07:13:11 +08:00
|
|
|
.. code-block:: python
|
2016-10-03 02:59:18 +08:00
|
|
|
|
2015-08-25 02:42:39 +08:00
|
|
|
import io
|
|
|
|
import os
|
2016-01-04 08:06:18 +08:00
|
|
|
from notebook.utils import to_api_path
|
2016-10-03 02:59:18 +08:00
|
|
|
|
2015-08-25 02:42:39 +08:00
|
|
|
_script_exporter = None
|
|
|
|
|
|
|
|
def script_post_save(model, os_path, contents_manager, **kwargs):
|
|
|
|
"""convert notebooks to Python script after save with nbconvert
|
|
|
|
|
2018-09-12 17:37:14 +08:00
|
|
|
replaces `jupyter notebook --script`
|
2015-08-25 02:42:39 +08:00
|
|
|
"""
|
|
|
|
from nbconvert.exporters.script import ScriptExporter
|
|
|
|
|
|
|
|
if model['type'] != 'notebook':
|
|
|
|
return
|
|
|
|
|
|
|
|
global _script_exporter
|
2016-10-03 02:59:18 +08:00
|
|
|
|
2015-08-25 02:42:39 +08:00
|
|
|
if _script_exporter is None:
|
|
|
|
_script_exporter = ScriptExporter(parent=contents_manager)
|
2016-10-03 02:59:18 +08:00
|
|
|
|
2015-08-25 02:42:39 +08:00
|
|
|
log = contents_manager.log
|
|
|
|
|
|
|
|
base, ext = os.path.splitext(os_path)
|
|
|
|
script, resources = _script_exporter.from_filename(os_path)
|
|
|
|
script_fname = base + resources.get('output_extension', '.txt')
|
|
|
|
log.info("Saving script /%s", to_api_path(script_fname, contents_manager.root_dir))
|
2016-10-03 02:59:18 +08:00
|
|
|
|
2015-08-25 02:42:39 +08:00
|
|
|
with io.open(script_fname, 'w', encoding='utf-8') as f:
|
|
|
|
f.write(script)
|
2016-10-03 02:59:18 +08:00
|
|
|
|
2015-08-25 02:42:39 +08:00
|
|
|
c.FileContentsManager.post_save_hook = script_post_save
|
|
|
|
|
2016-10-03 02:59:18 +08:00
|
|
|
|
2015-08-25 02:42:39 +08:00
|
|
|
This could be a simple call to ``jupyter nbconvert --to script``, but spawning
|
|
|
|
the subprocess every time is quite slow.
|