forked from ioos/ioos_code_lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ioos#175 from ocefpaf/add_colab_script
add colab missing dependencies script
- Loading branch information
Showing
2 changed files
with
108 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |