@@ -4,25 +4,56 @@ const transformime = require('transformime');
44const MarkdownTransform = require ( 'transformime-marked' ) ;
55const transform = transformime . createTransform ( [ MarkdownTransform ] ) as Function ;
66
7- ( window as any ) . initializeResults = ( rootDirName ) => {
8- const data = ( window as any ) . JUPYTER_DATA as any [ ] ;
9- const __dirname = rootDirName ;
10- data . forEach ( data => {
11- if ( typeof data [ 'text/html' ] === 'string' ) {
12- data [ 'text/html' ] = data [ 'text/html' ] . replace ( / < \/ s c r i p t s > / g, '</script>' ) ;
7+ function displayData ( data : any , whiteBg : boolean ) : Promise < HTMLElement > {
8+ if ( typeof data [ 'text/html' ] === 'string' ) {
9+ data [ 'text/html' ] = data [ 'text/html' ] . replace ( / < \/ s c r i p t s > / g, '</script>' ) ;
10+ }
11+ return transform ( data ) . then ( result => {
12+ // If dealing with images add them inside a div with white background
13+ if ( whiteBg === true || Object . keys ( data ) . some ( key => key . startsWith ( 'image/' ) ) ) {
14+ const div = document . createElement ( 'div' ) ;
15+ div . style . backgroundColor = 'white' ;
16+ div . style . display = 'inline-block' ;
17+ div . appendChild ( result . el ) ;
18+ document . body . appendChild ( div ) ;
19+ return div ;
20+ }
21+ else {
22+ document . body . appendChild ( result . el ) ;
23+ return result . el ;
24+ }
25+ } ) ;
26+ }
27+
28+ ( window as any ) . initializeResults = ( rootDirName : string , port : number , whiteBg : boolean ) => {
29+ const results = ( window as any ) . JUPYTER_DATA as any [ ] ;
30+ ( window as any ) . __dirname = rootDirName ;
31+ try {
32+ if ( typeof port === 'number' && port > 0 ) {
33+ var socket = ( window as any ) . io . connect ( 'http://localhost:' + port ) ;
34+ socket . on ( 'results' , function ( results : any [ ] ) {
35+ const promises = results . map ( data => displayData ( data , whiteBg ) ) ;
36+ Promise . all < HTMLElement > ( promises ) . then ( elements => {
37+ // Bring the first item into view
38+ if ( elements . length > 0 ) {
39+ elements [ 0 ] . scrollIntoView ( true ) ;
40+ }
41+ } ) ;
42+ } ) ;
43+ }
44+ }
45+ catch ( ex ) {
46+ const errorDiv = document . createElement ( 'div' ) ;
47+ errorDiv . innerHTML = 'Initializing live updates for results failed with the following error:\n' + ex . message ;
48+ errorDiv . style . color = 'red' ;
49+ document . body . appendChild ( errorDiv ) ;
50+ }
51+
52+ const promises = results . map ( data => displayData ( data , whiteBg ) ) ;
53+ Promise . all < HTMLElement > ( promises ) . then ( elements => {
54+ // Bring the first item into view
55+ if ( elements . length > 0 ) {
56+ elements [ 0 ] . scrollIntoView ( true ) ;
1357 }
14- transform ( data ) . then ( result => {
15- // If dealing with images add them inside a div with white background
16- if ( Object . keys ( data ) . some ( key => key . startsWith ( 'image/' ) ) ) {
17- const div = document . createElement ( 'div' ) ;
18- div . style . backgroundColor = 'white' ;
19- div . style . display = 'inline-block' ;
20- div . appendChild ( result . el ) ;
21- document . body . appendChild ( div ) ;
22- }
23- else {
24- document . body . appendChild ( result . el ) ;
25- }
26- } ) ;
2758 } ) ;
2859} ;
0 commit comments