UNPKG

@nteract/messaging

Version:

Messaging mechanics for nteract apps (jupyter spec)

176 lines (175 loc) 5.66 kB
import { KernelStatus } from "@nteract/types"; import { BasicOutputMessageContent, ExecuteRequest, JupyterMessage, MessageType, UpdateDisplayData, UpdateDisplayDataContent } from "./types"; export declare type Channel = "shell" | "iopub" | "stdin"; /** * Returns a fully-formatted kernel message. * * @param header An object containing the message type and session information * @param content The message type-specific contents to send in the kernel message * * @returns The fully-formatted kernel message */ export declare function message<MT extends MessageType>(header: { msg_type: MT; username?: string; session?: string; }, content?: object): JupyterMessage<MT>; /** * An execute request creator * * > executeRequest('print("hey")', { 'silent': true }) * { header: * { msg_id: 'f344cc6b-4308-4405-a8e8-a166b0345579', * date: 2017-10-23T22:33:39.970Z, * version: '5.0', * msg_type: 'execute_request', * username: 'kyle', * session: '123' }, * metadata: {}, * parent_header: {}, * content: * { code: 'print("hey")', * silent: false, * store_history: true, * user_expressions: {}, * allow_stdin: true, * stop_on_error: false } } * * @param code The code to execute * @param options The options for the execute request * * @returns A complete execute_request message */ export declare function executeRequest(code?: string, options?: { silent?: boolean; store_history?: boolean; user_expressions?: object; allow_stdin?: boolean; stop_on_error?: boolean; }): ExecuteRequest; /** * Creates a display_data message. * * ref: http://jupyter-client.readthedocs.io/en/stable/messaging.html#display-data * > displayData({username: 'rgbkrk', session: '123'}, {data: {'text/html': '<b>sup</b>'}}, "display_data") * { header: * { msg_id: '24e95ce7-73d5-4c5f-9ef0-ff8547779690', * date: 2017-10-23T22:57:58.704Z, * version: '5.1', * msg_type: 'display_data', * username: 'rgbkrk', * session: '123' }, * metadata: {}, * parent_header: {}, * content: * { data: { 'text/html': '<b>sup</b>' }, * metadata: {}, * transient: {} } } */ export declare function displayData<C extends BasicOutputMessageContent, T extends MessageType>(content: C, msg_type: T): JupyterMessage<T, C>; export declare function displayData<C extends BasicOutputMessageContent>(content: C, msg_type?: undefined): JupyterMessage<"display_data", C>; /** * Creates an update_display_data message. * * http://jupyter-client.readthedocs.io/en/stable/messaging.html#update-display-data */ export declare function updateDisplayData(content: UpdateDisplayDataContent): UpdateDisplayData; /** * Creates a message containing information about the result of an execution. * * http://jupyter-client.readthedocs.io/en/stable/messaging.html#id6 */ export declare function executeResult(content: { execution_count: number; data?: object; metadata?: object; transient?: object; }): JupyterMessage<"execute_result", { execution_count: number; data?: object | undefined; metadata?: object | undefined; transient?: object | undefined; }>; /** * Creates an error message to indicate when an exception has occurred during * code execution. * * http://jupyter-client.readthedocs.io/en/stable/messaging.html#execution-errors */ export declare function error(content: { ename?: string; evalue?: string; traceback?: string[]; }): JupyterMessage<"error", any>; /** * Creates a stream message. * * http://jupyter-client.readthedocs.io/en/stable/messaging.html#streams-stdout-stderr-etc * * @param content The message type and its contents. */ export declare function stream(content: { name: "stdout" | "stderr"; text: string; }): JupyterMessage<"stream", any>; /** * Creates a message containing the response from a kernel execute request. * * http://jupyter-client.readthedocs.io/en/stable/messaging.html#execution-results */ export interface ExecuteReplyError { status: string; execution_count: number; ename: string; evalue: string; traceback: string[]; } export interface ExecuteReplyOk { status: string; execution_count: number; payload?: object[]; user_expressions?: object; } export declare function executeReply(content: ExecuteReplyOk | ExecuteReplyError): JupyterMessage<"execute_reply", any>; /** * Creates a status message published by the kernel to indicate its state. * * @param execution_state The kernel's execution state */ export declare function status(execution_state: KernelStatus.Busy | KernelStatus.Idle | KernelStatus.Starting): JupyterMessage<"status", any>; /** * * @param content */ export declare function clearOutput(content?: { wait: boolean; }): JupyterMessage<"clear_output", any>; /** * * @param content */ export declare function executeInput(content: { code: string; execution_count: number; }): JupyterMessage<"execute_input", any>; /** * Creates a message to request information about a kernel. * * @returns A kernel_info_request message */ export declare function kernelInfoRequest(): JupyterMessage<"kernel_info_request", any>; /** * Creates a message to request the shutdown of a kernel. * * @param content An options object containing whether or not to restart the kernel * * @returns A shutdown_request message */ export declare function shutdownRequest(content?: { restart?: boolean; }): JupyterMessage<"shutdown_request", { restart?: boolean; }>; export declare function inputReply(content: { value: string; }): JupyterMessage<"input_reply">;