diff --git a/ipympl/backend_nbagg.py b/ipympl/backend_nbagg.py
index 7dfaa4ae..bdbdf175 100644
--- a/ipympl/backend_nbagg.py
+++ b/ipympl/backend_nbagg.py
@@ -7,7 +7,8 @@
import os
from uuid import uuid4 as uuid
-from IPython.display import display, HTML
+from IPython.display import update_display, display, HTML
+from IPython import version_info as ipython_version_info
from ipywidgets import DOMWidget
from traitlets import Unicode, Bool, Float, List, Any
@@ -106,12 +107,8 @@ class NavigationIPy(NavigationToolbar2WebAgg):
def export(self):
buf = io.BytesIO()
self.canvas.figure.savefig(buf, format='png', dpi='figure')
- # Figure width in pixels
- pwidth = self.canvas.figure.get_figwidth() * self.canvas.figure.get_dpi()
- # Scale size to match widget on HiPD monitors
- width = pwidth / self.canvas._dpi_ratio
- data = "
"
- data = data.format(b64encode(buf.getvalue()).decode('utf-8'), width)
+ data = "
"
+ data = data.format(b64encode(buf.getvalue()).decode('utf-8'))
display(HTML(data))
@@ -182,6 +179,7 @@ def send_binary(self, blob):
data = data.decode('ascii')
data_uri = "data:image/png;base64,{0}".format(data)
self.send({'data': data_uri})
+ update_display(self, display_id='matplotlib_{0}'.format(self.figure.number))
def new_timer(self, *args, **kwargs):
return TimerTornado(*args, **kwargs)
@@ -192,6 +190,48 @@ def start_event_loop(self, timeout):
def stop_event_loop(self):
FigureCanvasBase.stop_event_loop_default(self)
+ def _repr_mimebundle_(self, **kwargs):
+ if self._view_name is not None:
+ # now happens before the actual display call.
+ self._handle_displayed(**kwargs)
+ plaintext = repr(self)
+ if len(plaintext) > 110:
+ plaintext = plaintext[:110] + '…'
+ # The 'application/vnd.jupyter.widget-view+json' mimetype has not been registered yet.
+ # See the registration process and naming convention at
+ # http://tools.ietf.org/html/rfc6838
+ # and the currently registered mimetypes at
+ # http://www.iana.org/assignments/media-types/media-types.xhtml.
+
+ buf = io.BytesIO()
+ self.figure.savefig(buf, format='png', dpi='figure')
+ data = "
"
+ data = data.format(b64encode(buf.getvalue()).decode('utf-8'))
+
+ data = {
+ 'text/plain': plaintext,
+ 'text/html': data,
+ 'application/vnd.jupyter.widget-view+json': {
+ 'version_major': 2,
+ 'version_minor': 0,
+ 'model_id': self._model_id
+ }
+ }
+ return data
+
+ def _ipython_display_(self, **kwargs):
+ """Called when `IPython.display.display` is called on a widget.
+
+ Note: if we are in IPython 6.1 or later, we return NotImplemented so
+ that _repr_mimebundle_ is used directly.
+ """
+ if ipython_version_info >= (6, 1):
+ raise NotImplementedError
+
+ data = self._repr_mimebundle_(**kwargs)
+ if data:
+ display(data, raw=True, display_id='matplotlib_{0}'.format(self.number))
+
class FigureManagerNbAgg(FigureManagerWebAgg):
ToolbarCls = NavigationIPy