Skip to content

Add save and create_map methods #227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion folium/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import json
import base64

from .six import urlopen
from .six import urlopen, text_type, binary_type
from .utilities import _camelify, _parse_size

class Element(object):
Expand Down Expand Up @@ -80,6 +80,27 @@ def render(self, **kwargs):
"""TODO : docstring here."""
return self._template.render(this=self, kwargs=kwargs)

def save(self, outfile, close_file=True, **kwargs):
"""Saves an Element into a file.

Parameters
----------
outfile : str or file object
The file (or filename) where you want to ouput the html.
close_file : bool, default True
Whether the file has to be closed after write.
"""
if isinstance(outfile,text_type) or isinstance(outfile,binary_type):
fid = open(outfile, 'wb')
else:
fid = outfile

root = self.get_root()
html = root.render(**kwargs)
fid.write(html.encode('utf8'))
if close_file:
fid.close()

class Link(Element):
def get_code(self):
if self.code is None:
Expand Down
17 changes: 17 additions & 0 deletions folium/folium.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ class Map(_Map):
"""This class inherits from the map.Map object in order to provide bindings to
former folium API.
"""
def create_map(self, path='map.html', plugin_data_out=True, template=None):
"""Write Map output to HTML.

Parameters:
-----------
path: string, default 'map.html'
Path for HTML output for map
plugin_data_out: boolean, default True
Deprecated, not used anymore
template: string, default None
Deprecated, not used anymore
"""
warnings.warn("%s is deprecated. Use %s instead" % ("Map.create_map",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! I was going to suggest that. I like save or save_map better than create_map.

"Map.save"),
FutureWarning, stacklevel=2)
self.save(path)

def add_wms_layer(self, wms_name=None, wms_url=None, wms_format=None,
wms_layers=None, wms_transparent=True):
"""Adds a simple tile layer.
Expand Down
1 change: 1 addition & 0 deletions tests/test_folium.py
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ def test_create_map(self):

# Test write.
map._parent.render()
map.save('map.html')

def test_line(self):
"""Test line."""
Expand Down