notebook | readme.md | Cell 2 | Search

This script automates the process of integrating JavaScript code generated from Jupyter Notebook cells back into the original notebooks. It finds corresponding JavaScript files, extracts their content, and replaces the code cells in the notebooks with the retrieved JavaScript.

Run example

npm run import -- "import notebook, import all cell modules"

import notebook, import all cell modules

var fs = require('fs');
var glob = require('glob');

var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
var PROJECT_PATH = PROFILE_PATH + '/Documents/jupytangular2/Utilities/.modules';
var project = PROFILE_PATH + '/Documents/jupytangular2';

var cells = glob.sync('**/cell-*', {cwd: PROJECT_PATH});
for(const c of cells) {
    const cell = path.basename(c);
    const notebook = path.basename(path.dirname(c));
    const parent = path.basename(path.dirname(path.dirname(c)));
    if(parent === 'jupytangular2') {
        continue;
    }
    const nb = JSON.parse(fs.readFileSync(path.join(
        project, parent, notebook + '.ipynb')));
    let counter = 0;
    for(const i in nb.cells) {
        if(!nb.cells.hasOwnProperty(i)) {
            continue;
        }
        // TODO: reimport all cells
        if(nb.cells[i].cell_type === 'code') {
            if(cell === 'cell-' + counter + '.js') {
                nb.cells[i].source = fs.readFileSync(path.join(PROJECT_PATH, c)).toString().split('\n');
            }
            counter++;
        }
    }
    fs.writeFileSync(path.join(project, parent, notebook + '.ipynb'),
                     JSON.stringify(nb, null, 2));
}

What the code could have been:

const path = require('path');
const fs = require('fs');
const glob = require('glob');

// Define constants
const { HOME, HOMEPATH, USERPROFILE } = process.env;
const PROFILE_PATH = HOME || HOMEPATH || USERPROFILE;
const PROJECT_PATH = path.join(PROFILE_PATH, 'Documents', 'jupytangular2', 'Utilities', '.modules');
const PROJECT_DIR = path.join(PROFILE_PATH, 'Documents', 'jupytangular2');

// Find notebook cells
const cells = glob.sync('**/cell-*', { cwd: PROJECT_PATH });

// Process notebook cells
cells.forEach((c) => {
  const cellName = path.basename(c);
  const notebookName = path.basename(path.dirname(c));
  const parentDir = path.basename(path.dirname(path.dirname(c)));

  // Skip notebooks in the project directory
  if (parentDir === 'jupytangular2') {
    return;
  }

  // Parse notebook file
  const nbPath = path.join(PROJECT_DIR, parentDir, `${notebookName}.ipynb`);
  const nb = JSON.parse(fs.readFileSync(nbPath));

  // Replace code cells with JavaScript files
  let counter = 0;
  nb.cells.forEach((cell, index) => {
    if (cell.cell_type === 'code') {
      const targetCellName = `cell-${counter}.js`;
      if (cellName === targetCellName) {
        const jsContent = fs.readFileSync(path.join(PROJECT_PATH, c), 'utf8');
        const lines = jsContent.split('\n');
        cell.source = lines.map((line) => line.trim());
      }
      counter++;
    }
  });

  // Write updated notebook file
  fs.writeFileSync(nbPath, JSON.stringify(nb, null, 2));
});

This code script processes Jupyter Notebook files (.ipynb) and replaces code cells with content from corresponding JavaScript files.

Here's a breakdown:

  1. Initialization:

  2. Finding Notebook Cells:

  3. Processing Each Notebook:

  4. Replacing Code Cells:

  5. Saving Modified Notebook: