CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-jupyterlab--outputarea

JupyterLab output area component for rendering notebook execution results with multiple MIME type support

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

model.mddocs/

Output Area Model

The Output Area Model provides the core data management layer for JupyterLab output areas. It manages collections of output items with observable state changes, stream consolidation, and JSON serialization support.

Capabilities

Output Area Model Interface

Core interface defining the contract for output area data models.

/**
 * Interface for output area model providing observable data management
 */
interface IOutputAreaModel extends IDisposable {
  /** Signal emitted when an output item changes (emits the index) */
  readonly stateChanged: ISignal<IOutputAreaModel, number>;
  
  /** Signal emitted when the list of items changes */
  readonly changed: ISignal<IOutputAreaModel, IOutputAreaModel.ChangedArgs>;
  
  /** The number of output items in the model */
  readonly length: number;
  
  /** Whether the output area is trusted for executing untrusted content */
  trusted: boolean;
  
  /** The content factory used by the model for creating output items */
  readonly contentFactory: IOutputAreaModel.IContentFactory;
  
  /**
   * Get an output item at the specified index
   * @param index - The index of the item to retrieve
   * @returns The output model at the specified index
   */
  get(index: number): IOutputModel;
  
  /**
   * Remove characters from the end of the last stream output
   * @param number - Number of characters to remove
   */
  removeStreamOutput(number: number): void;
  
  /**
   * Append text to the last stream output
   * @param text - Text to append
   */
  appendStreamOutput(text: string): void;
  
  /**
   * Add an output item, potentially combining with previous stream output
   * @param output - The notebook output to add
   * @returns The total number of outputs after addition
   */
  add(output: nbformat.IOutput): number;
  
  /**
   * Remove an output item at the specified index
   * @param index - The index of the item to remove
   */
  remove(index: number): void;
  
  /**
   * Set the output value at the specified index
   * @param index - The index to set
   * @param output - The notebook output to set
   */
  set(index: number, output: nbformat.IOutput): void;
  
  /**
   * Clear all output items
   * @param wait - If true, delay clearing until next output is added
   */
  clear(wait?: boolean): void;
  
  /**
   * Deserialize the model from JSON notebook output format
   * @param values - Array of notebook outputs to load
   */
  fromJSON(values: nbformat.IOutput[]): void;
  
  /**
   * Serialize the model to JSON notebook output format
   * @returns Array of notebook outputs
   */
  toJSON(): nbformat.IOutput[];
}

Output Area Model Implementation

Default implementation of the output area model with stream consolidation and change notifications.

/**
 * Default implementation of IOutputAreaModel with observable behavior
 */
class OutputAreaModel implements IOutputAreaModel {
  /**
   * Create a new output area model
   * @param options - Configuration options for the model
   */
  constructor(options?: IOutputAreaModel.IOptions);
  
  /** Signal emitted when an output item state changes */
  get stateChanged(): ISignal<IOutputAreaModel, number>;
  
  /** Signal emitted when the output list changes */
  get changed(): ISignal<IOutputAreaModel, IOutputAreaModel.ChangedArgs>;
  
  /** Number of output items in the model */
  get length(): number;
  
  /** Whether the model is trusted for untrusted content execution */
  get trusted(): boolean;
  set trusted(value: boolean);
  
  /** Content factory for creating output models */
  readonly contentFactory: IOutputAreaModel.IContentFactory;
  
  /** Whether the model has been disposed */
  get isDisposed(): boolean;
  
  /**
   * Dispose of the model and clean up resources
   */
  dispose(): void;
  
  /**
   * Get an output model at the specified index
   * @param index - Index of the output to retrieve
   * @returns The output model at the index
   */
  get(index: number): IOutputModel;
  
  /**
   * Set an output at the specified index
   * @param index - Index to set the output at
   * @param value - Notebook output to set
   */
  set(index: number, value: nbformat.IOutput): void;
  
  /**
   * Remove characters from the end of the last stream output
   * @param number - Number of characters to remove from the end
   */
  removeStreamOutput(number: number): void;
  
  /**
   * Append text to the last stream output
   * @param text - Text to append to the stream
   */
  appendStreamOutput(text: string): void;
  
  /**
   * Add an output, with intelligent stream consolidation
   * @param output - Notebook output to add
   * @returns Total number of outputs after addition
   */
  add(output: nbformat.IOutput): number;
  
  /**
   * Remove an output at the given index
   * @param index - Index of output to remove
   */
  remove(index: number): void;
  
  /**
   * Clear all outputs from the model
   * @param wait - If true, delay clearing until next output
   */
  clear(wait?: boolean): void;
  
  /**
   * Load outputs from JSON notebook format
   * @param values - Array of notebook outputs to load
   */
  fromJSON(values: nbformat.IOutput[]): void;
  
  /**
   * Export outputs to JSON notebook format
   * @returns Array of notebook outputs
   */
  toJSON(): nbformat.IOutput[];
  
  /**
   * Determine whether a new output should be combined with the previous one
   * @param options - Object containing the new output and last model
   * @returns Whether the outputs should be combined
   * 
   * #### Notes
   * This method is called only when both outputs are stream messages of the same type.
   * The default implementation always returns true, but subclasses can override this
   * to provide custom combination logic.
   */
  protected shouldCombine(options: {
    value: nbformat.IOutput;
    lastModel: IOutputModel;
  }): boolean;
}

Usage Examples:

import { OutputAreaModel } from "@jupyterlab/outputarea";

// Create a basic model
const model = new OutputAreaModel();

// Create a model with initial outputs and trust
const modelWithData = new OutputAreaModel({
  values: [
    {
      output_type: "stream",
      name: "stdout", 
      text: ["Hello World\n"]
    }
  ],
  trusted: true
});

// Listen for changes
model.changed.connect((sender, args) => {
  console.log(`Output list changed: ${args.type}`);
});

model.stateChanged.connect((sender, index) => {
  console.log(`Output at index ${index} changed`);
});

// Add various output types
model.add({
  output_type: "execute_result",
  execution_count: 1,
  data: {
    "text/plain": ["42"]
  },
  metadata: {}
});

model.add({
  output_type: "stream",
  name: "stderr",
  text: ["Error message\n"]
});

// Serialize to JSON
const outputs = model.toJSON();
console.log(outputs);

// Load from JSON
model.fromJSON(outputs);

Content Factory

Factory interface and implementation for creating output models with customization support.

/**
 * Interface for creating output models
 */
interface IOutputAreaModel.IContentFactory {
  /**
   * Create an output model for rendering
   * @param options - Options for the output model
   * @returns A new output model instance
   */
  createOutputModel(options: IOutputModel.IOptions): IOutputModel;
}

/**
 * Default content factory implementation
 */
class OutputAreaModel.ContentFactory implements IOutputAreaModel.IContentFactory {
  /**
   * Create an output model using the default OutputModel class
   * @param options - Options for creating the output model
   * @returns New OutputModel instance
   */
  createOutputModel(options: IOutputModel.IOptions): IOutputModel;
}

/**
 * Default content factory instance used when none is provided
 */
namespace OutputAreaModel {
  export const defaultContentFactory: OutputAreaModel.ContentFactory;
}

Model Options and Types

/**
 * Options for creating an output area model
 */
interface IOutputAreaModel.IOptions {
  /** Initial output values to populate the model */
  values?: nbformat.IOutput[];
  
  /** Whether the output should be treated as trusted (default: false) */
  trusted?: boolean;
  
  /** Content factory for creating output models (uses default if not provided) */
  contentFactory?: IOutputAreaModel.IContentFactory;
}

/**
 * Type alias for output list change arguments
 */
type IOutputAreaModel.ChangedArgs = IObservableList.IChangedArgs<IOutputModel>;

Stream Processing

The model includes sophisticated stream processing capabilities:

  • Stream Consolidation: Consecutive stream outputs of the same type are automatically merged
  • Text Processing: Handles backspace, carriage return, and newline characters properly
  • Memory Management: Prevents memory leaks in long-running stream outputs
  • Real-time Updates: Provides incremental updates to stream content without full re-rendering

docs

index.md

model.md

widget.md

tile.json