I want to export the geographic coordinates of a drawn rectangle in Folium using a button from ipywidgets. Ideally, the coordinates will be saved as a geojson file to the working directory of the Jupyter notebook I am operating from. The pre-built Export button from Folium does not serve my purpose as I want the downloaded file to be saved in my working directory (not my local computer's Downloads folder) and I want the button to be created with ipywidgets.
I have tried many different ways, but have not been able to get this to work. For reference, I am using IPython version 7.22.0, folium version 0.14.0, and ipywidgets version 7.6.3
``
import folium, folium.plugins, IPython
from IPython.display import display, clear_output
import ipywidgets as ipyw
%matplotlib inline
# Create map (with specified size) and center on USA, include draw tools for rectangle only
f = folium.Figure(width=750,height=350)
m = folium.Map(location=[39, -95],zoom_start=3).add_to(f)
folium.plugins.Geocoder(add_marker=False,collapsed=True,position='bottomleft').add_to(m)
draw = folium.plugins.Draw(draw_options={'polyline':False,'polygon':False,'rectangle':True,
'circle':False,'marker':False,'circlemarker':False},
filename='data.geojson',show_geometry_on_click=True, export=False).add_to(m)
# Add plugin for displaying lat/lon of mouse position
formatter = "function(num) {return L.Util.formatNum(num, 6) + ' º ';};"
folium.plugins.MousePosition(position='topright',separator=' | ',empty_string='NaN',lng_first=False,
num_digits=20,prefix='Coordinates:',lat_formatter=formatter,
lng_formatter=formatter).add_to(m)
# Display map on widget page
map_output = ipyw.Output(layout=ipyw.Layout(width='100%'))
with map_output: display(m)
# Create 'submit' button that will print lat/lon coordinates of drawn rectangle to the screen
button_submit = ipyw.Button(description='Export Coordinates of Rectangle',layout=ipyw.Layout(width='250px'))
button_output = ipyw.Output()
def on_button_clicked(event):
with button_output:
clear_output()
print('Output printed here...')
# Display widgets components: map on top, submit button below
button_submit.on_click(on_button_clicked)
page1 = ipyw.VBox([ipyw.HBox([map_output],layout=ipyw.Layout(justify_content='center')),
ipyw.HBox([ipyw.VBox([button_submit,button_output])],
layout=ipyw.Layout(justify_content='center'))
],layout=ipyw.Layout(display='flex',flex_flow='column',align_content='space-around',
align_items='center'))
display(page1)
``