Skip to content

Commit

Permalink
Manage javascript and css import with Link objects ; embed notebook d…
Browse files Browse the repository at this point in the history
…isplay in an iframe
  • Loading branch information
BibMartin authored and Martin Journois committed Aug 26, 2015
1 parent 5b0246f commit 60c1755
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 6 deletions.
21 changes: 17 additions & 4 deletions mplleaflet/_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,19 @@
from jinja2 import Environment, PackageLoader

from .leaflet_renderer import LeafletRenderer
from .links import JavascriptLink, CssLink
from . import maptiles

# We download explicitely the CSS and the JS
_leaflet_js = JavascriptLink('https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js')
_leaflet_css = CssLink('https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css')
_attribution = '<a href="https://github.com/jwass/mplleaflet">mplleaflet</a>'

env = Environment(loader=PackageLoader('mplleaflet', 'templates'),
trim_blocks=True, lstrip_blocks=True)

def fig_to_html(fig=None, template='base.html', tiles=None, crs=None,
epsg=None):
epsg=None, embed_links=False):
"""
Convert a Matplotlib Figure to a Leaflet map
Expand Down Expand Up @@ -46,6 +50,8 @@ def fig_to_html(fig=None, template='base.html', tiles=None, crs=None,
epsg : int, default 4326
The EPSG code of the current plot. This can be used in place of the
'crs' parameter.
embed_links : bool, default False
Whether external links (except tiles) shall be explicitely embedded in the final html.
Note: only one of 'crs' or 'epsg' may be specified. Both may be None, in
which case the plot is assumed to be longitude / latitude.
Expand Down Expand Up @@ -85,6 +91,8 @@ def fig_to_html(fig=None, template='base.html', tiles=None, crs=None,
'mapid': mapid,
'tile_url': tiles[0],
'attribution': attribution,
'links': [_leaflet_js,_leaflet_css],
'embed_links': embed_links,
}
html = template.render(params)

Expand Down Expand Up @@ -131,17 +139,22 @@ def display(fig=None, closefig=True, **kwargs):
Figure used to convert to map
closefig : boolean, default True
Close the current Figure
"""
from IPython.display import HTML
if fig is None:
fig = plt.gcf()
if closefig:
plt.close(fig)

html = fig_to_html(fig, template="ipynb.html", **kwargs)
return HTML(html)
html = fig_to_html(fig, **kwargs)

# We embed everything in an iframe.
iframe_html = '<iframe src="data:text/html;base64,{html}" width="{width}" height="{height}"></iframe>'\
.format(html = html.encode('base64'),
width = int(60.*fig.get_figwidth()),
height= int(60.*fig.get_figheight()),
)
return HTML(iframe_html)

def show(fig=None, path='_map.html', **kwargs):
"""
Expand Down
51 changes: 51 additions & 0 deletions mplleaflet/links.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from six import Module_six_moves_urllib as urllib

class Link(object):
def __init__(self, url, download=False):
"""Create a link object base on an url.
Parameters
----------
url : str
The url to be linked
download : bool, default False
Whether the target document shall be loaded right now.
"""
self.url = url
self.code = None
if download:
self.code = urllib.request.urlopen(self.url).read()

class JavascriptLink(Link):
def render(self, embedded=False):
"""Renders the object.
Parameters
----------
embedded : bool, default False
Whether the code shall be embedded explicitely in the render.
"""
if embedded:
if self.code is None:
self.code = urllib.request.urlopen(self.url).read()
return '<script>{}</script>'.format(self.code)
else:
return '<script src="{}"></script>'.format(self.url)

class CssLink(Link):
def render(self, embedded=False):
"""Renders the object.
Parameters
----------
embedded : bool, default False
Whether the code shall be embedded explicitely in the render.
"""
if embedded:
if self.code is None:
self.code = urllib.request.urlopen(self.url).read()
return '<style>{}</style>'.format(self.code)
else:
return '<link rel="stylesheet" href="{}" />'.format(self.url)



5 changes: 3 additions & 2 deletions mplleaflet/templates/base.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<head>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.1/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.1/leaflet.js"></script>
{% for link in links %}
{{link.render(embedded=embed_links)}}
{% endfor %}
<style>
{% block style %}
#map{{ mapid }} {
Expand Down

0 comments on commit 60c1755

Please sign in to comment.