Add generate_all_notebook.sh
This commit is contained in:
12
tools/export_to_ipynb.py
Normal file → Executable file
12
tools/export_to_ipynb.py
Normal file → Executable file
@@ -1,3 +1,4 @@
|
||||
#!/usr/bin/env python3
|
||||
import ast
|
||||
from nbformat import v3, v4
|
||||
import sys
|
||||
@@ -40,7 +41,16 @@ nbook['cells'].append(v4.new_code_cell(full_text))
|
||||
jsonform = v4.writes(nbook) + '\n'
|
||||
output = input
|
||||
output = output.replace('.py', '.ipynb')
|
||||
output = output.replace('examples/python', 'examples/notebook')
|
||||
# For example/python/foo.py -> example/notebook/examples/foo.ipynb
|
||||
output = output.replace('examples/python', 'examples/notebook/examples')
|
||||
|
||||
# For example/contrib/foo.py -> example/notebook/contrib/foo.ipynb
|
||||
output = output.replace('examples/contrib', 'examples/notebook/contrib')
|
||||
|
||||
# For ortools/*/samples/foo.py -> example/notebook/*/foo.ipynb
|
||||
output = output.replace('ortools', 'examples/notebook')
|
||||
output = output.replace('samples/', '')
|
||||
|
||||
print('writing %s' % output)
|
||||
with open(output, "w") as fpout:
|
||||
fpout.write(jsonform)
|
||||
|
||||
55
tools/generate_all_notebooks.sh
Executable file
55
tools/generate_all_notebooks.sh
Executable file
@@ -0,0 +1,55 @@
|
||||
#!/usr/bin/env bash
|
||||
# usage: ./tools/generate_all_notebooks.sh
|
||||
set -e
|
||||
|
||||
DIR="${BASH_SOURCE%/*}"
|
||||
if [[ ! -d "${DIR}" ]]; then
|
||||
DIR="${PWD}";
|
||||
fi
|
||||
|
||||
echo "Intalling python3 notebook..."
|
||||
/usr/bin/env python3 -m pip install --user notebook
|
||||
|
||||
###############
|
||||
## Cleanup ##
|
||||
###############
|
||||
echo "Remove previous .ipynb files..."
|
||||
rm -f examples/notebook/*.ipynb
|
||||
rm -f examples/notebook/*/*.ipynb
|
||||
echo "Remove previous .ipynb files...DONE"
|
||||
|
||||
################
|
||||
## Examples ##
|
||||
################
|
||||
for FILE in examples/python/*.py; do
|
||||
# if no files found do nothing
|
||||
[[ -e "$FILE" ]] || continue
|
||||
mkdir -p examples/notebook/examples
|
||||
echo "Generating ${FILE%.py}.ipynb"
|
||||
./tools/export_to_ipynb.py "$FILE";
|
||||
done
|
||||
|
||||
###############
|
||||
## Contrib ##
|
||||
###############
|
||||
for FILE in examples/contrib/*.py; do
|
||||
# if no files found do nothing
|
||||
[[ -e "$FILE" ]] || continue
|
||||
if [ $(basename "$FILE") == "word_square.py" ]; then continue; fi
|
||||
mkdir -p examples/notebook/contrib
|
||||
echo "Generating ${FILE%.py}.ipynb"
|
||||
./tools/export_to_ipynb.py "$FILE";
|
||||
done
|
||||
|
||||
###############
|
||||
## Samples ##
|
||||
###############
|
||||
for FILE in ortools/*/samples/*.py ; do
|
||||
# if no files found do nothing
|
||||
[[ -e "$FILE" ]] || continue
|
||||
D=$(dirname $(dirname "${FILE}"))
|
||||
mkdir -p ${D/ortools/examples\/notebook}
|
||||
echo "Generating ${FILE%.py}.ipynb"
|
||||
./tools/export_to_ipynb.py "$FILE"
|
||||
done
|
||||
# vim: set tw=0 ts=2 sw=2 expandtab:
|
||||
Reference in New Issue
Block a user