@@ -42,32 +42,62 @@ export class ExampleModel extends DOMWidgetModel {
4242export class ExampleView extends DOMWidgetView {
4343 static tracker : INotebookTracker ;
4444
45+ private select ! : HTMLSelectElement ;
4546 private button ! : HTMLButtonElement ;
4647 private textarea ! : HTMLTextAreaElement ;
4748
4849 render ( ) {
50+ // TODO: Type as GeoJSON (@types/geojson)
51+ const adminBoundariesGeoJson : any = JSON . parse (
52+ this . model . get ( 'admin_boundaries_geojson' )
53+ ) ;
54+ const countries = adminBoundariesGeoJson [ 'features' ]
55+ . map ( ( f : any ) => f [ 'properties' ] [ 'NAME' ] as string )
56+ . sort ( ) ;
57+
4958 this . textarea = document . createElement ( 'textarea' ) ;
5059 this . textarea . value = this . model . get ( 'value' ) ;
5160
5261 this . button = document . createElement ( 'button' ) ;
53- this . button . textContent = 'test' ; //this.model.get('value');
62+ this . button . textContent = 'Export to Python code cell' ;
63+
64+ this . select = document . createElement ( 'select' ) ;
65+ for ( const country of countries ) {
66+ const option = document . createElement ( 'option' ) ;
67+ option . textContent = country ;
68+ this . select . appendChild ( option ) ;
69+ }
70+
71+ const notebook = ExampleView . tracker ?. currentWidget ?. content ;
5472
5573 this . button . onclick = ( ) => {
56- const notebook = ExampleView . tracker ?. currentWidget ?. content ;
5774 if ( ! notebook ?. model ) {
5875 return ;
59- }
76+ } // Should never happen; just a typeguard
77+
78+ const geoJson = adminBoundariesGeoJson [ 'features' ] . find (
79+ ( f : any ) => f [ 'properties' ] [ 'NAME' ] === this . select . value
80+ ) ;
81+ const codeCellContents =
82+ `print('${ String ( this . textarea . value ) } ')\n` +
83+ 'from ipyleaflet import Map, GeoJSON\n' +
84+ 'import json\n' +
85+ 'm = Map()\n' +
86+ `m.add(GeoJSON(data=json.loads("""${ JSON . stringify ( geoJson ) } """)))\n` +
87+ 'm' ;
88+
6089 notebook . model . sharedModel . insertCell (
6190 notebook . widgets . findIndex ( cell => cell . node . contains ( this . el ) ) + 1 ,
6291 {
6392 cell_type : 'code' ,
64- source : "print('" + String ( this . textarea . value ) + "')" ,
93+ source : codeCellContents ,
6594 metadata : { }
6695 }
6796 ) ;
6897 } ;
6998
7099 this . el . appendChild ( this . textarea ) ;
100+ this . el . appendChild ( this . select ) ;
71101 this . el . appendChild ( this . button ) ;
72102 this . el . classList . add ( 'custom-widget' ) ;
73103 this . model . on ( 'change:value' , this . value_changed , this ) ;
0 commit comments