44 imageIcon ,
55} from '@jupyterlab/ui-components' ;
66
7+ import { requestAPI } from './request' ;
8+
79class ImageCaptionWidget extends Widget {
810 // Initialization
911 constructor ( ) {
@@ -14,7 +16,40 @@ class ImageCaptionWidget extends Widget {
1416 const hello = document . createElement ( 'p' ) ;
1517 hello . innerHTML = "Hello, world!" ;
1618 this . node . appendChild ( hello ) ;
19+
20+ const center = document . createElement ( 'center' ) ;
21+ this . node . appendChild ( center ) ;
22+
23+ // Put an <img> tag into the <center> tag, and also save it as a class
24+ // attribute so we can update it later.
25+ this . img = document . createElement ( 'img' ) ;
26+ center . appendChild ( this . img ) ;
27+
28+ // Do the same for a caption!
29+ this . caption = document . createElement ( 'p' ) ;
30+ center . appendChild ( this . caption ) ;
31+
32+ // Initialize the image from the server extension
33+ this . load_image ( ) ;
34+ }
35+
36+ // Fetch data from the server extension and save the results to img and
37+ // caption class attributes
38+ load_image ( ) : void {
39+ requestAPI < any > ( 'random-image-caption' )
40+ . then ( data => {
41+ console . log ( data ) ;
42+ this . img . src = `data:image/jpeg;base64, ${ data . b64_bytes } ` ;
43+ this . caption . innerHTML = data . caption ;
44+ } )
45+ . catch ( reason => {
46+ console . error ( `Error fetching image data.\n${ reason } ` ) ;
47+ } ) ;
1748 }
49+
50+ // Information about class attributes for the type checker
51+ img : HTMLImageElement ;
52+ caption : HTMLParagraphElement ;
1853}
1954
2055export class ImageCaptionMainAreaWidget extends MainAreaWidget < ImageCaptionWidget > {
0 commit comments