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 authored and maartenbreddels committed Nov 30, 2018
1 parent fcda672 commit 55019f8
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 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
from contextlib import contextmanager

Expand Down Expand Up @@ -335,6 +335,7 @@ def preprocess(self, nb, resources, km=None):
Preprocess notebook executing each code cell.
The input argument `nb` is modified in-place.
self.widget_buffers = {}
Parameters
----------
Expand Down Expand Up @@ -489,7 +490,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 @@ -553,3 +557,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 55019f8

Please sign in to comment.