Skip to content

Commit

Permalink
Bring back cleaning of notebooks (#203)
Browse files Browse the repository at this point in the history
- Add local `pre-commit` hook that will ensure any notebooks added to the gallery won't contain any output cells
- Fix `Checkerboard` notebook
  • Loading branch information
alcarney authored Mar 19, 2020
1 parent 814fe18 commit e1bfcea
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 20 deletions.
8 changes: 8 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@ repos:
hooks:
- id: black
exclude: 'solutions'

- repo: local
hooks:
- id: clean-notebooks
name: clean-notebooks
entry: ./scripts/clean-notebooks.sh
language: script
files: \.ipynb$
12 changes: 6 additions & 6 deletions blog/src/gallery/Checkerboard.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -13,7 +13,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -24,13 +24,13 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"@ar.definition\n",
"def Checkerboard(width, height) -> ar.Image:\n",
" board = Grid(shape=CheckerPattern())\n",
" board = Grid(defn=CheckerPattern())\n",
" return ar.fill(board(width, height))\n",
"\n",
"checkerboard = Checkerboard()"
Expand Down Expand Up @@ -64,9 +64,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.1-final"
"version": "3.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
}
10 changes: 5 additions & 5 deletions blog/src/gallery/Rolling Hills.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -14,7 +14,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -37,7 +37,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -95,9 +95,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.1-final"
"version": "3.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
}
16 changes: 8 additions & 8 deletions blog/src/gallery/Simple Clock.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -45,7 +45,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -87,7 +87,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -118,7 +118,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -162,7 +162,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -210,7 +210,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -262,9 +262,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.1-final"
"version": "3.8.2"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
}
34 changes: 34 additions & 0 deletions scripts/clean-notebooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/bin/bash

# This ensures that all of te tutorial notebook files have no
# content when committed. It does the following:
#
# 1. Take the MD5 hash of the original file.
# 2. Strip out all the output cells from the file, saving to a temp file
# 3. Take the MD5 hash of the converted file.
# 4. If they are different, copy the file over the original and add it to the
# list of modified

for f in "$@"
do
md5Orig=$(md5sum "$f" | cut -f 1 -d \ )

outDir=$(dirname "$f")
out="${outDir}/tmp.ipynb"

jupyter nbconvert --log-level=ERROR \
--ClearOutputPreprocessor.enabled=True \
--to notebook \
--output="tmp" \
"$f"

md5New=$(md5sum "$out" | cut -f 1 -d \ )

if [ "$md5New" == "$md5Orig" ]; then
rm "$out"
else
echo "Cleaned: $f"
mv "$out" "$f"
fi

done
2 changes: 1 addition & 1 deletion tests/data/Pythagoras.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down

0 comments on commit e1bfcea

Please sign in to comment.