From 7b1cf047225fbcf6f80f54d0aa3d12acb1c44320 Mon Sep 17 00:00:00 2001 From: Peter Killick Date: Tue, 14 Nov 2017 13:49:54 +0000 Subject: [PATCH 1/4] Add _repr_html_ to Projection class --- lib/cartopy/crs.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/cartopy/crs.py b/lib/cartopy/crs.py index 90b254c66..9357d276a 100644 --- a/lib/cartopy/crs.py +++ b/lib/cartopy/crs.py @@ -156,6 +156,13 @@ def _determine_longitude_bounds(self, central_longitude): minlon += epsilon return minlon, maxlon + def _repr_html_(self): + import matplotlib.pyplot as plt + ax = plt.axes(projection=self) + ax.set_global() + ax.coastlines('110m') + plt.show() + def _as_mpl_axes(self): import cartopy.mpl.geoaxes as geoaxes return geoaxes.GeoAxes, {'map_projection': self} From ce2c811e91546ce04317ac69a2d00ab83daee7d6 Mon Sep 17 00:00:00 2001 From: Peter Killick Date: Fri, 1 Dec 2017 16:47:17 +0000 Subject: [PATCH 2/4] You want html? Here's some html --- lib/cartopy/crs.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/cartopy/crs.py b/lib/cartopy/crs.py index 9357d276a..18e063fc8 100644 --- a/lib/cartopy/crs.py +++ b/lib/cartopy/crs.py @@ -157,11 +157,29 @@ def _determine_longitude_bounds(self, central_longitude): return minlon, maxlon def _repr_html_(self): + """ + Make a visual representation of the projection and return it as an + html element. + + """ + # Imports. + import base64 + from io import BytesIO import matplotlib.pyplot as plt + # Produce a visual repr of the Projection instance. ax = plt.axes(projection=self) ax.set_global() ax.coastlines('110m') - plt.show() + # "Save" to a bytestring. + fmt = 'png' + buf = BytesIO() + plt.savefig(buf, format=fmt) + plt.close() + buf.seek(0) # "Rewind" the buffer to the start. + img_str = base64.b64encode(buf.getvalue()).decode() + # Produce html output. + html = '' + return html.format(fmt=fmt, img_str=img_str) def _as_mpl_axes(self): import cartopy.mpl.geoaxes as geoaxes From 61e31736b71215fef0902d60963c85fb5ce72b6f Mon Sep 17 00:00:00 2001 From: Peter Killick Date: Thu, 15 Nov 2018 12:22:10 +0000 Subject: [PATCH 3/4] Produce SVG repr instead --- lib/cartopy/crs.py | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/cartopy/crs.py b/lib/cartopy/crs.py index 18e063fc8..2efca4376 100644 --- a/lib/cartopy/crs.py +++ b/lib/cartopy/crs.py @@ -156,30 +156,40 @@ def _determine_longitude_bounds(self, central_longitude): minlon += epsilon return minlon, maxlon - def _repr_html_(self): + def _repr_svg_(self): """ Make a visual representation of the projection and return it as an html element. """ # Imports. - import base64 - from io import BytesIO - import matplotlib.pyplot as plt - # Produce a visual repr of the Projection instance. - ax = plt.axes(projection=self) - ax.set_global() - ax.coastlines('110m') - # "Save" to a bytestring. - fmt = 'png' - buf = BytesIO() - plt.savefig(buf, format=fmt) - plt.close() - buf.seek(0) # "Rewind" the buffer to the start. - img_str = base64.b64encode(buf.getvalue()).decode() - # Produce html output. - html = '' - return html.format(fmt=fmt, img_str=img_str) + try: + # As matplotlib is not a core cartopy dependency, don't error + # if it's not available, but fall back to the default repr. + import matplotlib.pyplot as plt + except ImportError: + pass + else: + if six.PY2: + from StringIO import StringIO + else: + from io import StringIO + # Produce a visual repr of the Projection instance. + fig, ax = plt.subplots(figsize=(5, 3), + subplot_kw={'projection': self}) + ax.set_global() + ax.coastlines('110m') + ax.gridlines() + # "Save" to a string buffer. + buf = StringIO() + fig.savefig(buf, format='svg') + plt.close(fig) + # "Rewind" the buffer to the start and return it as an svg string. + buf.seek(0) + return buf.read() + finally: + # Fall back to the standard repr if anything goes wrong. + repr(self) def _as_mpl_axes(self): import cartopy.mpl.geoaxes as geoaxes From c924e87c24c8167aedbd726c5e35d6b1a9e3795c Mon Sep 17 00:00:00 2001 From: Phil Elson Date: Fri, 16 Nov 2018 04:42:34 +0000 Subject: [PATCH 4/4] Added Projection._repr_html_ to show the projection in a notebook. --- lib/cartopy/crs.py | 50 +++++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/lib/cartopy/crs.py b/lib/cartopy/crs.py index 2efca4376..ca1bf4065 100644 --- a/lib/cartopy/crs.py +++ b/lib/cartopy/crs.py @@ -156,40 +156,30 @@ def _determine_longitude_bounds(self, central_longitude): minlon += epsilon return minlon, maxlon - def _repr_svg_(self): - """ - Make a visual representation of the projection and return it as an - html element. - - """ - # Imports. + def _repr_html_(self): + import cgi try: # As matplotlib is not a core cartopy dependency, don't error - # if it's not available, but fall back to the default repr. + # if it's not available. import matplotlib.pyplot as plt except ImportError: - pass - else: - if six.PY2: - from StringIO import StringIO - else: - from io import StringIO - # Produce a visual repr of the Projection instance. - fig, ax = plt.subplots(figsize=(5, 3), - subplot_kw={'projection': self}) - ax.set_global() - ax.coastlines('110m') - ax.gridlines() - # "Save" to a string buffer. - buf = StringIO() - fig.savefig(buf, format='svg') - plt.close(fig) - # "Rewind" the buffer to the start and return it as an svg string. - buf.seek(0) - return buf.read() - finally: - # Fall back to the standard repr if anything goes wrong. - repr(self) + # We can't return an SVG of the CRS, so let Jupyter fall back to + # a default repr by returning None. + return None + + # Produce a visual repr of the Projection instance. + fig, ax = plt.subplots(figsize=(5, 3), + subplot_kw={'projection': self}) + ax.set_global() + ax.coastlines('auto') + ax.gridlines() + buf = six.StringIO() + fig.savefig(buf, format='svg', bbox_inches='tight') + plt.close(fig) + # "Rewind" the buffer to the start and return it as an svg string. + buf.seek(0) + svg = buf.read() + return '{}
{}
'.format(svg, cgi.escape(repr(self))) def _as_mpl_axes(self): import cartopy.mpl.geoaxes as geoaxes