##// END OF EJS Templates
Merge pull request #3528 from minrk/staticbase...
Merge pull request #3528 from minrk/staticbase fix local mathjax with custom base_project_url

File last commit:

r11089:45d39d22
r11159:4e7b8eb3 merge
Show More
extractfigure.py
143 lines | 4.9 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
#-----------------------------------------------------------------------------
Zbigniew Jędrzejewski-Szmek
Fix problem with missing .index_generator...
r10795 import itertools
Jonathan Frederic
Cleanup and refactor, transformers
r10674
Brian E. Granger
Fixing import for nbconvert.
r11089 from IPython.utils.traitlets import Dict, Unicode
Jonathan Frederic
Fixed all broken references, refactored some stuff here and there,...
r10624 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 #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)
Zbigniew Jędrzejewski-Szmek
Fix problem with missing .index_generator...
r10795
def __init__(self, config=None, **kw):
"""
Public constructor
Jonathan Frederic
Generator used to create figure indicies
r10772
Zbigniew Jędrzejewski-Szmek
Fix problem with missing .index_generator...
r10795 Parameters
----------
config : Config
Configuration file structure
**kw : misc
Additional arguments
"""
super(ExtractFigureTransformer, self).__init__(config=config, **kw)
# A unique index for association with extracted figures
self.index_generator = itertools.count(1)
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
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 """
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):
Jonathan Frederic
Generator used to create figure indicies
r10772 figname, key, data, binary = self._new_figure(out[out_type], out_type)
Jonathan Frederic
Cleanup and refactor, transformers
r10674 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
Generator used to create figure indicies
r10772 def _new_figure(self, data, format):
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
Jonathan Frederic
Generator used to create figure indicies
r10772 Index of the figure being extracted
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
Generator used to create figure indicies
r10772
Jonathan Frederic
Cleanup and refactor, transformers
r10674 #TODO: option to pass the hash as data?
Zbigniew Jędrzejewski-Szmek
Fix problem with missing .index_generator...
r10795 index = next(self.index_generator)
Jonathan Frederic
Cleanup and refactor, transformers
r10674 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