Skip to content

Commit

Permalink
Merge pull request #1196 from pelson/repr_html
Browse files Browse the repository at this point in the history
Add pretty Jupyter repr of Projections
  • Loading branch information
QuLogic authored Nov 16, 2018
2 parents 52d9fc5 + c924e87 commit 6d095db
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lib/cartopy/crs.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,31 @@ def _determine_longitude_bounds(self, central_longitude):
minlon += epsilon
return minlon, maxlon

def _repr_html_(self):
import cgi
try:
# As matplotlib is not a core cartopy dependency, don't error
# if it's not available.
import matplotlib.pyplot as plt
except ImportError:
# 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 '{}<pre>{}</pre>'.format(svg, cgi.escape(repr(self)))

def _as_mpl_axes(self):
import cartopy.mpl.geoaxes as geoaxes
return geoaxes.GeoAxes, {'map_projection': self}
Expand Down

0 comments on commit 6d095db

Please sign in to comment.