##// END OF EJS Templates
figure renamed to output
Jonathan Frederic -
Show More
@@ -1,101 +1,101 b''
1 1 """Module containing a transformer that extracts all of the outputs from the
2 2 notebook file. The extracted outputs are returned in the 'resources' dictionary.
3 3 """
4 4 #-----------------------------------------------------------------------------
5 5 # Copyright (c) 2013, the IPython Development Team.
6 6 #
7 7 # Distributed under the terms of the Modified BSD License.
8 8 #
9 9 # The full license is in the file COPYING.txt, distributed with this software.
10 10 #-----------------------------------------------------------------------------
11 11
12 12 #-----------------------------------------------------------------------------
13 13 # Imports
14 14 #-----------------------------------------------------------------------------
15 15
16 16 import base64
17 17 import sys
18 18 import os
19 19
20 20 from IPython.utils.traitlets import Unicode
21 21 from .base import Transformer
22 22 from IPython.utils import py3compat
23 23
24 24 #-----------------------------------------------------------------------------
25 25 # Classes
26 26 #-----------------------------------------------------------------------------
27 27
28 28 class ExtractOutputTransformer(Transformer):
29 29 """
30 30 Extracts all of the outputs from the notebook file. The extracted
31 31 outputs are returned in the 'resources' dictionary.
32 32 """
33 33
34 34 figure_filename_template = Unicode(
35 35 "{unique_key}_{cell_index}_{index}.{extension}", config=True)
36 36
37 37
38 38 def transform_cell(self, cell, resources, cell_index):
39 39 """
40 40 Apply a transformation on each cell,
41 41
42 42 Parameters
43 43 ----------
44 44 cell : NotebookNode cell
45 45 Notebook cell being processed
46 46 resources : dictionary
47 47 Additional resources used in the conversion process. Allows
48 48 transformers to pass variables into the Jinja engine.
49 49 cell_index : int
50 50 Index of the cell being processed (see base.py)
51 51 """
52 52
53 53 #Get the unique key from the resource dict if it exists. If it does not
54 #exist, use 'figure' as the default. Also, get files directory if it
54 #exist, use 'output' as the default. Also, get files directory if it
55 55 #has been specified
56 unique_key = resources.get('unique_key', 'figure')
56 unique_key = resources.get('unique_key', 'output')
57 57 output_files_dir = resources.get('output_files_dir', None)
58 58
59 59 #Make sure outputs key exists
60 60 if not 'outputs' in resources:
61 61 resources['outputs'] = {}
62 62
63 63 #Loop through all of the outputs in the cell
64 64 for index, out in enumerate(cell.get('outputs', [])):
65 65
66 66 #Get the output in data formats that the template is interested in.
67 67 for out_type in self.display_data_priority:
68 68 if out.hasattr(out_type):
69 69 data = out[out_type]
70 70
71 71 #Binary files are base64-encoded, SVG is already XML
72 72 if out_type in ('png', 'jpg', 'jpeg', 'pdf'):
73 73 # data is b64-encoded as text (str, unicode)
74 74 # decodestring only accepts bytes
75 75 data = py3compat.cast_bytes(data)
76 76 data = base64.decodestring(data)
77 77 elif sys.platform == 'win32':
78 78 data = data.replace('\n', '\r\n').encode("UTF-8")
79 79 else:
80 80 data = data.encode("UTF-8")
81 81
82 #Build a figure name
82 #Build a output name
83 83 filename = self.figure_filename_template.format(
84 84 unique_key=unique_key,
85 85 cell_index=cell_index,
86 86 index=index,
87 87 extension=out_type)
88 88
89 89 #On the cell, make the figure available via
90 90 # cell.outputs[i].svg_filename ... etc (svg in example)
91 91 # Where
92 92 # cell.outputs[i].svg contains the data
93 93 if output_files_dir is not None:
94 94 filename = os.path.join(output_files_dir, filename)
95 95 out[out_type + '_filename'] = filename
96 96
97 97 #In the resources, make the figure available via
98 98 # resources['outputs']['filename'] = data
99 99 resources['outputs'][filename] = data
100 100
101 101 return cell, resources
General Comments 0
You need to be logged in to leave comments. Login now