import { NotebookPanel } from "@jupyterlab/notebook"; import { Kernel } from "@jupyterlab/services"; import { CommandRegistry } from "@phosphor/commands"; /** Contains utility functions for manipulating/handling notebooks in the application. */ export default class NotebookUtilities { /** * Opens a pop-up dialog in JupyterLab to display a simple message. * @param title The title for the message popup * @param msg The message as an array of strings * @param buttonLabel The label to use for the button. Default is 'OK' * @param buttonClassName The classname to give to the 'ok' button * @returns Promise - A promise once the message is closed. */ static showMessage(title: string, msg: string[], buttonLabel?: string, buttonClassName?: string): Promise; /** * Opens a pop-up dialog in JupyterLab to display a yes/no dialog. * @param title The title for the message popup * @param msg The message * @param acceptLabel The label to use for the accept button. Default is 'YES' * @param rejectLabel The label to use for the reject button. Default is 'NO' * @param yesButtonClassName The classname to give to the accept button. * @param noButtonClassName The classname to give to the cancel button. * @returns Promise - A promise once the message is closed. */ static showYesNoDialog(title: string, msg: string, acceptLabel?: string, rejectLabel?: string, yesButtonClassName?: string, noButtonClassName?: string): Promise; /** * Opens a pop-up dialog in JupyterLab with various information and button * triggering reloading the page. * @param title The title for the message popup * @param msg The message * @param buttonLabel The label to use for the button. Default is 'Refresh' * @param buttonClassName The classname to give to the 'refresh' button. * @returns Promise - A promise once the message is closed. */ static showRefreshDialog(title: string, msg: string[], buttonLabel?: string, buttonClassName?: string): Promise; /** * @description Creates a new JupyterLab notebook for use by the application * @param command The command registry * @returns Promise - A promise containing the notebook panel object that was created (if successful). */ static createNewNotebook(command: CommandRegistry): Promise; /** * Safely saves the Jupyter notebook document contents to disk * @param notebookPanel The notebook panel containing the notebook to save */ static saveNotebook(notebookPanel: NotebookPanel): Promise; /** * Convert the notebook contents to JSON * @param notebookPanel The notebook panel containing the notebook to serialize */ static notebookToJSON(notebookPanel: NotebookPanel): any; /** * @description Gets the value of a key from specified notebook's metadata. * @param notebookPanel The notebook to get meta data from. * @param key The key of the value. * @returns any -The value of the metadata. Returns null if the key doesn't exist. */ static getMetaData(notebookPanel: NotebookPanel, key: string): any; /** * @description Sets the key value pair in the notebook's metadata. * If the key doesn't exists it will add one. * @param notebookPanel The notebook to set meta data in. * @param key The key of the value to create. * @param value The value to set. * @param save Default is false. Whether the notebook should be saved after the meta data is set. * Note: This function will not wait for the save to complete, it only sends a save request. * @returns The old value for the key, or undefined if it did not exist. */ static setMetaData(notebookPanel: NotebookPanel, key: string, value: any, save?: boolean): any; /** * Get a new Kernel, not tied to a Notebook * Source code here: https://github.com/jupyterlab/jupyterlab/tree/473348d25bcb258ca2f0c127dd8fb5b193217135/packages/services */ static createNewKernel(): Promise; static executeWithNewKernel(action: Function, args?: any[]): Promise; /** * @description This function runs code directly in the notebook's kernel and then evaluates the * result and returns it as a promise. * @param kernel The kernel to run the code in. * @param runCode The code to run in the kernel. * @param userExpressions The expressions used to capture the desired info from the executed code. * @param runSilent Default is false. If true, kernel will execute as quietly as possible. * store_history will be set to false, and no broadcast on IOPUB channel will be made. * @param storeHistory Default is false. If true, the code executed will be stored in the kernel's history * and the counter which is shown in the cells will be incremented to reflect code was run. * @param allowStdIn Default is false. If true, code running in kernel can prompt user for input using * an input_request message. * @param stopOnError Default is false. If True, does not abort the execution queue, if an exception is encountered. * This allows the queued execution of multiple execute_requests, even if they generate exceptions. * @returns Promise - A promise containing the execution results of the code as an object with * keys based on the user_expressions. * @example * //The code * const code = "a=123\nb=456\nsum=a+b"; * //The user expressions * const expr = {sum: "sum",prod: "a*b",args:"[a,b,sum]"}; * //Async function call (returns a promise) * sendKernelRequest(notebookPanel, code, expr,false); * //Result when promise resolves: * { * sum:{status:"ok",data:{"text/plain":"579"},metadata:{}}, * prod:{status:"ok",data:{"text/plain":"56088"},metadata:{}}, * args:{status:"ok",data:{"text/plain":"[123, 456, 579]"}} * } * @see For more information on JupyterLab messages: * https://jupyter-client.readthedocs.io/en/latest/messaging.html#execution-results */ static sendKernelRequest(kernel: Kernel.IKernelConnection, runCode: string, userExpressions: any, runSilent?: boolean, storeHistory?: boolean, allowStdIn?: boolean, stopOnError?: boolean): Promise; /** * Same as method sendKernelRequest but passing * a NotebookPanel instead of a Kernel */ static sendKernelRequestFromNotebook(notebookPanel: NotebookPanel, runCode: string, userExpressions: any, runSilent?: boolean, storeHistory?: boolean, allowStdIn?: boolean, stopOnError?: boolean): Promise; }