nbtx
Version:
Jupyter Notebook Translators: Transform Jupyter notebook JSON files (*.ipynb) to and from more compact data structures for use in web applications or other contexts where loading component parts (e.g. images, data, etc.) is preferred.
39 lines (38 loc) • 1.97 kB
JavaScript
import { minifyMimeOutput } from './mime.js';
import { minifyErrorOutput, minifyStreamOutput } from './text.js';
import { DEFAULT_HASH_WARNING, isNotNull, MAX_CHARS, TRUNCATED_CHARS_COUNT } from './utils.js';
async function minifyOneOutputItem(output, outputCache, opts) {
if (!('output_type' in output))
return null;
switch (output.output_type) {
case 'stream':
return minifyStreamOutput(output, outputCache, opts);
case 'error':
return minifyErrorOutput(output, outputCache, opts);
case 'update_display_data':
case 'display_data':
case 'execute_result':
return minifyMimeOutput(output, outputCache, opts);
default:
return null;
}
}
/**
* Given a list of nbformat IOutput objects, extract large outputs and cache their content on a separate data structure
*
* @param outputs: List of IOutput objects, including stream, error, execute_result, display_data, and update_display_data types.
* @param outputCache: MinifiedContentCache object for storing large output content
* @param opts:
* maxCharacters - the maximum allowed length for output content to remain in outputs; larger contents will be moved to outputCache
* truncateTo - where applicable, truncated text outputs will remain on the output when the full text is moved to outputCache
*/
export async function minifyCellOutput(outputs, outputCache, opts = {}) {
var _a, _b, _c;
const options = {
maxCharacters: (_a = opts.maxCharacters) !== null && _a !== void 0 ? _a : MAX_CHARS,
truncateTo: (_b = opts.truncateTo) !== null && _b !== void 0 ? _b : TRUNCATED_CHARS_COUNT,
computeHash: (_c = opts.computeHash) !== null && _c !== void 0 ? _c : DEFAULT_HASH_WARNING,
};
const minifiedOrNull = await Promise.all(outputs.map(async (output) => minifyOneOutputItem(output, outputCache, options)));
return minifiedOrNull.filter(isNotNull);
}