10
10
from ipywidgets import DOMWidget , Widget , widget_serialization
11
11
from traitlets import Unicode , List , Instance , CFloat , Bool , Dict , Int , Float
12
12
from ._frontend import module_name , module_version
13
+ import requests
14
+ import unicodedata
15
+ import configparser
16
+
13
17
14
18
def_loc = [0.0 , 0.0 ]
15
19
@@ -166,4 +170,73 @@ def remove_control(self, control):
166
170
167
171
168
172
def clear_layers (self ):
169
- self .layers = []
173
+ self .layers = []
174
+
175
+ def on_msg (self , callback ):
176
+ """Register a callback for receiving messages from the frontend."""
177
+ self ._msg_callback = callback
178
+
179
+ def _handle_msg (self , msg ):
180
+ """Handle a message received from the frontend."""
181
+ if hasattr (self , '_msg_callback' ):
182
+ content = msg .get ('content' , {})
183
+ buffers = msg .get ('buffers' , [])
184
+ self ._msg_callback (self , content , buffers )
185
+
186
+ def _handle_click (self , lat , lon , api_key ):
187
+ """
188
+ Handle click events and trigger registered callbacks.
189
+ """
190
+ print (f"Handling click event at Longitude: { lon } , Latitude: { lat } " )
191
+ country = self .get_country_from_coordinates_geoapify (lat , lon , api_key )
192
+ self .clicked_country = country
193
+ print (f"Country: { country } " )
194
+
195
+ callbacks = self ._click_callbacks .get_callbacks () if self ._click_callbacks else []
196
+ for callback in callbacks :
197
+ if callable (callback ):
198
+ callback (lat , lon , country )
199
+
200
+ def on_click (self , callback , api_key ):
201
+ """
202
+ Register a callback to handle click events on the map and pass the API key.
203
+ """
204
+ self ._click_callback = callback
205
+
206
+ def handle_frontend_event (widget , content , buffers ):
207
+ """Handle the click event from the frontend."""
208
+ data = content .get ('data' , {})
209
+ method = data .get ('method' , '' )
210
+
211
+ if method == 'custom' :
212
+ event_data = data .get ('content' , {})
213
+ lon = event_data .get ('lon' )
214
+ lat = event_data .get ('lat' )
215
+
216
+ if callable (self ._click_callback ):
217
+ self ._click_callback (lat , lon , self .get_country_from_coordinates_geoapify (lat , lon , api_key ))
218
+
219
+ self .on_msg (handle_frontend_event )
220
+
221
+ def normalize_country_name (self , country_name ):
222
+ normalized_name = country_name .split (' ' )[0 ]
223
+ normalized_name = unicodedata .normalize ('NFKD' , normalized_name )
224
+ normalized_name = normalized_name .encode ('ASCII' , 'ignore' ).decode ('utf-8' )
225
+ return normalized_name
226
+
227
+ def get_country_from_coordinates_geoapify (self , lat , lon , api_key ):
228
+ url = f"https://api.geoapify.com/v1/geocode/reverse?lat={ lat } &lon={ lon } &apiKey={ api_key } "
229
+ response = requests .get (url )
230
+ data = response .json ()
231
+ features = data .get ('features' , [])
232
+
233
+ if features :
234
+ first_feature = features [0 ]
235
+ properties = first_feature .get ('properties' , {})
236
+ country = properties .get ('country' , None )
237
+ if country :
238
+ normalized_name = country .split (' ' )[0 ]
239
+ normalized_name = unicodedata .normalize ('NFKD' , normalized_name )
240
+ normalized_name = normalized_name .encode ('ASCII' , 'ignore' ).decode ('utf-8' )
241
+ return normalized_name
242
+ return "Unknown"
0 commit comments