Skip to content

Commit 5e90c1b

Browse files
author
Martin Journois
committed
plugins JavascriptLink and CssLink
1 parent a55680b commit 5e90c1b

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

folium/plugins/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212
from .layer import Layer, LayerControl
1313
from .geo_json import GeoJson
1414
from .timestamped_geo_json import TimestampedGeoJson
15+
from .links import JavascriptLink, CssLink

folium/plugins/links.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Links
4+
-----
5+
6+
Plugins to add links into the header of the file.
7+
8+
"""
9+
10+
#from six import Module_six_moves_urllib as urllib
11+
#urlopen = urllib.request.urlopen
12+
import sys, urllib
13+
PY3 = sys.version_info[0] == 3
14+
urlopen = urllib.request.urlopen if PY3 else urllib.urlopen
15+
16+
from .plugin import Plugin
17+
18+
class Link(Plugin):
19+
def __init__(self, url, download=False):
20+
"""Create a link object based on a url.
21+
Parameters
22+
----------
23+
url : str
24+
The url to be linked
25+
download : bool, default False
26+
Whether the target document shall be loaded right now.
27+
"""
28+
self.url = url
29+
self.code = None
30+
if download:
31+
self.code = urlopen(self.url).read()
32+
def render_header(self, nb, embedded=False):
33+
"""Generates the Header part of the plugin."""
34+
return self.render(embedded=embedded)
35+
36+
class JavascriptLink(Link):
37+
def render(self, embedded=False):
38+
"""Renders the object.
39+
40+
Parameters
41+
----------
42+
embedded : bool, default False
43+
Whether the code shall be embedded explicitely in the render.
44+
"""
45+
if embedded:
46+
if self.code is None:
47+
self.code = urlopen(self.url).read()
48+
return '<script>{}</script>'.format(self.code)
49+
else:
50+
return '<script src="{}"></script>'.format(self.url)
51+
52+
class CssLink(Link):
53+
def render(self, embedded=False):
54+
"""Renders the object.
55+
56+
Parameters
57+
----------
58+
embedded : bool, default False
59+
Whether the code shall be embedded explicitely in the render.
60+
"""
61+
if embedded:
62+
if self.code is None:
63+
self.code = urlopen(self.url).read()
64+
return '<style>{}</style>'.format(self.code)
65+
else:
66+
return '<link rel="stylesheet" href="{}" />'.format(self.url)
67+
68+
69+

tests/test_plugins.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111

1212
class testPlugins(object):
1313
'''Test class for Folium plugins'''
14+
def test_javascript_link(self):
15+
j = plugins.JavascriptLink("https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js")
16+
j.render(embedded=False)
17+
j.render(embedded=True)
18+
j.render_header(0)
19+
j.render_html(0)
20+
21+
def test_css_link(self):
22+
c = plugins.CssLink("https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css")
23+
c.render(embedded=False)
24+
c.render(embedded=True)
25+
c.render_header(0)
26+
c.render_html(0)
1427

1528
def test_scroll_zoom_toggler(self):
1629
mapa = folium.Map([45.,3.], zoom_start=4)

0 commit comments

Comments
 (0)