extractfigure.py
143 lines
| 4.9 KiB
| text/x-python
|
PythonLexer
Jonathan Frederic
|
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
|
r10795 | import itertools | ||
Jonathan Frederic
|
r10674 | |||
Brian E. Granger
|
r11089 | from IPython.utils.traitlets import Dict, Unicode | ||
Jonathan Frederic
|
r10624 | from .activatable import ActivatableTransformer | ||
Jonathan Frederic
|
r10437 | |||
Jonathan Frederic
|
r10674 | #----------------------------------------------------------------------------- | ||
# Constants | ||||
#----------------------------------------------------------------------------- | ||||
FIGURES_KEY = "figures" | ||||
BINARY_KEY = "binary" | ||||
TEXT_KEY = "text" | ||||
#----------------------------------------------------------------------------- | ||||
# Classes | ||||
#----------------------------------------------------------------------------- | ||||
Jonathan Frederic
|
r10437 | class ExtractFigureTransformer(ActivatableTransformer): | ||
Jonathan Frederic
|
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
|
r10437 | |||
Jonathan Frederic
|
r10674 | #TODO: Change this to .format {} syntax | ||
Jonathan Frederic
|
r10760 | default_key_template = Unicode('_fig_{index:02d}.{ext}', config=True) | ||
Zbigniew Jędrzejewski-Szmek
|
r10795 | |||
def __init__(self, config=None, **kw): | ||||
""" | ||||
Public constructor | ||||
Jonathan Frederic
|
r10772 | |||
Zbigniew Jędrzejewski-Szmek
|
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
|
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
|
r10772 | Index of the cell being processed (see base.py) | ||
Jonathan Frederic
|
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
|
r10772 | figname, key, data, binary = self._new_figure(out[out_type], out_type) | ||
Jonathan Frederic
|
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
|
r10437 | |||
Jonathan Frederic
|
r10674 | def _get_override_extension(self, extension): | ||
"""Gets the overriden extension if it exists, else returns extension. | ||||
Jonathan Frederic
|
r10437 | |||
Jonathan Frederic
|
r10674 | Parameters | ||
---------- | ||||
extension : str | ||||
File extension. | ||||
""" | ||||
if extension in self.extra_extension_map : | ||||
return self.extra_extension_map[extension] | ||||
return extension | ||||
Jonathan Frederic
|
r10437 | |||
Jonathan Frederic
|
r10772 | def _new_figure(self, data, format): | ||
Jonathan Frederic
|
r10437 | """Create a new figure file in the given format. | ||
Jonathan Frederic
|
r10674 | Parameters | ||
---------- | ||||
data : str | ||||
Cell data (from Notebook node cell) | ||||
Jonathan Frederic
|
r10760 | format : str | ||
Figure format | ||||
Jonathan Frederic
|
r10674 | index : int | ||
Jonathan Frederic
|
r10772 | Index of the figure being extracted | ||
Jonathan Frederic
|
r10437 | """ | ||
Jonathan Frederic
|
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
|
r10772 | |||
Jonathan Frederic
|
r10674 | #TODO: option to pass the hash as data? | ||
Zbigniew Jędrzejewski-Szmek
|
r10795 | index = next(self.index_generator) | ||
Jonathan Frederic
|
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
|
r10437 | |||
Jonathan Frederic
|
r10674 | #Binary files are base64-encoded, SVG is already XML | ||
Jonathan Frederic
|
r10437 | binary = False | ||
Jonathan Frederic
|
r10674 | if format in ('png', 'jpg', 'pdf'): | ||
Jonathan Frederic
|
r10437 | data = data.decode('base64') | ||
binary = True | ||||
Jonathan Frederic
|
r10674 | return figure_name, key, data, binary | ||