Links

Get installs

Tags: #jupyternotebooks #naas #jupyter-notebooks #read #installs #snippet #operations #text
Author: Florent Ravenel

Input

Import libraries

import json
from pprint import pprint

Variables

# Input
notebook_path = "../template.ipynb"

Model

Get module install in notebook

def get_installs(notebook_path):
with open(notebook_path) as f:
nb = json.load(f)
data = []
cells = nb.get("cells")
# Check each cells
for cell in cells:
cell_type = cell.get('cell_type')
sources = cell.get('source')
for source in sources:
if cell_type == "code":
if "pip install" in source:
install = source.split("pip install")[-1].replace("\n", "").strip()
data.append(install)
if len(data) == 0:
print("❎ No install found in notebook:", notebook_path)
else:
print(f"✅ {len(data)} install(s) found in notebook:", notebook_path)
return data

Output

Display result

installs = get_installs(notebook_path)
print(installs)