import CodeMirror from 'codemirror' ; import 'codemirror/lib/codemirror.css' ; import 'codemirror/mode/python/python' ; import 'font-awesome/css/font-awesome.css' ; import { WidgetManager } from './manager' ; import { KernelManager , ServerConnection , KernelMessage , } from '@jupyterlab/services' ; const BASEURL = prompt ( 'Notebook BASEURL' , 'http://localhost:8888' ) ; if ( BASEURL === null ) { alert ( 'A base URL is needed to run the example!' ) ; throw new Error ( 'A base URL is needed to run the example!' ) ; } const WSURL = 'ws:' + BASEURL . split ( ':' ) . slice ( 1 ) . join ( ':' ) ; document . addEventListener ( 'DOMContentLoaded' , async function ( event ) { const connectionInfo = ServerConnection . makeSettings ( { baseUrl : BASEURL ! , wsUrl : WSURL , } ) ; const kernelManager = new KernelManager ( { serverSettings : connectionInfo } ) ; const kernel = await kernelManager . startNew ( ) ; const code = require ( '../widget_code.json' ) . join ( '\n' ) ; const inputarea = document . getElementsByClassName ( 'inputarea' ) [ 0 ] as HTMLElement ; CodeMirror ( inputarea , { value : code , mode : 'python' , tabSize : 4 , showCursorWhenSelecting : true , viewportMargin : Infinity , readOnly : true , } ) ; const widgetarea = document . getElementsByClassName ( 'widgetarea' ) [ 0 ] as HTMLElement ; const manager = new WidgetManager ( kernel ) ; const execution = kernel . requestExecute ( { code : code } ) ; execution . onIOPub = async ( msg ) : Promise < void > => { if ( KernelMessage . isDisplayDataMsg ( msg ) ) { const widgetData = msg . content . data [ 'application/vnd.jupyter.widget-view+json' ] ; if ( widgetData !== undefined && widgetData . version_major === 2 ) { const model = await manager . get_model ( widgetData . model_id ) ; if ( model !== undefined ) { manager . display_view ( manager . create_view ( model ) , widgetarea ) ; } } } } ; } ) ;