Notebook format interfaces and utilities for working with Jupyter Notebook format specifications
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Type-safe interfaces for all cell output types including execution results, display data, streams, and errors, with comprehensive type guard functions for runtime type checking.
Core output type definitions and the base interface all outputs extend.
/**
* An output union type representing all possible output types
*/
type IOutput = IUnrecognizedOutput | IExecuteResult | IDisplayData | IStream | IError;
/**
* The valid output types
*/
type OutputType = 'execute_result' | 'display_data' | 'stream' | 'error' | 'update_display_data';
/**
* The base output type that all outputs extend
*/
interface IBaseOutput extends PartialJSONObject {
/** Type of cell output */
output_type: string;
}
/**
* Cell output metadata type alias
*/
type OutputMetadata = PartialJSONObject;Interface for outputs produced by code execution that return a value.
/**
* Result of executing a code cell that produces a return value
*/
interface IExecuteResult extends IBaseOutput {
/** Type of cell output */
output_type: 'execute_result';
/** A result's prompt number */
execution_count: ExecutionCount;
/** A mime-type keyed dictionary of data */
data: IMimeBundle;
/** Cell output metadata */
metadata: OutputMetadata;
}Usage Example:
import { IExecuteResult, IMimeBundle } from "@jupyterlab/nbformat";
const executeResult: IExecuteResult = {
output_type: 'execute_result',
execution_count: 1,
data: {
'text/plain': '42',
'application/json': { value: 42 }
},
metadata: {}
};Interface for rich display outputs like images, HTML, and other media.
/**
* Data displayed as a result of code cell execution
*/
interface IDisplayData extends IBaseOutput {
/** Type of cell output */
output_type: 'display_data';
/** A mime-type keyed dictionary of data */
data: IMimeBundle;
/** Cell output metadata */
metadata: OutputMetadata;
}Usage Example:
import { IDisplayData } from "@jupyterlab/nbformat";
const displayData: IDisplayData = {
output_type: 'display_data',
data: {
'text/html': '<h1>Hello World</h1>',
'text/plain': 'Hello World'
},
metadata: {
'text/html': {
'isolated': true
}
}
};Interface for updating existing display outputs.
/**
* Data displayed as an update to existing display data
*/
interface IDisplayUpdate extends IBaseOutput {
/** Type of cell output */
output_type: 'update_display_data';
/** A mime-type keyed dictionary of data */
data: IMimeBundle;
/** Cell output metadata */
metadata: OutputMetadata;
}Interface for text streams like stdout and stderr.
/**
* Stream output from a code cell
*/
interface IStream extends IBaseOutput {
/** Type of cell output */
output_type: 'stream';
/** The name of the stream */
name: StreamType;
/** The stream's text output */
text: MultilineString;
}
/**
* An alias for a stream type
*/
type StreamType = 'stdout' | 'stderr';Usage Example:
import { IStream, StreamType } from "@jupyterlab/nbformat";
const streamOutput: IStream = {
output_type: 'stream',
name: 'stdout',
text: ['Hello, world!', 'Line 2 of output']
};
const errorStream: IStream = {
output_type: 'stream',
name: 'stderr',
text: 'Warning: deprecated function'
};Interface for error outputs including tracebacks.
/**
* Output of an error that occurred during code cell execution
*/
interface IError extends IBaseOutput {
/** Type of cell output */
output_type: 'error';
/** The name of the error */
ename: string;
/** The value, or message, of the error */
evalue: string;
/** The error's traceback */
traceback: string[];
}Usage Example:
import { IError } from "@jupyterlab/nbformat";
const errorOutput: IError = {
output_type: 'error',
ename: 'ValueError',
evalue: 'invalid literal for int() with base 10: "abc"',
traceback: [
'\u001b[0;31m---------------------------------------------------------------------------\u001b[0m',
'\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)',
'\u001b[0;32m<ipython-input-1-abc>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m',
'\u001b[0;31mValueError\u001b[0m: invalid literal for int() with base 10: "abc"'
]
};Interface for outputs that don't match known types.
/**
* Unrecognized output interface for unknown output types
*/
interface IUnrecognizedOutput extends IBaseOutput {}Runtime type checking functions to determine output types.
/**
* Test whether an output is an execute result
*/
function isExecuteResult(output: IOutput): output is IExecuteResult;
/**
* Test whether an output is from display data
*/
function isDisplayData(output: IOutput): output is IDisplayData;
/**
* Test whether an output is from updated display data
*/
function isDisplayUpdate(output: IOutput): output is IDisplayUpdate;
/**
* Test whether an output is from a stream
*/
function isStream(output: IOutput): output is IStream;
/**
* Test whether an output is an error
*/
function isError(output: IOutput): output is IError;Usage Example:
import {
IOutput,
isExecuteResult,
isDisplayData,
isStream,
isError
} from "@jupyterlab/nbformat";
function processOutputs(outputs: IOutput[]): void {
outputs.forEach((output, index) => {
if (isExecuteResult(output)) {
console.log(`Output ${index}: Execute result (count: ${output.execution_count})`);
console.log(`Data keys: ${Object.keys(output.data).join(', ')}`);
} else if (isDisplayData(output)) {
console.log(`Output ${index}: Display data`);
console.log(`MIME types: ${Object.keys(output.data).join(', ')}`);
} else if (isStream(output)) {
console.log(`Output ${index}: ${output.name} stream`);
const text = Array.isArray(output.text) ? output.text.join('') : output.text;
console.log(`Content: ${text.substring(0, 50)}...`);
} else if (isError(output)) {
console.log(`Output ${index}: Error - ${output.ename}: ${output.evalue}`);
console.log(`Traceback lines: ${output.traceback.length}`);
} else {
console.log(`Output ${index}: Unrecognized type: ${output.output_type}`);
}
});
}
// Example: Filter only error outputs
function getErrors(outputs: IOutput[]): IError[] {
return outputs.filter(isError);
}
// Example: Get all text content from stream outputs
function getStreamText(outputs: IOutput[]): string {
return outputs
.filter(isStream)
.map(stream => Array.isArray(stream.text) ? stream.text.join('') : stream.text)
.join('');
}