aboutsummaryrefslogtreecommitdiffstats
path: root/nbs/clean.ipynb
blob: a93eb83cd845831b9ffe9bc2052a2f8fd3b5286a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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
}