Skip to content

Commit

Permalink
Merge pull request ioos#175 from ocefpaf/add_colab_script
Browse files Browse the repository at this point in the history
add colab missing dependencies script
  • Loading branch information
ocefpaf authored Dec 23, 2023
2 parents 060ee1f + d97ca03 commit c4a0e06
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 4 deletions.
7 changes: 3 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@ repos:
- id: check-docstring-first
- id: check-json
- id: check-yaml
- id: double-quote-string-fixer

- repo: https://github.com/pre-commit/mirrors-prettier
rev: v4.0.0-alpha.3
rev: v4.0.0-alpha.8
hooks:
- id: prettier
types_or: [html]
exclude: "_templates/layout.html"

- repo: https://github.com/psf/black
rev: 23.11.0
rev: 23.12.0
hooks:
- id: black
language_version: python3
Expand All @@ -35,7 +34,7 @@ repos:
- id: add-trailing-comma

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.6
rev: v0.1.9
hooks:
- id: ruff

Expand Down
105 changes: 105 additions & 0 deletions jupyterbook/content/add_colab_install_missing_deps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
from pathlib import Path

from depfinder import notebook_path_to_dependencies

notebook_path = Path(".")
notebooks = list(notebook_path.glob("**/*.ipynb"))

## Get modules that are absent on GoogleColab
# required = []

# for notebook in notebooks:
# try:
# ret = notebook_path_to_dependencies(notebook)
# except Exception as err:
# print(f"Could not parse {notebook=}. {err}")
# required.extend(ret["required"])

# modules = sorted(set(required))

## go to colab with this list and run to obtain the absent list

# """
# import importlib
# for module in modules:
# if importlib.util.find_spec(module) is None:
# print(module)
# """

## update notebooks
import nbformat

absent = [
"bagit",
"cartopy",
"cf-units",
"cf_xarray",
"compliance-checker",
"erddapy",
"geoplot",
"gridgeo",
"ioos-tools",
"ioos_qc",
"ipyleaflet",
"iris",
"netcdf4",
"oceans",
"odvc",
"owslib",
"palettable",
"pocean-core",
"pyobis",
"pyoos",
"pysgrid",
"pyugrid",
"pyworms",
"retrying",
"seawater",
"zarr",
]


code = """\
import subprocess
import sys
COLAB = "google.colab" in sys.modules
def _install(package):
if COLAB:
ans = input(f"Install {{ package }}? [y/n]:")
if ans.lower() in ["y", "yes"]:
subprocess.check_call([sys.executable, "-m", "pip", "install", "--quiet", package])
print(f"{{ package }} installed!")
def _colab_install_missing_deps(deps):
import importlib
for dep in deps:
if importlib.util.find_spec(dep) is None:
if dep == "iris":
dep = "scitools-iris"
_install(dep)
deps = {}
_colab_install_missing_deps(deps)"""


def update_notebook(notebook, code):
nb = nbformat.read(notebook, as_version=4)

new_cell = nbformat.v4.new_code_cell(code)
new_cell.pop("id") # cannot save mod nb with this
new_cell["metadata"] = {"tags": ["remove-cell"]} # won't render on jupyterbook
# nb.cells.insert(0, new_cell)
nb.cells[0] = new_cell
nbformat.write(nb, notebook, version=4)


for notebook in notebooks:
if "archived" not in str(notebook):
try:
required = notebook_path_to_dependencies(notebook)["required"]
missing_from_colab = [module for module in absent if module in required]
update_notebook(notebook, code.format(missing_from_colab))
except Exception as err:
print(f"Could not parse {notebook=}. {err}")
continue

0 comments on commit c4a0e06

Please sign in to comment.