[0ce940]: / docs / render_ipynb.bash

Download this file

49 lines (37 with data), 1.6 kB

 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
#!/bin/bash
#
# Compile .md from .ipynb files for mkdocs
# Names of ipynb files to convert (from /notebooks) are specified in ipynb_pages.txt
set -e
function sed_replace {
# https://stackoverflow.com/a/10467453/7529152
# sed_replace <from> <to> <file>"
sed -i "s/$(echo $1 | sed -e 's/\([[\/.*]\|\]\)/\\&/g')/$(echo $2 | sed -e 's/[\/&]/\\&/g')/g" $3
}
DIR=tutorials
for file in $(cat ipynb_pages.txt); do
echo "converting: ${file}.ipynb"
# .ipynb -> .md
jupyter nbconvert --to markdown ../notebooks/${file}.ipynb --output-dir sources/${DIR}/
file_out=sources/${DIR}/${file}.md
dir_out=sources/${DIR}/${file}_files
# fix the paths for the original images
sed_replace '![img](../docs/theme_dir/img/' '![img](/bpnet/img/' $file_out
# prepend the original ipython notebook link
echo -e "Generated from [notebooks/${file}.ipynb](https://github.com/kundajelab/bpnet/blob/master/notebooks/${file}.ipynb)\n$(cat ${file_out})" > ${file_out}
if [ -d "${dir_out}" ]; then
# move the additional files <file>_files to theme_dir/img/ipynb/ (inline plots in ipynb)
if [ -d "theme_dir/img/ipynb/${file}_files" ]; then
rm -r theme_dir/img/ipynb/${file}_files
fi
mv -f ${dir_out} theme_dir/img/ipynb/
# fix the path in the .md file
sed_replace '![png]('${file}'_files' '![png](/bpnet/img/ipynb/'${file}'_files' $file_out
sed_replace '![svg]('${file}'_files' '![svg](/bpnet/img/ipynb/'${file}'_files' $file_out
fi
done
if [ -d "theme_dir/bpnet" ]; then
rm -r theme_dir/bpnet
fi
mkdir -p theme_dir/bpnet
ln -sr theme_dir/img theme_dir/bpnet/img