Skip to content

Commit

Permalink
jupyter#751 support for converting buffered data in widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasz Mitusinski committed Mar 15, 2018
1 parent cc7f3c3 commit b5e9997
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions nbconvert/preprocessors/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.

import base64
from textwrap import dedent

try:
Expand Down Expand Up @@ -274,6 +274,7 @@ def start_new_kernel(startup_timeout=60, kernel_name='python', **kwargs):
self.kc.allow_stdin = False
self.nb = nb
self.widget_state = {}
self.widget_buffers = {}

try:
nb, resources = super(ExecutePreprocessor, self).preprocess(nb, resources)
Expand All @@ -291,6 +292,10 @@ def start_new_kernel(startup_timeout=60, kernel_name='python', **kwargs):
'version_minor': 0,
}
}
for key, widget in self.nb.metadata.widgets['application/vnd.jupyter.widget-state+json']['state'].items():
buffers = self.widget_buffers.get(key)
if buffers:
widget['buffers'] = buffers

delattr(self, 'nb')

Expand Down Expand Up @@ -421,7 +426,10 @@ def run_cell(self, cell, cell_index=0):
cell_map[cell_index] = []
continue
elif msg_type.startswith('comm'):
self.widget_state.setdefault(content['comm_id'], {}).update(content['data']['state'])
data = content['data']
self.widget_state.setdefault(content['comm_id'], {}).update(data['state'])
if 'buffer_paths' in data and data['buffer_paths']:
self.widget_buffers[content['comm_id']] = _get_buffer_data(msg)
continue

display_id = None
Expand Down Expand Up @@ -483,3 +491,15 @@ def _serialize_widget_state(state):
'model_module_version': state.get('_model_module_version'),
'state': state,
}


def _get_buffer_data(msg):
buffers = []
path = msg['content']['data']['buffer_paths']
for buffer in msg['buffers']:
buffers.append({
'data': base64.b64encode(buffer.obj).decode('utf-8'),
'encoding': 'base64',
'path': path
})
return buffers

0 comments on commit b5e9997

Please sign in to comment.