Skip to content

Commit 9aa0620

Browse files
authored
Merge pull request #574 from martinRenou/add_save
Add save to HTML method
2 parents bff337f + d923679 commit 9aa0620

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

docs/source/api_reference/basemaps.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _basemaps-section:
2+
13
Basemaps
24
========
35

docs/source/api_reference/map.rst

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,51 @@
11
Map
22
===
33

4-
Example
5-
-------
4+
Usage
5+
-----
66

77
.. jupyter-execute::
88

99
from ipyleaflet import Map, basemaps, basemap_to_tiles
1010

1111
m = Map(
12-
layers=(basemap_to_tiles(basemaps.NASAGIBS.ModisTerraTrueColorCR, "2017-04-08"), ),
12+
basemap=basemap_to_tiles(basemaps.NASAGIBS.ModisTerraTrueColorCR, "2017-04-08"),
1313
center=(52.204793, 360.121558),
1414
zoom=4
1515
)
1616

1717
m
1818

19+
You can find the list of available basemaps in the :ref:`basemaps-section` page.
20+
21+
You can add multiple layers and controls to the map, using the ``add_layer``/``add_control`` methods. All those layers and controls are widgets themselves. So you can dynamically update their attributes from Python or by interacting with the map on the page (see :ref:`usage-section`)
22+
23+
.. jupyter-execute::
24+
25+
from ipyleaflet import Map, Marker, basemaps, basemap_to_tiles
26+
27+
m = Map(
28+
basemap=basemap_to_tiles(basemaps.NASAGIBS.ModisTerraTrueColorCR, "2017-04-08"),
29+
center=(52.204793, 360.121558),
30+
zoom=4
31+
)
32+
33+
m.add_layer(Marker(location=(52.204793, 360.121558)))
34+
35+
m
36+
37+
Save to HTML
38+
------------
39+
40+
You can save the ``Map`` and all its layers and controls to an HTML page using the ``save`` method:
41+
42+
.. code::
43+
44+
m.save('my_map.html', title='My Map')
45+
46+
.. note::
47+
The saved file is a static HTML page, so there is no possible interaction with Python anymore. This means that all the Python callbacks you defined (`e.g.` on marker move) cannot be executed. If you want to serve the ``Map`` widget to an HTML page while keeping a Python kernel alive on the server, you might want to look at `Voilà <https://voila.readthedocs.io>`_.
48+
1949

2050
Attributes
2151
----------
@@ -65,4 +95,5 @@ add_control Control instance Add a new control t
6595
remove_control Control instance Remove a control from the map
6696
clear_controls Remove all controls from the map
6797
on_interaction callable object Add a callback on interaction
98+
save output file Save the map to an HTML file
6899
================ ===================================== ===

docs/source/usage.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _usage-section:
2+
13
Usage
24
=====
35

ipyleaflet/leaflet.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
)
1313

1414
from ipywidgets.widgets.trait_types import InstanceDict
15+
from ipywidgets.embed import embed_minimal_html
1516

1617
from traitlets import (
1718
CFloat, Float, Unicode, Int, Tuple, List, Instance, Bool, Dict, Enum,
@@ -1181,6 +1182,18 @@ def remove_control(self, control):
11811182
def clear_controls(self):
11821183
self.controls = ()
11831184

1185+
def save(self, outfile, **kwargs):
1186+
"""Save the Map to an .html file.
1187+
1188+
Parameters
1189+
----------
1190+
outfile: str or file-like object
1191+
The file to write the HTML output to.
1192+
kwargs: keyword-arguments
1193+
Extra parameters to pass to the ipywidgets.embed.embed_minimal_html function.
1194+
"""
1195+
embed_minimal_html(outfile, views=[self], **kwargs)
1196+
11841197
def __iadd__(self, item):
11851198
if isinstance(item, Layer):
11861199
self.add_layer(item)

0 commit comments

Comments
 (0)