import { IVariableInspector , VariableInspectorPanel , } from './variableinspector' ; import { KernelConnector } from './kernelconnector' ; import { VariableInspectionHandler , DummyHandler } from './handler' ; import { VariableInspectorManager , IVariableInspectorManager } from './manager' ; import { Languages } from './inspectorscripts' ; import { ICommandPalette , WidgetTracker } from '@jupyterlab/apputils' ; import { ILabShell , ILayoutRestorer , JupyterFrontEnd , JupyterFrontEndPlugin , } from '@jupyterlab/application' ; import { IConsoleTracker } from '@jupyterlab/console' ; import { INotebookTracker , NotebookPanel } from '@jupyterlab/notebook' ; import { listIcon } from '@jupyterlab/ui-components' ; namespace CommandIDs { export const open = 'variableinspector:open' ; } /**
 * A service providing variable introspection.
 */ const variableinspector = { id : '@lckr/jupyterlab_variableinspector' , requires : [ ICommandPalette , ILayoutRestorer , ILabShell ] , provides : IVariableInspectorManager , autoStart : true , activate : ( app , palette , restorer , labShell ) : IVariableInspectorManager => { const manager = new VariableInspectorManager ( ) ; const category = 'Variable Inspector' ; const command = CommandIDs . open ; const label = 'Open Variable Inspector' ; const namespace = 'variableinspector' ; const tracker = new WidgetTracker < VariableInspectorPanel > ( { namespace } ) ; /**
         * Create and track a new inspector.
         */ function newPanel ( ) { const panel = new VariableInspectorPanel ( ) ; panel . id = 'jp-variableinspector' ; panel . title . label = 'Variable Inspector' ; panel . title . icon = listIcon ; panel . title . closable = true ; panel . disposed . connect ( ( ) => { if ( manager . panel === panel ) { manager . panel = null ; } } ) ; tracker . add ( panel ) ; return panel ; } restorer . restore ( tracker , { command , args : ( ) => null , name : ( ) => 'variableinspector' , } ) ; app . commands . addCommand ( command , { label , execute : ( ) => { if ( ! manager . panel || manager . panel . isDisposed ) { manager . panel = newPanel ( ) ; } if ( ! manager . panel . isAttached ) { labShell . add ( manager . panel , 'main' ) ; } if ( manager . source ) { manager . source . performInspection ( ) ; } labShell . activateById ( manager . panel . id ) ; } , } ) ; palette . addItem ( { command , category } ) ; console . log ( 'JupyterLab extension @lckr/jupyterlab_variableinspector is activated!' ) ; return manager ; } , } ; /**
 * An extension that registers consoles for variable inspection.
 */ const consoles = { id : '@lckr/jupyterlab-variableinspector:consoles' , requires : [ IVariableInspectorManager , IConsoleTracker , ILabShell ] , autoStart : true , activate : ( app , manager , consoles , labShell ) : void => { const handlers = { } ; /**
         * Subscribes to the creation of new consoles. If a new notebook is created, build a new handler for the consoles.
         * Adds a promise for a instanced handler to the 'handlers' collection.
         */ consoles . widgetAdded . connect ( ( sender , consolePanel ) => { if ( manager . hasHandler ( consolePanel . sessionContext . path ) ) { handlers [ consolePanel . id ] = new Promise ( ( resolve , reject ) => { resolve ( manager . getHandler ( consolePanel . sessionContext . path ) ) ; } ) ; } else { handlers [ consolePanel . id ] = new Promise ( ( resolve , reject ) => { const session = consolePanel . sessionContext ; const connector = new KernelConnector ( { session } ) ; const scripts = connector . ready . then ( ( ) => { return connector . kernelLanguage . then ( ( lang ) => { return Languages . getScript ( lang ) ; } ) ; } ) ; scripts . then ( ( result ) => { const initScript = result . initScript ; const queryCommand = result . queryCommand ; const matrixQueryCommand = result . matrixQueryCommand ; const widgetQueryCommand = result . widgetQueryCommand ; const deleteCommand = result . deleteCommand ; const options = { queryCommand : queryCommand , matrixQueryCommand : matrixQueryCommand , widgetQueryCommand , deleteCommand : deleteCommand , connector : connector , initScript : initScript , id : session . path , } ; const handler = new VariableInspectionHandler ( options ) ; manager . addHandler ( handler ) ; consolePanel . disposed . connect ( ( ) => { delete handlers [ consolePanel . id ] ; handler . dispose ( ) ; } ) ; handler . ready . then ( ( ) => { resolve ( handler ) ; } ) ; } ) ; scripts . catch ( ( result ) => { console . log ( result ) ; const handler = new DummyHandler ( connector ) ; consolePanel . disposed . connect ( ( ) => { delete handlers [ consolePanel . id ] ; handler . dispose ( ) ; } ) ; resolve ( handler ) ; } ) ; } ) ; } } ) ; /**
         * If focus window changes, checks whether new focus widget is a console.
         * In that case, retrieves the handler associated to the console after it has been
         * initialized and updates the manager with it.
         */ labShell . currentChanged . connect ( ( sender , args ) => { const widget = args . newValue ; if ( ! widget || ! consoles . has ( widget ) ) { return ; } const future = handlers [ widget . id ] ; future . then ( ( source ) => { if ( source ) { manager . source = source ; manager . source . performInspection ( ) ; } } ) ; } ) ; app . contextMenu . addItem ( { command : CommandIDs . open , selector : '.jp-CodeConsole' , } ) ; } , } ; /**
 * An extension that registers notebooks for variable inspection.
 */ const notebooks = { id : '@lckr/jupyterlab-variableinspector:notebooks' , requires : [ IVariableInspectorManager , INotebookTracker , ILabShell ] , autoStart : true , activate : ( app , manager , notebooks , labShell ) : void => { const handlers = { } ; /**
         * Subscribes to the creation of new notebooks. If a new notebook is created, build a new handler for the notebook.
         * Adds a promise for a instanced handler to the 'handlers' collection.
         */ notebooks . widgetAdded . connect ( ( sender , nbPanel ) => { handlers [ nbPanel . id ] = new Promise ( ( resolve , reject ) => { const session = nbPanel . sessionContext ; const connector = new KernelConnector ( { session } ) ; const rendermime = nbPanel . content . rendermime ; const scripts = connector . ready . then ( ( ) => { return connector . kernelLanguage . then ( ( lang ) => { return Languages . getScript ( lang ) ; } ) ; } ) ; scripts . then ( ( result ) => { const initScript = result . initScript ; const queryCommand = result . queryCommand ; const matrixQueryCommand = result . matrixQueryCommand ; const widgetQueryCommand = result . widgetQueryCommand ; const deleteCommand = result . deleteCommand ; const options = { queryCommand : queryCommand , matrixQueryCommand : matrixQueryCommand , widgetQueryCommand , deleteCommand : deleteCommand , connector : connector , rendermime , initScript : initScript , id : session . path , } ; const handler = new VariableInspectionHandler ( options ) ; manager . addHandler ( handler ) ; nbPanel . disposed . connect ( ( ) => { delete handlers [ nbPanel . id ] ; handler . dispose ( ) ; } ) ; handler . ready . then ( ( ) => { resolve ( handler ) ; } ) ; } ) ; scripts . catch ( ( result ) => { reject ( result ) ; } ) ; } ) ; } ) ; /**
         * If focus window changes, checks whether new focus widget is a notebook.
         * In that case, retrieves the handler associated to the notebook after it has been
         * initialized and updates the manager with it.
         */ labShell . currentChanged . connect ( ( sender , args ) => { const widget = args . newValue ; if ( ! widget || ! notebooks . has ( widget ) ) { return ; } const future = handlers [ widget . id ] ; future . then ( ( source ) => { if ( source ) { manager . source = source ; manager . source . performInspection ( ) ; } } ) ; } ) ; app . contextMenu . addItem ( { command : CommandIDs . open , selector : '.jp-Notebook' , } ) ; } , } ; /**
 * Export the plugins as default.
 */ const plugins = [ variableinspector , consoles , notebooks , ] ; export default plugins ;