This code defines a function searchNotebooks
that searches for text within Jupyter Notebook files in a project directory and returns a promise that resolves with an array of matching cells.
npm run import -- "search notebooks gulp"
var importer = require('../Core');
var path = require('path');
var gulp = require('gulp');
var tap = require('gulp-tap');
var Duplex = require('stream').Duplex;
var tasksToPromise = importer.import("gulp tasks to promise",
"{gulp}");
var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
var project = path.join(__dirname, '..');
function bufferToStream(buffer) {
let stream = new Duplex();
stream.push(buffer);
stream.push(null);
return stream;
}
function searchNotebooks(search) {
var cells;
gulp.task('search notebooks', function () {
cells = [];
return gulp.src(
['**/*.ipynb', '!node_modules{,/**}', '!**/\\.*'],
{cwd: project})
.pipe(tap(file => {
const result = importer.getCells(file.path);
result.forEach(r => {
if(r.source.join('').match(search)) {
cells.push(r);
}
})
}));
});
return tasksToPromise(['search notebooks'], [])
.then(() => cells);
}
module.exports = searchNotebooks;
// Import required modules
const { getCells } = require('../Core');
const path = require('path');
const gulp = require('gulp');
const tap = require('gulp-tap');
const { Duplex } = require('stream');
const tasksToPromise = require('../Core').import('gulp tasks to promise', { gulp });
// Define constants
const PROFILE_PATH = (process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE) || '';
const PROJECT_ROOT = path.join(__dirname, '..');
/**
* Convert a buffer to a stream
* @param {Buffer} buffer The buffer to convert
* @returns {Duplex} The resulting stream
*/
function bufferToStream(buffer) {
const stream = new Duplex();
stream.push(buffer);
stream.push(null);
return stream;
}
/**
* Search notebooks for a given search term
* @param {string} search The search term to use
* @returns {Promise<Array>} A promise resolving to an array of matching cells
*/
async function searchNotebooks(search) {
// Initialize an empty array to store matching cells
let cells = [];
// Define the gulp task to search for notebooks
const searchTask = gulp.task('search notebooks', async () => {
// Use gulp.src to find all notebooks in the project root
await gulp.src(['**/*.ipynb', '!node_modules{,/**}', '!**/\\.*'], { cwd: PROJECT_ROOT })
.pipe(tap((file) => {
// Get the cells from the notebook
const result = getCells(file.path);
// Filter the cells based on the search term
result.forEach((r) => {
if (r.source.join('').match(search)) {
cells.push(r);
}
});
}));
});
// Run the gulp task and wait for it to complete
await tasksToPromise(['search notebooks'], []);
// Return the array of matching cells
return cells;
}
// Export the searchNotebooks function
module.exports = searchNotebooks;
This code defines a function searchNotebooks
that searches for specific text within Jupyter Notebook files within a project directory.
Here's a breakdown:
Initialization:
importer
(likely for interacting with Jupyter Notebook files), path
(for file path manipulation), gulp
(for task management), tap
(for stream processing), Duplex
(for creating custom streams), and tasksToPromise
(for converting Gulp tasks to promises).PROFILE_PATH
(user's home directory) and project
(path to the project directory).bufferToStream
Function:
searchNotebooks
Function:
.ipynb
files in the project directory (excluding node_modules
and hidden files).importer.getCells
to extract code cells from each notebook.search
string.cells
array.tasksToPromise
to convert the Gulp task into a promise.cells
array containing all matching cells.Export:
searchNotebooks
function, making it available for use in other parts of the application.