Skip to content

Commit 3676318

Browse files
author
Martin Journois
committed
Add save and create_map methods
1 parent f3df0dd commit 3676318

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

folium/element.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import json
1414
import base64
1515

16-
from .six import urlopen
16+
from .six import urlopen, text_type, binary_type
1717
from .utilities import _camelify, _parse_size
1818

1919
class Element(object):
@@ -80,6 +80,27 @@ def render(self, **kwargs):
8080
"""TODO : docstring here."""
8181
return self._template.render(this=self, kwargs=kwargs)
8282

83+
def save(self, outfile, close_file=True, **kwargs):
84+
"""Saves an Element into a file.
85+
86+
Parameters
87+
----------
88+
outfile : str or file object
89+
The file (or filename) where you want to ouput the html.
90+
close_file : bool, default True
91+
Whether the file has to be closed after write.
92+
"""
93+
if isinstance(outfile,text_type) or isinstance(outfile,binary_type):
94+
fid = open(outfile, 'wb')
95+
else:
96+
fid = outfile
97+
98+
root = self.get_root()
99+
html = root.render(**kwargs)
100+
fid.write(html.encode('utf8'))
101+
if close_file:
102+
fid.close()
103+
83104
class Link(Element):
84105
def get_code(self):
85106
if self.code is None:

folium/folium.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,23 @@ class Map(_Map):
3030
"""This class inherits from the map.Map object in order to provide bindings to
3131
former folium API.
3232
"""
33+
def create_map(self, path='map.html', plugin_data_out=True, template=None):
34+
"""Write Map output to HTML.
35+
36+
Parameters:
37+
-----------
38+
path: string, default 'map.html'
39+
Path for HTML output for map
40+
plugin_data_out: boolean, default True
41+
Deprecated, not used anymore
42+
template: string, default None
43+
Deprecated, not used anymore
44+
"""
45+
warnings.warn("%s is deprecated. Use %s instead" % ("Map.create_map",
46+
"Map.save"),
47+
FutureWarning, stacklevel=2)
48+
self.save(path)
49+
3350
def add_wms_layer(self, wms_name=None, wms_url=None, wms_format=None,
3451
wms_layers=None, wms_transparent=True):
3552
"""Adds a simple tile layer.

tests/test_folium.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,7 @@ def test_create_map(self):
563563

564564
# Test write.
565565
map._parent.render()
566+
map.save('map.html')
566567

567568
def test_line(self):
568569
"""Test line."""

0 commit comments

Comments
 (0)