##// END OF EJS Templates
Rename api to exporters, as in discussion
Rename api to exporters, as in discussion

File last commit:

r10760:612fb92d
r10761:3ca63525
Show More
extractfigure.py
126 lines | 4.5 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
Fixed all broken references, refactored some stuff here and there,...
r10624 from IPython.utils.traitlets import (Dict, List, Unicode)
from .activatable import ActivatableTransformer
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Cleanup and refactor, transformers
r10674 #-----------------------------------------------------------------------------
# Constants
#-----------------------------------------------------------------------------
FIGURES_KEY = "figures"
BINARY_KEY = "binary"
TEXT_KEY = "text"
#-----------------------------------------------------------------------------
# Classes
#-----------------------------------------------------------------------------
Jonathan Frederic
Split transformer code
r10437 class ExtractFigureTransformer(ActivatableTransformer):
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.
"""
extra_extension_map = Dict({},
config=True,
help="""Extra map to override extension based on type.
Useful for latex where SVG will be converted to PDF before inclusion
""")
key_format_map = Dict({}, config=True,)
figure_name_format_map = Dict({}, config=True)
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Cleanup and refactor, transformers
r10674 display_data_priority = List(['svg', 'png', 'latex', 'jpg', 'jpeg','text'])
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Cleanup and refactor, transformers
r10674 #TODO: Change this to .format {} syntax
Jonathan Frederic
Fixed extract figure, rename from count to index
r10760 default_key_template = Unicode('_fig_{index:02d}.{ext}', config=True)
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Cleanup and refactor, transformers
r10674 def cell_transform(self, cell, resources, index):
"""
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.
index : int
Modified index of the cell being processed (see base.py)
"""
if resources.get(FIGURES_KEY, None) is None :
resources[FIGURES_KEY] = {TEXT_KEY:{},BINARY_KEY:{}}
for out in cell.get('outputs', []):
for out_type in self.display_data_priority:
if out.hasattr(out_type):
figname, key, data, binary = self._new_figure(out[out_type], out_type, index)
out['key_'+out_type] = figname
if binary :
resources[FIGURES_KEY][BINARY_KEY][key] = data
else :
resources[FIGURES_KEY][TEXT_KEY][key] = data
index += 1
return cell, resources
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Cleanup and refactor, transformers
r10674 def _get_override_extension(self, extension):
"""Gets the overriden extension if it exists, else returns extension.
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Cleanup and refactor, transformers
r10674 Parameters
----------
extension : str
File extension.
"""
if extension in self.extra_extension_map :
return self.extra_extension_map[extension]
return extension
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Cleanup and refactor, transformers
r10674 def _new_figure(self, data, format, index):
Jonathan Frederic
Split transformer code
r10437 """Create a new figure file in the given format.
Jonathan Frederic
Cleanup and refactor, transformers
r10674 Parameters
----------
data : str
Cell data (from Notebook node cell)
Jonathan Frederic
Fixed extract figure, rename from count to index
r10760 format : str
Figure format
Jonathan Frederic
Cleanup and refactor, transformers
r10674 index : int
Modified index of the cell being processed (see base.py)
Jonathan Frederic
Split transformer code
r10437 """
Jonathan Frederic
Cleanup and refactor, transformers
r10674
figure_name_template = self.figure_name_format_map.get(format, self.default_key_template)
key_template = self.key_format_map.get(format, self.default_key_template)
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Cleanup and refactor, transformers
r10674 #TODO: option to pass the hash as data?
figure_name = figure_name_template.format(index=index, ext=self._get_override_extension(format))
key = key_template.format(index=index, ext=self._get_override_extension(format))
Jonathan Frederic
Split transformer code
r10437
Jonathan Frederic
Cleanup and refactor, transformers
r10674 #Binary files are base64-encoded, SVG is already XML
Jonathan Frederic
Split transformer code
r10437 binary = False
Jonathan Frederic
Cleanup and refactor, transformers
r10674 if format in ('png', 'jpg', 'pdf'):
Jonathan Frederic
Split transformer code
r10437 data = data.decode('base64')
binary = True
Jonathan Frederic
Cleanup and refactor, transformers
r10674 return figure_name, key, data, binary