##// END OF EJS Templates
Fixes for Py3.3
Fixes for Py3.3

File last commit:

r11547:917c213a
r11547:917c213a
Show More
extractfigure.py
99 lines | 4.0 KiB | text/x-python | PythonLexer
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """Module containing a transformer that extracts all of the figures from the
notebook file. The extracted figures are returned in the 'resources' dictionary.
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2013, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------
Jonathan Frederic
Missing sys import
r11370 import sys
Jonathan Frederic
Missing imports
r11389 from IPython.utils.traitlets import Unicode
MinRK
s/ConfigurableTransformer/Transformer/
r11452 from .base import Transformer
Jonathan Frederic
Fixes for Py3.3
r11547 from IPython.utils import py3compat
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Cleanup and refactor, transformers
r10674 #-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
MinRK
s/ConfigurableTransformer/Transformer/
r11452 class ExtractFigureTransformer(Transformer):
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """
Extracts all of the figures from the notebook file. The extracted
figures are returned in the 'resources' dictionary.
"""
Jonathan Frederic
Added writers and supporting code.
r11367 figure_filename_template = Unicode(
"{unique_key}_{cell_index}_{index}.{extension}", config=True)
Zbigniew Jędrzejewski-Szmek
Fix problem with missing .index_generator...
r10795
Jonathan Frederic
Added writers and supporting code.
r11367 def transform_cell(self, cell, resources, cell_index):
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """
Apply a transformation on each cell,
Parameters
----------
cell : NotebookNode cell
Notebook cell being processed
resources : dictionary
Additional resources used in the conversion process. Allows
transformers to pass variables into the Jinja engine.
Jonathan Frederic
Added writers and supporting code.
r11367 cell_index : int
Jonathan Frederic
Generator used to create figure indicies
r10772 Index of the cell being processed (see base.py)
Jonathan Frederic
Cleanup and refactor, transformers
r10674 """
Jonathan Frederic
Added writers and supporting code.
r11367
#Get the unique key from the resource dict if it exists. If it does not
#exist, use 'figure' as the default.
unique_key = resources.get('unique_key', 'figure')
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Added writers and supporting code.
r11367 #Make sure figures key exists
if not 'figures' in resources:
resources['figures'] = {}
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Jonathan Frederic
Added writers and supporting code.
r11367 #Loop through all of the outputs in the cell
for index, out in enumerate(cell.get('outputs', [])):
#Get the output in data formats that the template is interested in.
Jonathan Frederic
Cleanup and refactor, transformers
r10674 for out_type in self.display_data_priority:
if out.hasattr(out_type):
Jonathan Frederic
Added writers and supporting code.
r11367 data = out[out_type]
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Added writers and supporting code.
r11367 #Binary files are base64-encoded, SVG is already XML
Jonathan Frederic
Added 'jpeg' key
r11393 if out_type in ('png', 'jpg', 'jpeg', 'pdf'):
Jonathan Frederic
Fixes for Py3.3
r11547
#Python3 base64 is in a separate library...
if py3compat.PY3:
#Base 64 decode the bytes
import base64
data = base64.b64decode(data)
else:
data = data.decode('base64')
Jonathan Frederic
Changes after in person review with @ellisonbg including TODO tags
r11379 elif sys.platform == 'win32':
Jonathan Frederic
Fixed unicode data not written to files properly.
r11371 data = data.replace('\n', '\r\n').encode("UTF-8")
else:
data = data.encode("UTF-8")
Jonathan Frederic
Added writers and supporting code.
r11367
#Build a figure name
figure_name = self.figure_filename_template.format(
unique_key=unique_key,
cell_index=cell_index,
index=index,
extension=out_type)
#On the cell, make the figure available via
# cell.outputs[i].svg_filename ... etc (svg in example)
# Where
# cell.outputs[i].svg contains the data
out[out_type + '_filename'] = figure_name
#In the resources, make the figure available via
# resources['figures']['filename'] = data
resources['figures'][figure_name] = data
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Added writers and supporting code.
r11367 return cell, resources