diff options
| -rw-r--r-- | nb/__init__.py | 1 | ||||
| -rw-r--r-- | nb/clean.py | 19 | ||||
| -rw-r--r-- | nbs/clean.ipynb | 121 | ||||
| -rw-r--r-- | nbs/init.ipynb | 4 |
4 files changed, 143 insertions, 2 deletions
diff --git a/nb/__init__.py b/nb/__init__.py index 87ba832..9aef474 100644 --- a/nb/__init__.py +++ b/nb/__init__.py @@ -1,4 +1,5 @@ # AUTOGENERATED! DO NOT EDIT! # cell: d072d8a4 +from nb.clean import clean_nb from nb.export import export_nb diff --git a/nb/clean.py b/nb/clean.py new file mode 100644 index 0000000..b733591 --- /dev/null +++ b/nb/clean.py @@ -0,0 +1,19 @@ +# AUTOGENERATED! DO NOT EDIT! + +# cell: 189df365 +import nbformat +from nbformat import NotebookNode + +# cell: 4c127179 +def _clean_cell(cell:dict): + if 'execution_count' in cell: cell['execution_count'] = None + if cell['source'].strip() == '': cell['source'] = [] + return cell + +# cell: 00c3e419 +def clean_nb(nb:NotebookNode): + "Clean noisy metadata in the notebook." + for c in nb['cells']: _clean_cell(c) + nb['metadata'] = {k:v for k,v in nb['metadata'].items() if k in _allowed_nb_metadata_keys} + if nb['metadata'].get('kernelspec', {}).get('name'): + nb['metadata']['kernelspec']['display_name'] = nb["metadata"]["kernelspec"]["name"] diff --git a/nbs/clean.ipynb b/nbs/clean.ipynb new file mode 100644 index 0000000..a93eb83 --- /dev/null +++ b/nbs/clean.ipynb @@ -0,0 +1,121 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "ce317915", + "metadata": {}, + "outputs": [], + "source": [ + "#| default_exp: clean" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "189df365", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "import nbformat\n", + "from nbformat import NotebookNode" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "6a4b558e", + "metadata": {}, + "outputs": [], + "source": [ + "from nb import test" + ] + }, + { + "cell_type": "markdown", + "id": "c3022df1", + "metadata": {}, + "source": [ + "# Clean\n", + "\n", + "> Cleaning ipynb notebooks to minimize diffs across machines" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4c127179", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "def _clean_cell(cell:dict):\n", + " if 'execution_count' in cell: cell['execution_count'] = None\n", + " if cell['source'].strip() == '': cell['source'] = []\n", + " return cell" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "1256ed4b", + "metadata": {}, + "outputs": [], + "source": [ + "cc = nbformat.v4.new_code_cell(\"print('hello')\", execution_count=10)\n", + "test.equal(_clean_cell(cc.copy()), {**cc, 'execution_count': None})" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e772071d", + "metadata": {}, + "outputs": [], + "source": [ + "cc = nbformat.v4.new_code_cell(\" \\n \\t\")\n", + "test.equal(_clean_cell(cc.copy()), {**cc, 'source': []})" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "944a371b", + "metadata": {}, + "outputs": [], + "source": [ + "_allowed_nb_metadata_keys = {\"kernelspec\", \"jupytext\", \"widgets\"}" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "00c3e419", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "def clean_nb(nb:NotebookNode):\n", + " \"Clean noisy metadata in the notebook.\"\n", + " for c in nb['cells']: _clean_cell(c)\n", + " nb['metadata'] = {k:v for k,v in nb['metadata'].items() if k in _allowed_nb_metadata_keys}\n", + " if nb['metadata'].get('kernelspec', {}).get('name'):\n", + " nb['metadata']['kernelspec']['display_name'] = nb[\"metadata\"][\"kernelspec\"][\"name\"]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "df8e33bf", + "metadata": {}, + "outputs": [], + "source": [ + "import nb; nb.export_nb('clean.ipynb', src_dir='../nb/')" + ] + } + ], + "metadata": {}, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/nbs/init.ipynb b/nbs/init.ipynb index b6aed2d..d120b49 100644 --- a/nbs/init.ipynb +++ b/nbs/init.ipynb @@ -26,8 +26,8 @@ "outputs": [], "source": [ "#| export\n", - "#| export\n", - "from nb.export import export_nb\n" + "from nb.clean import clean_nb\n", + "from nb.export import export_nb" ] }, { |