Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions jb/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ set -euo pipefail
# Build the Jupyter book version
cd "$(dirname "$0")"

# Link the translated notebooks into the Jupyter Book source directory.
# Copy the translated notebooks into the Jupyter Book source directory so
# prep_notebooks.py can add build-only metadata without modifying originals.
for notebook in ../chapters/chap[01][0-9]*.ipynb; do
target="$(basename "$notebook")"
if [ -e "$target" ] && [ ! -L "$target" ]; then
echo "Refusing to replace existing non-symlink: $target" >&2
if [ -e "$target" ] && [ ! -f "$target" ] && [ ! -L "$target" ]; then
echo "Refusing to replace existing non-file: $target" >&2
exit 1
fi
ln -sfn "$notebook" "$target"
rm -f "$target"
cp "$notebook" "$target"
done

python3 prep_notebooks.py

if command -v jupyter-book >/dev/null 2>&1; then
jupyter_book_cmd="jupyter-book"
else
Expand Down
7 changes: 6 additions & 1 deletion jb/prep_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@ def process_cell(cell):
# get tags
tags = cell['metadata'].get('tags', [])

# add hide-cell tag to solutions
if cell['cell_type'] == 'code':
source = cell['source']

if 'def download(url):' in source and 'import thinkpython' in source:
tags = cell['metadata'].setdefault('tags', [])
for tag in ['remove-cell', 'keep']:
if tag not in tags:
tags.append(tag)

# remove solutions
if source.startswith('# Solution') or 'solution' in tags:
cell['source'] = []
Expand Down