Skip to content

pep8 and typos #248

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 13, 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
89 changes: 48 additions & 41 deletions folium/element.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
from uuid import uuid4

from jinja2 import Environment, PackageLoader, Template
ENV = Environment(loader=PackageLoader('folium', 'templates'))
from collections import OrderedDict
import json
import base64

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


ENV = Environment(loader=PackageLoader('folium', 'templates'))


class Element(object):
"""Basic Element object that does nothing.
Other Elements may inherit from this one."""
Expand All @@ -35,7 +38,7 @@ def __init__(self, template=None, template_name=None):
""")

def get_name(self):
return _camelify(self._name) + '_' +self._id
return _camelify(self._name) + '_' + self._id

def add_children(self, child, name=None, index=None):
"""Add a children."""
Expand All @@ -44,8 +47,9 @@ def add_children(self, child, name=None, index=None):
if index is None:
self._children[name] = child
else:
items = [item for item in self._children.items() if item[0] != name]
items.insert(int(index),(name,child))
items = [item for item in self._children.items()
if item[0] != name]
items.insert(int(index), (name, child))
self._children = items
child._parent = self

Expand All @@ -62,8 +66,8 @@ def to_dict(self, depth=-1, ordered=True, **kwargs):
out['name'] = self._name
out['id'] = self._id
if depth != 0:
out['children'] = dict_fun([(name, child.to_dict(depth=depth-1))\
for name,child in self._children.items()])
out['children'] = dict_fun([(name, child.to_dict(depth=depth-1))
for name, child in self._children.items()])
return out

def to_json(self, depth=-1, **kwargs):
Expand All @@ -86,11 +90,11 @@ def save(self, outfile, close_file=True, **kwargs):
Parameters
----------
outfile : str or file object
The file (or filename) where you want to ouput the html.
The file (or filename) where you want to output 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):
if isinstance(outfile, text_type) or isinstance(outfile, binary_type):
fid = open(outfile, 'wb')
else:
fid = outfile
Expand All @@ -101,16 +105,19 @@ def save(self, outfile, close_file=True, **kwargs):
if close_file:
fid.close()


class Link(Element):
def get_code(self):
if self.code is None:
self.code = urlopen(self.url).read()
return self.code

def to_dict(self, depth=-1, **kwargs):
out = super(Link, self).to_dict(depth=-1, **kwargs)
out['url'] = self.url
return out


class JavascriptLink(Link):
def __init__(self, url, download=False):
"""Create a JavascriptLink object based on a url.
Expand All @@ -136,6 +143,7 @@ def __init__(self, url, download=False):
{% endif %}
""")


class CssLink(Link):
def __init__(self, url, download=False):
"""Create a CssLink object based on a url.
Expand Down Expand Up @@ -195,6 +203,7 @@ def __init__(self, url, download=False):
"https://raw.githubusercontent.com/python-visualization/folium/master/folium/templates/leaflet.awesome.rotate.css"),
]


class Figure(Element):
def __init__(self, width="100%", height=None, ratio="60%", figsize=None):
"""Create a Figure object, to plot things into it.
Expand All @@ -216,7 +225,7 @@ def __init__(self, width="100%", height=None, ratio="60%", figsize=None):
super(Figure, self).__init__()
self._name = 'Figure'
self.header = Element()
self.html = Element()
self.html = Element()
self.script = Element()

self.header._parent = self
Expand Down Expand Up @@ -246,7 +255,7 @@ def __init__(self, width="100%", height=None, ratio="60%", figsize=None):
# Create the meta tag
self.header.add_children(Element(
'<meta http-equiv="content-type" content="text/html; charset=UTF-8" />'),
name='meta_http')
name='meta_http')

# Import Javascripts
for name, url in _default_js:
Expand Down Expand Up @@ -296,7 +305,7 @@ def _repr_html_(self, **kwargs):
----------
"""
html = self.render(**kwargs)
html = "data:text/html;base64,"+base64.b64encode(html.encode('utf8')).decode('utf8')
html = "data:text/html;base64," + base64.b64encode(html.encode('utf8')).decode('utf8')

if self.height is None:
iframe = """
Expand All @@ -310,22 +319,19 @@ def _repr_html_(self, **kwargs):
ratio=self.ratio,
)
else:
iframe = '<iframe src="{html}" width="{width}" height="{height}"></iframe>'\
.format(\
html = html,
width = self.width,
height= self.height,
)
iframe = ('<iframe src="{html}" width="{width}" '
'height="{height}"></iframe>').format
iframe = iframe(html=html, width=self.width, height=self.height)
return iframe

def add_subplot(self, x,y,n,margin=0.05):
def add_subplot(self, x, y, n, margin=0.05):
width = 1./y
height = 1./x
left = ((n-1)%y)*width
left = ((n-1) % y)*width
top = ((n-1)//y)*height

left = left+width*margin
top = top+height*margin
top = top+height*margin
width = width*(1-2.*margin)
height = height*(1-2.*margin)

Expand All @@ -338,39 +344,40 @@ def add_subplot(self, x,y,n,margin=0.05):
self.add_children(div)
return div


class Html(Element):
def __init__(self, data, width="100%", height="100%"):
"""TODO : docstring here"""
super(Html, self).__init__()
self._name = 'Html'
self.data = data

self.width = _parse_size(width)
self.height = _parse_size(height)
self.width = _parse_size(width)
self.height = _parse_size(height)

self._template = Template(u"""
<div id="{{this.get_name()}}"
style="width: {{this.width[0]}}{{this.width[1]}}; height: {{this.height[0]}}{{this.height[1]}};">
{{this.data}}</div>
""")


class Div(Figure):
def __init__(self, width='100%', height='100%',
left="0%", top="0%", position='relative'):
"""Create a Map with Folium and Leaflet.js
"""
"""Create a Map with Folium and Leaflet.js."""
super(Figure, self).__init__()
self._name = 'Div'

# Size Parameters.
self.width = _parse_size(width)
self.width = _parse_size(width)
self.height = _parse_size(height)
self.left = _parse_size(left)
self.top = _parse_size(top)
self.top = _parse_size(top)
self.position = position

self.header = Element()
self.html = Element("""
self.html = Element("""
{% for name, element in this._children.items() %}
{{element.render(**kwargs)}}
{% endfor %}
Expand Down Expand Up @@ -404,8 +411,8 @@ def get_root(self):
def render(self, **kwargs):
"""TODO : docstring here."""
figure = self._parent
assert isinstance(figure,Figure), ("You cannot render this Element "
"if it's not in a Figure.")
assert isinstance(figure, Figure), ("You cannot render this Element "
"if it's not in a Figure.")

for name, element in self._children.items():
element.render(**kwargs)
Expand All @@ -416,24 +423,23 @@ def render(self, **kwargs):
for name, element in self.script._children.items():
figure.script.add_children(element, name=name)

header = self._template.module.__dict__.get('header',None)
header = self._template.module.__dict__.get('header', None)
if header is not None:
figure.header.add_children(Element(header(self, kwargs)),
name=self.get_name())

html = self._template.module.__dict__.get('html',None)
html = self._template.module.__dict__.get('html', None)
if html is not None:
figure.html.add_children(Element(html(self, kwargs)),
name=self.get_name())
name=self.get_name())

script = self._template.module.__dict__.get('script',None)
script = self._template.module.__dict__.get('script', None)
if script is not None:
figure.script.add_children(Element(script(self, kwargs)),
name=self.get_name())

def _repr_html_(self, **kwargs):
"""Displays the Div in a Jupyter notebook.
"""
"""Displays the Div in a Jupyter notebook."""
if self._parent is None:
self.add_to(Figure())
out = self._parent._repr_html_(**kwargs)
Expand All @@ -442,6 +448,7 @@ def _repr_html_(self, **kwargs):
out = self._parent._repr_html_(**kwargs)
return out


class MacroElement(Element):
"""This is a parent class for Elements defined by a macro template.
To compute your own element, all you have to do is:
Expand Down Expand Up @@ -469,20 +476,20 @@ def __init__(self):

def render(self, **kwargs):
figure = self.get_root()
assert isinstance(figure,Figure), ("You cannot render this Element "
"if it's not in a Figure.")
assert isinstance(figure, Figure), ("You cannot render this Element "
"if it's not in a Figure.")

header = self._template.module.__dict__.get('header',None)
header = self._template.module.__dict__.get('header', None)
if header is not None:
figure.header.add_children(Element(header(self, kwargs)),
name=self.get_name())

html = self._template.module.__dict__.get('html',None)
html = self._template.module.__dict__.get('html', None)
if html is not None:
figure.html.add_children(Element(html(self, kwargs)),
name=self.get_name())
name=self.get_name())

script = self._template.module.__dict__.get('script',None)
script = self._template.module.__dict__.get('script', None)
if script is not None:
figure.script.add_children(Element(script(self, kwargs)),
name=self.get_name())
Expand Down
Loading