Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support nbagg matplotlib backend (WIP) #75

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
10 changes: 9 additions & 1 deletion nbclient/client.py
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
)
from .util import run_sync, ensure_async
from .output_widget import OutputWidget
from .matplotlib import MatplotlibCommHandler


def timestamp():
@@ -293,7 +294,8 @@ def __init__(self, nb, km=None, **kw):
}
# comm_open_handlers should return an object with a .handle_msg(msg) method or None
self.comm_open_handlers = {
'jupyter.widget': self.on_comm_open_jupyter_widget
'jupyter.widget': self.on_comm_open_jupyter_widget,
'matplotlib': self.on_comm_open_matplotlib
}

def reset_execution_trackers(self):
@@ -914,6 +916,12 @@ def on_comm_open_jupyter_widget(self, msg):
if widget_class:
return widget_class(comm_id, state, self.kc, self)

def on_comm_open_matplotlib(self, msg):
content = msg['content']
data = content['data']
return MatplotlibCommHandler(comm_id=content['comm_id'], kernel_client=self.kc,
parent_header=msg['parent_header'], nbagg_id=data['id'])


def execute(nb, cwd=None, km=None, **kwargs):
"""Execute a notebook's code, updating outputs within the notebook object.
47 changes: 47 additions & 0 deletions nbclient/matplotlib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import json

from .jsonutil import json_clean


class MatplotlibCommHandler:
def __init__(self, comm_id, kernel_client, parent_header, nbagg_id):
self.comm_id = comm_id
self.kernel_client = kernel_client
self.parent_header = parent_header
self.nbagg_id = nbagg_id

# mimics https://github.com/matplotlib/matplotlib/blob/002b27e352b90410c9840233b6ce42c54e291403/lib/matplotlib/backends/web_backend/js/mpl.js#L63 # noqa
self.send('{"value":false,"type":"supports_binary","figure_id":"%s"}' % nbagg_id)
self.send('{"type":"send_image_mode","figure_id":"%s"}' % nbagg_id)
self.send('{"dpi_ratio":2,"type":"set_dpi_ratio","figure_id":"%s"}' % nbagg_id)
self.send('{"type":"refresh","figure_id":"%s"}' % nbagg_id)

def _publish_msg(self, msg_type, data=None, metadata=None, buffers=None, **keys):
"""Helper for sending a comm message on IOPub"""
data = {} if data is None else data
metadata = {} if metadata is None else metadata
content = json_clean(dict(data=data, comm_id=self.comm_id, **keys))
# it seems from looking at the websocket output in Chrome, that parent header
# is always empty
msg = self.kernel_client.session.msg(msg_type, content=content, parent={},
metadata=metadata)
print("SEND", msg)
self.kernel_client.shell_channel.send(msg)

def send(self, data=None, metadata=None, buffers=None):
self._publish_msg('comm_msg', data=data, metadata=metadata, buffers=buffers)

def handle_msg(self, msg):
print("RECV", msg)
content = msg['content']
data = content['data']
nbagg_data = data.get('data')
if nbagg_data:
nbagg_data = json.loads(nbagg_data)
print(nbagg_data)
if nbagg_data.get('type') == 'refresh':
# cannot figure out when we should send this
self.send('{"type":"refresh","figure_id":"%s"}' % self.nbagg_id)
if nbagg_data.get('type') == 'resize':
# mimics https://github.com/matplotlib/matplotlib/blob/002b27e352b90410c9840233b6ce42c54e291403/lib/matplotlib/backends/web_backend/js/mpl.js#L354 # noqa
self.send('{"type":"draw","figure_id":"%s"}' % self.nbagg_id)
852 changes: 852 additions & 0 deletions nbclient/tests/files/Matplotlib.ipynb

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions nbclient/tests/test_client.py
Original file line number Diff line number Diff line change
@@ -243,6 +243,7 @@ def notebook_resources():
@pytest.mark.parametrize(
["input_name", "opts"],
[
("Matplotlib.ipynb", dict(kernel_name="python")),
("Clear Output.ipynb", dict(kernel_name="python")),
("Empty Cell.ipynb", dict(kernel_name="python")),
("Factorials.ipynb", dict(kernel_name="python")),
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -15,3 +15,4 @@ pip>=18.1
wheel>=0.31.0
setuptools>=38.6.0
twine>=1.11.0
matplotlib