Skip to content

Fix issue #219 #221

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
Oct 30, 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
78 changes: 45 additions & 33 deletions folium/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,23 @@ def __init__(self, url, download=False):
]

class Figure(Element):
def __init__(self, figsize=(17,10)):
def __init__(self, width="100%", height=None, ratio="60%", figsize=None):
"""Create a Figure object, to plot things into it.

Parameters
----------
width : str, default "100%"
The width of the Figure. It may be a percentage or a distance (like "300px").
height : str, default None
The height of the Figure. It may be a percentage or a distance (like "300px").
ratio : str, default "60%"
A percentage defining the aspect ratio of the Figure.
It will be ignored if height is not None.
figsize : tuple of two int, default None
If you're a matplotlib addict, you can overwrite width and height.
Values will be converted into pixels in using 60 dpi.
For example figsize=(10,5) wil result in width="600px", height="300px".
"""
super(Figure, self).__init__()
self._name = 'Figure'
self.header = Element()
Expand All @@ -186,7 +202,12 @@ def __init__(self, figsize=(17,10)):
self.html._parent = self
self.script._parent = self

self.figsize = figsize
self.width = width
self.height = height
self.ratio = ratio
if figsize is not None:
self.width = str(60*figsize[0])+'px'
self.height = str(60*figsize[1])+'px'

self._template = Template(u"""
<!DOCTYPE html>
Expand Down Expand Up @@ -252,25 +273,27 @@ def _repr_html_(self, **kwargs):

Parameters
----------
self : folium.Map object
The map you want to display

figsize : tuple of length 2, default (17,10)
The size of the output you expect in inches.
Output is 60dpi so that the output has same size as a
matplotlib figure with the same figsize.

"""
html = self.render(**kwargs)

width, height = self.figsize

iframe = '<iframe src="{html}" width="{width}px" height="{height}px"></iframe>'\
html = "data:text/html;base64,"+base64.b64encode(html.encode('utf8')).decode('utf8')

if self.height is None:
iframe = """
<div style="width:{width};">
<div style="position:relative;width:100%;height:0;padding-bottom:{ratio};">
<iframe src="{html}" style="position:absolute;width:100%;height:100%;left:0;top:0;">
</iframe>
</div></div>""".format(
html=html,
width=self.width,
ratio=self.ratio,
)
else:
iframe = '<iframe src="{html}" width="{width}" height="{height}"></iframe>'\
.format(\
html = "data:text/html;base64,"+base64.b64encode(html.encode('utf8')).decode('utf8'),
#html = self.HTML.replace('"','&quot;'),
width = int(60.*width),
height= int(60.*height),
html = html,
width = self.width,
height= self.height,
)
return iframe

Expand Down Expand Up @@ -387,26 +410,15 @@ def render(self, **kwargs):
figure.script.add_children(Element(script(self, kwargs)),
name=self.get_name())

def _repr_html_(self, figsize=(17,10), **kwargs):
"""Displays the Map in a Jupyter notebook.

Parameters
----------
self : folium.Map object
The map you want to display

figsize : tuple of length 2, default (17,10)
The size of the output you expect in inches.
Output is 60dpi so that the output has same size as a
matplotlib figure with the same figsize.

def _repr_html_(self, **kwargs):
"""Displays the Div in a Jupyter notebook.
"""
if self._parent is None:
self.add_to(Figure())
out = self._parent._repr_html_(figsize=figsize, **kwargs)
out = self._parent._repr_html_(**kwargs)
self._parent = None
else:
out = self._parent._repr_html_(figsize=figsize, **kwargs)
out = self._parent._repr_html_(**kwargs)
return out

class MacroElement(Element):
Expand Down
17 changes: 3 additions & 14 deletions folium/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,15 @@ def __init__(self, location=None, width='100%', height='100%',
{% endmacro %}
""")

def _repr_html_(self, figsize=(17,10), **kwargs):
def _repr_html_(self, **kwargs):
"""Displays the Map in a Jupyter notebook.

Parameters
----------
self : folium.Map object
The map you want to display

figsize : tuple of length 2, default (17,10)
The size of the output you expect in inches.
Output is 60dpi so that the output has same size as a
matplotlib figure with the same figsize.

"""
if self._parent is None:
self.add_to(Figure())
out = self._parent._repr_html_(figsize=figsize, **kwargs)
out = self._parent._repr_html_(**kwargs)
self._parent = None
else:
out = self._parent._repr_html_(figsize=figsize, **kwargs)
out = self._parent._repr_html_(**kwargs)
return out

def add_tile_layer(self, tiles='OpenStreetMap', name=None,
Expand Down
8 changes: 6 additions & 2 deletions folium/six.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import sys
import urllib

PY3 = sys.version_info[0] == 3

Expand All @@ -17,4 +16,9 @@ def iteritems(d, **kw):
def iteritems(d, **kw):
return iter(d.iteritems(**kw))

urlopen = urllib.request.urlopen if PY3 else urllib.urlopen
if PY3:
import urllib.request
urlopen = urllib.request.urlopen
else:
import urllib
urlopen = urllib.urlopen