Skip to content

Commit

Permalink
Merge pull request #314 from willirath/python3_support_set_notebook_k…
Browse files Browse the repository at this point in the history
…ernels

Set python version when copying example notebooks
  • Loading branch information
erikvansebille committed Mar 12, 2018
2 parents 89d90f5 + dc3fe01 commit bc70d77
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
9 changes: 2 additions & 7 deletions .travis.yml
Expand Up @@ -51,12 +51,7 @@ script:
make linkcheck -C docs;
fi
- |
# evaluate example scripts and notebooks on linux python2
if [[ "${PY_VERSION}" = 2 ]] && [[ "${TRAVIS_OS_NAME}" = "linux" ]]; then
# evaluate example scripts and notebooks on linux only
if [[ "${TRAVIS_OS_NAME}" = "linux" ]]; then
py.test -v -s --nbval-lax examples/;
fi
- |
# evaluate example scripts only on linux python2
if [[ "${PY_VERSION}" = 3 ]] && [[ "${TRAVIS_OS_NAME}" = "linux" ]]; then
py.test -v -s examples/;
fi
27 changes: 27 additions & 0 deletions parcels/scripts/get_examples.py
Expand Up @@ -2,6 +2,8 @@

import argparse
from datetime import datetime, timedelta
from glob import glob
import json
import os
import pkg_resources
from progressbar import ProgressBar
Expand All @@ -12,6 +14,7 @@
# Fall back to Python 2's urllib2
from urllib2 import urlopen
import shutil
import sys

example_data_files = (
["MovingEddies_data/" + fn for fn in [
Expand Down Expand Up @@ -58,6 +61,25 @@ def copy_data_and_examples_from_package_to(target_path):
pass


def set_jupyter_kernel_to_python_version(path, python_version=2):
"""Set notebook kernelspec to desired python version.
This also drops all other meta data from the notebook.
"""
for file_name in glob(os.path.join(path, "*.ipynb")):

with open(file_name, 'r') as f:
notebook_data = json.load(f)

notebook_data['metadata'] = {"kernelspec": {
"display_name": "Python {}".format(python_version),
"language": "python",
"name": "python{}".format(python_version)}}

with open(file_name, 'w') as f:
json.dump(notebook_data, f, indent=2)


def _still_to_download(file_names, target_path):
"""Only return the files that are not yet present on disk."""
for fn in list(file_names):
Expand Down Expand Up @@ -103,6 +125,11 @@ def main(target_path=None):
# copy data and examples
copy_data_and_examples_from_package_to(target_path)

# make sure the notebooks use the correct python version
set_jupyter_kernel_to_python_version(
target_path,
python_version=sys.version_info[0])

# try downloading remaining files
remaining_example_data_files = _still_to_download(
example_data_files, target_path)
Expand Down

0 comments on commit bc70d77

Please sign in to comment.