##// END OF EJS Templates
Fixed unicode data not written to files properly.
Jonathan Frederic -
Show More
@@ -1,98 +1,100 b''
1 1 """Module containing a transformer that extracts all of the figures from the
2 2 notebook file. The extracted figures 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 sys
17 17 from IPython.utils.traitlets import Dict, Unicode
18 18 from .activatable import ActivatableTransformer
19 19
20 20 #-----------------------------------------------------------------------------
21 21 # Constants
22 22 #-----------------------------------------------------------------------------
23 23
24 24 # win64 is win32 for backwards compatability, for now. See
25 25 # http://mail.python.org/pipermail/patches/2000-May/000648.html
26 26 # for the original patch that this decision was made.
27 27 WINDOWS_PLATFORMS = ['win32']
28 28
29 29 #-----------------------------------------------------------------------------
30 30 # Classes
31 31 #-----------------------------------------------------------------------------
32 32
33 33 class ExtractFigureTransformer(ActivatableTransformer):
34 34 """
35 35 Extracts all of the figures from the notebook file. The extracted
36 36 figures are returned in the 'resources' dictionary.
37 37 """
38 38
39 39
40 40 figure_filename_template = Unicode(
41 41 "{unique_key}_{cell_index}_{index}.{extension}", config=True)
42 42
43 43
44 44 def transform_cell(self, cell, resources, cell_index):
45 45 """
46 46 Apply a transformation on each cell,
47 47
48 48 Parameters
49 49 ----------
50 50 cell : NotebookNode cell
51 51 Notebook cell being processed
52 52 resources : dictionary
53 53 Additional resources used in the conversion process. Allows
54 54 transformers to pass variables into the Jinja engine.
55 55 cell_index : int
56 56 Index of the cell being processed (see base.py)
57 57 """
58 58
59 59 #Get the unique key from the resource dict if it exists. If it does not
60 60 #exist, use 'figure' as the default.
61 61 unique_key = resources.get('unique_key', 'figure')
62 62
63 63 #Make sure figures key exists
64 64 if not 'figures' in resources:
65 65 resources['figures'] = {}
66 66
67 67 #Loop through all of the outputs in the cell
68 68 for index, out in enumerate(cell.get('outputs', [])):
69 69
70 70 #Get the output in data formats that the template is interested in.
71 71 for out_type in self.display_data_priority:
72 72 if out.hasattr(out_type):
73 73 data = out[out_type]
74 74
75 75 #Binary files are base64-encoded, SVG is already XML
76 76 if out_type in ('png', 'jpg', 'pdf'):
77 77 data = data.decode('base64')
78 78 elif sys.platform in WINDOWS_PLATFORMS:
79 data = data.replace('\n', '\r\n')
79 data = data.replace('\n', '\r\n').encode("UTF-8")
80 else:
81 data = data.encode("UTF-8")
80 82
81 83 #Build a figure name
82 84 figure_name = self.figure_filename_template.format(
83 85 unique_key=unique_key,
84 86 cell_index=cell_index,
85 87 index=index,
86 88 extension=out_type)
87 89
88 90 #On the cell, make the figure available via
89 91 # cell.outputs[i].svg_filename ... etc (svg in example)
90 92 # Where
91 93 # cell.outputs[i].svg contains the data
92 94 out[out_type + '_filename'] = figure_name
93 95
94 96 #In the resources, make the figure available via
95 97 # resources['figures']['filename'] = data
96 98 resources['figures'][figure_name] = data
97 99
98 100 return cell, resources
General Comments 0
You need to be logged in to leave comments. Login now