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