Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 47 additions & 7 deletions ipympl/backend_nbagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = "<img src='data:image/png;base64,{0}' width={1}/>"
data = data.format(b64encode(buf.getvalue()).decode('utf-8'), width)
data = "<img src='data:image/png;base64,{0}'/>"
data = data.format(b64encode(buf.getvalue()).decode('utf-8'))
display(HTML(data))


Expand Down Expand Up @@ -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)
Expand All @@ -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 = "<img src='data:image/png;base64,{0}'/>"
data = data.format(b64encode(buf.getvalue()).decode('utf-8'))

data = {
'text/plain': plaintext,
'text/html': data,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not directly use image/png?

'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
Expand Down