Skip to content

Commit

Permalink
move notebook conversion logic into a script
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 671267079
Change-Id: I7f9e4d11441fdf3f9cc211aa64f845f2d5aeb612
  • Loading branch information
jagapiou authored and copybara-github committed Sep 5, 2024
1 parent d3fb4a6 commit 0b9b3f7
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 10 deletions.
11 changes: 1 addition & 10 deletions .github/actions/install-examples/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,4 @@ runs:

- name: Convert notebooks
shell: bash
run: |
find "examples" -type f -iname '*.ipynb' -exec sh -c '
for file do
sed "/^\s*\"%%/d" "$file" > "${file%.ipynb}_modified.ipynb"
done
' sh {} + && \
jupyter nbconvert --to=python --output-dir=notebooks \
--RegexRemovePreprocessor.patterns='(!|%)pip' \
$(find examples -type f -iname '*_modified.ipynb')
ls notebooks/*.py
run: ./bin/convert_notebooks.sh notebooks
61 changes: 61 additions & 0 deletions bin/convert_notebooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/sh
#
# Copyright 2023 DeepMind Technologies Limited.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Converts notebooks to .py files for testing.

readonly SCRIPT_DIR="$( cd "$( dirname "$0" )" &> /dev/null && pwd )"
readonly EXAMPLES_DIR="${SCRIPT_DIR}"/../examples
readonly OUTPUT_DIR="$1"
readonly TEMP_DIR="$(mktemp -d)"


function find_notebooks() {
find "${EXAMPLES_DIR}" -type f -iname '*.ipynb'
}


function collect_notebooks() {
for file in $(find_notebooks); do
echo "Copying ${file} to ${TEMP_DIR}"
cp "${file}" "${TEMP_DIR}"
done
}


function remove_cell_magic() {
for file in "${TEMP_DIR}"/*; do
echo "Removing cell magic from ${file}"
sed -i "/^\s*\"%%/d" "${file}"
done
}


function convert_notebooks() {
jupyter nbconvert --to=python --output-dir="${OUTPUT_DIR}" \
--RegexRemovePreprocessor.patterns='(%|!)pip' \
"${TEMP_DIR}"/*
}


function main() {
collect_notebooks \
&& remove_cell_magic \
&& convert_notebooks \
&& ls "${OUTPUT_DIR}"
}


main >&2

0 comments on commit 0b9b3f7

Please sign in to comment.