srstevenson / nb-clean

Clean Jupyter notebooks for version control. Remove metadata, outputs, and execution counts with Git and pre-commit support.
https://pypi.org/project/nb-clean
ISC License
135 stars 18 forks source link

Support Jupyter `savehook` #162

Closed yasirroni closed 1 year ago

yasirroni commented 1 year ago

It is nice to have git-filter and pre-commit. But, how about save hook so that it is running on save?

See https://jupyter-notebook.readthedocs.io/en/stable/extending/savehooks.html


What is the benefit? It will not cause git diff if you just open a notebook (because opening a notebook will add metadata) and save. Running it will also not add diff if nothing changed.

srstevenson commented 1 year ago

I expect this can be done by writing a function that:

  1. converts the model argument passed to the pre-save hook from a dictionary to a nbformat.notebooknode.NotebookNode;
  2. passes the NotebookNode to the clean_notebook function from nb-clean;
  3. converts the output back to a dictionary, and returns it.

This function could then be bound to c.ContentsManager.pre_save_hook in your jupyter_notebook_config.py.

Let's wait until there's a concrete use case to implement this though, as there's a maintenance cost to any new code. Hence I'll close this issue for now and it can be reopened if the need arises.

yasirroni commented 1 year ago

Then maybe, a simple snippet example of jupyter_notebook_config.py in this issue? So that user can just copy paste it without the need of us implementing installation inside nb-clean add-jupyter-hook