Skip to content

Commit 618c3bd

Browse files
committed
Merge pull request #4 from BibMartin/cleanup
Cleanup Templates strings
2 parents 36e7284 + 32d641c commit 618c3bd

File tree

7 files changed

+959
-120
lines changed

7 files changed

+959
-120
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ ENV
4949
.cache
5050
data.png
5151
map.html
52+
examples/foo.html
5253

5354
# documentation builds
5455
docs/_build

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ before_install:
1616
- travis_retry conda create --yes -n test python=$PYTHON --file requirements.txt
1717
- source activate test
1818
- conda install --yes --file requirements-dev.txt;
19+
- if [[ "$PYTHON" == "3.4" ]]; then
20+
travis_retry conda install --yes nbconvert jupyter_client ipykernel;
21+
fi
1922
- travis_retry pip install
2023

2124
script:

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ It may become a HTML+JS generation library in the future.
66

77
It is based on Jinja2 only.
88

9-
**It is a draft right now!!**
9+
There's no documentation, but you can browse the
10+
[example gallery](http://nbviewer.jupyter.org/github/python-visualization/branca/tree/master/examples/).

branca/element.py

Lines changed: 55 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ def __init__(self, template=None, template_name=None):
4343
self._parent = None
4444
self._template = Template(template) if template is not None\
4545
else ENV.get_template(template_name) if template_name is not None\
46-
else Template(u"""
47-
{% for name, element in this._children.items() %}
48-
{{element.render(**kwargs)}}
49-
{% endfor %}
50-
""")
46+
else Template(
47+
"{% for name, element in this._children.items() %}\n"
48+
" {{element.render(**kwargs)}}"
49+
"{% endfor %}"
50+
)
5151

5252
def get_name(self):
5353
"""Returns a string representation of the object.
@@ -189,13 +189,13 @@ def __init__(self, url, download=False):
189189
if download:
190190
self.get_code()
191191

192-
self._template = Template(u"""
193-
{% if kwargs.get("embedded",False) %}
194-
<script>{{this.get_code()}}</script>
195-
{% else %}
196-
<script src="{{this.url}}"></script>
197-
{% endif %}
198-
""")
192+
self._template = Template(
193+
'{% if kwargs.get("embedded",False) %}'
194+
'<script>{{this.get_code()}}</script>'
195+
'{% else %}'
196+
'<script src="{{this.url}}"></script>'
197+
'{% endif %}'
198+
)
199199

200200

201201
class CssLink(Link):
@@ -215,47 +215,13 @@ def __init__(self, url, download=False):
215215
if download:
216216
self.get_code()
217217

218-
self._template = Template(u"""
219-
{% if kwargs.get("embedded",False) %}
220-
<style>{{this.get_code()}}</style>
221-
{% else %}
222-
<link rel="stylesheet" href="{{this.url}}" />
223-
{% endif %}
224-
""")
225-
226-
_default_js = [
227-
('leaflet',
228-
"https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"),
229-
('jquery',
230-
"https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"),
231-
('bootstrap',
232-
"https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"),
233-
('awesome_markers',
234-
"https://rawgithub.com/lvoogdt/Leaflet.awesome-markers/2.0/develop/dist/leaflet.awesome-markers.js"), # noqa
235-
('marker_cluster_src',
236-
"https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/leaflet.markercluster-src.js"), # noqa
237-
('marker_cluster',
238-
"https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/leaflet.markercluster.js"), # noqa
239-
]
240-
241-
_default_css = [
242-
("leaflet_css",
243-
"https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css"),
244-
("bootstrap_css",
245-
"https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css"),
246-
("bootstrap_theme_css",
247-
"https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap-theme.min.css"), # noqa
248-
("awesome_markers_font_css",
249-
"https://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css"), # noqa
250-
("awesome_markers_css",
251-
"https://rawgit.com/lvoogdt/Leaflet.awesome-markers/2.0/develop/dist/leaflet.awesome-markers.css"), # noqa
252-
("marker_cluster_default_css",
253-
"https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.Default.css"), # noqa
254-
("marker_cluster_css",
255-
"https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/0.4.0/MarkerCluster.css"), # noqa
256-
("awesome_rotate_css",
257-
"https://raw.githubusercontent.com/python-visualization/folium/master/folium/templates/leaflet.awesome.rotate.css"), # noqa
258-
]
218+
self._template = Template(
219+
'{% if kwargs.get("embedded",False) %}'
220+
'<style>{{this.get_code()}}</style>'
221+
'{% else %}'
222+
'<link rel="stylesheet" href="{{this.url}}" />'
223+
'{% endif %}'
224+
)
259225

260226

261227
class Figure(Element):
@@ -296,52 +262,24 @@ def __init__(self, width="100%", height=None, ratio="60%", figsize=None):
296262
self.width = str(60*figsize[0])+'px'
297263
self.height = str(60*figsize[1])+'px'
298264

299-
self._template = Template(u"""
300-
<!DOCTYPE html>
301-
<head>
302-
{{this.header.render(**kwargs)}}
303-
</head>
304-
<body>
305-
{{this.html.render(**kwargs)}}
306-
</body>
307-
<script>
308-
{{this.script.render(**kwargs)}}
309-
</script>
310-
""")
265+
self._template = Template(
266+
'<!DOCTYPE html>\n'
267+
'<head>'
268+
' {{this.header.render(**kwargs)}}\n'
269+
'</head>\n'
270+
'<body>'
271+
' {{this.html.render(**kwargs)}}\n'
272+
'</body>\n'
273+
'<script>'
274+
' {{this.script.render(**kwargs)}}\n'
275+
'</script>\n'
276+
)
311277

312278
# Create the meta tag.
313279
self.header.add_children(Element(
314280
'<meta http-equiv="content-type" content="text/html; charset=UTF-8" />'), # noqa
315281
name='meta_http')
316282

317-
# Import Javascripts
318-
for name, url in _default_js:
319-
self.header.add_children(JavascriptLink(url), name=name)
320-
321-
# Import Css
322-
for name, url in _default_css:
323-
self.header.add_children(CssLink(url), name=name)
324-
325-
self.header.add_children(Element("""
326-
<style>
327-
328-
html, body {
329-
width: 100%;
330-
height: 100%;
331-
margin: 0;
332-
padding: 0;
333-
}
334-
335-
#map {
336-
position:absolute;
337-
top:0;
338-
bottom:0;
339-
right:0;
340-
left:0;
341-
}
342-
</style>
343-
"""), name='css_style')
344-
345283
def to_dict(self, depth=-1, **kwargs):
346284
"""Returns a dict representation of the object."""
347285
out = super(Figure, self).to_dict(depth=depth, **kwargs)
@@ -450,11 +388,11 @@ def __init__(self, data, script=False, width="100%", height="100%"):
450388
self.width = _parse_size(width)
451389
self.height = _parse_size(height)
452390

453-
self._template = Template(u"""
454-
<div id="{{this.get_name()}}"
455-
style="width: {{this.width[0]}}{{this.width[1]}}; height: {{this.height[0]}}{{this.height[1]}};">
456-
{% if this.script %}{{this.data}}{% else %}{{this.data|e}}{% endif %}</div>
457-
""") # noqa
391+
self._template = Template(
392+
'<div id="{{this.get_name()}}" '
393+
'style="width: {{this.width[0]}}{{this.width[1]}}; height: {{this.height[0]}}{{this.height[1]}};">' # noqa
394+
'{% if this.script %}{{this.data}}{% else %}{{this.data|e}}{% endif %}</div>'
395+
) # noqa
458396

459397

460398
class Div(Figure):
@@ -487,33 +425,31 @@ def __init__(self, width='100%', height='100%',
487425
self.position = position
488426

489427
self.header = Element()
490-
self.html = Element("""
491-
{% for name, element in this._children.items() %}
492-
{{element.render(**kwargs)}}
493-
{% endfor %}
494-
""")
428+
self.html = Element(
429+
'{% for name, element in this._children.items() %}'
430+
'{{element.render(**kwargs)}}'
431+
'{% endfor %}'
432+
)
495433
self.script = Element()
496434

497435
self.header._parent = self
498436
self.html._parent = self
499437
self.script._parent = self
500438

501-
self._template = Template(u"""
502-
{% macro header(this, kwargs) %}
503-
<style> #{{this.get_name()}} {
504-
position : {{this.position}};
505-
width : {{this.width[0]}}{{this.width[1]}};
506-
height: {{this.height[0]}}{{this.height[1]}};
507-
left: {{this.left[0]}}{{this.left[1]}};
508-
top: {{this.top[0]}}{{this.top[1]}};
509-
</style>
510-
{% endmacro %}
511-
{% macro html(this, kwargs) %}
512-
<div id="{{this.get_name()}}">
513-
{{this.html.render(**kwargs)}}
514-
</div>
515-
{% endmacro %}
516-
""")
439+
self._template = Template(
440+
'{% macro header(this, kwargs) %}'
441+
'<style> #{{this.get_name()}} {\n'
442+
' position : {{this.position}};\n'
443+
' width : {{this.width[0]}}{{this.width[1]}};\n'
444+
' height: {{this.height[0]}}{{this.height[1]}};\n'
445+
' left: {{this.left[0]}}{{this.left[1]}};\n'
446+
' top: {{this.top[0]}}{{this.top[1]}};\n'
447+
' </style>'
448+
'{% endmacro %}'
449+
'{% macro html(this, kwargs) %}'
450+
'<div id="{{this.get_name()}}">{{this.html.render(**kwargs)}}</div>'
451+
'{% endmacro %}'
452+
)
517453

518454
def get_root(self):
519455
"""Returns the root of the elements tree."""

0 commit comments

Comments
 (0)