11import  { 
2+   ILayoutRestorer , 
23  JupyterFrontEnd , 
34  JupyterFrontEndPlugin 
45}  from  '@jupyterlab/application' ; 
5- import  {  ICommandPalette  }  from  '@jupyterlab/apputils' ; 
6+ import  { 
7+   ICommandPalette , 
8+   WidgetTracker 
9+ }  from  '@jupyterlab/apputils' ; 
610import  {  ILauncher  }  from  '@jupyterlab/launcher' ; 
711import  {  imageIcon  }  from  '@jupyterlab/ui-components' ; 
812
@@ -17,12 +21,14 @@ const plugin: JupyterFrontEndPlugin<void> = {
1721  description : 'A JupyterLab extension that displays a random image and caption.' , 
1822  autoStart : true , 
1923  requires : [ ICommandPalette ,  ILauncher ] ,   // dependencies of our extension 
24+   optional : [ ILayoutRestorer ] , 
2025  activate : ( 
2126    app : JupyterFrontEnd , 
2227    // The activation method receives dependencies in the order they are specified in 
2328    // the "requires" parameter above: 
2429    palette : ICommandPalette , 
25-     launcher : ILauncher 
30+     launcher : ILauncher , 
31+     restorer : ILayoutRestorer  |  null 
2632  )  =>  { 
2733    console . log ( 'JupyterLab extension jupytercon2025-extension-workshop is activated!' ) ; 
2834
@@ -36,13 +42,23 @@ const plugin: JupyterFrontEndPlugin<void> = {
3642        ) ; 
3743      } ) ; 
3844
45+     // Track widget state 
46+     const  tracker_namespace  =  'jupytercon2025-extension-workshop' ; 
47+     const  tracker  =  new  WidgetTracker < ImageCaptionMainAreaWidget > ( { 
48+       namespace : tracker_namespace 
49+     } ) ; 
50+ 
3951    //Register a new command: 
4052    const  command_id  =  'image-caption:open' ; 
4153    app . commands . addCommand ( command_id ,  { 
4254      execute : ( )  =>  { 
4355        // When the command is executed, create a new instance of our widget 
4456        const  widget  =  new  ImageCaptionMainAreaWidget ( ) ; 
4557
58+         if  ( ! tracker . has ( widget ) )  { 
59+           tracker . add ( widget ) ; 
60+         } 
61+ 
4662        // Then add it to the main area: 
4763        app . shell . add ( widget ,  'main' ) ; 
4864      } , 
@@ -52,6 +68,14 @@ const plugin: JupyterFrontEndPlugin<void> = {
5268
5369    palette . addItem ( {  command : command_id ,  category : 'Tutorial'  } ) ; 
5470    launcher . add ( {  command : command_id  } ) ; 
71+ 
72+     // Restore widget state 
73+     if  ( restorer )  { 
74+       restorer . restore ( tracker ,  { 
75+         command : command_id , 
76+         name : ( )  =>  tracker_namespace 
77+       } ) ; 
78+     } 
5579  } 
5680} ; 
5781
0 commit comments