##// END OF EJS Templates
Missing imports
Jonathan Frederic -
Show More
@@ -1,78 +1,81 b''
1 1 """Module containing a transformer that converts outputs in the notebook from
2 2 one format to another.
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 os
17 import sys
18 import subprocess
19
17 20 from IPython.utils.tempdir import TemporaryDirectory
18 21
19 22 from .convertfigures import ConvertFiguresTransformer
20 23
21 24
22 25 #-----------------------------------------------------------------------------
23 26 # Constants
24 27 #-----------------------------------------------------------------------------
25 28
26 29 INKSCAPE_COMMAND = 'inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
27 30 INKSCAPE_OSX_COMMAND = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape --without-gui --export-pdf="{to_filename}" "{from_filename}"'
28 31
29 32
30 33 #-----------------------------------------------------------------------------
31 34 # Classes
32 35 #-----------------------------------------------------------------------------
33 36
34 37 class ConvertSvgTransformer(ConvertFiguresTransformer):
35 38 """
36 39 Converts all of the outputs in a notebook from one format to another.
37 40 """
38 41
39 42
40 43 def __init__(self, **kw):
41 44 """
42 45 Constructor
43 46 """
44 47 super(ConvertSvgTransformer, self).__init__(['svg'], 'pdf', **kw)
45 48
46 49
47 50 def convert_figure(self, data_format, data):
48 51 """
49 52 Convert a single Svg figure. Returns converted data.
50 53 """
51 54
52 55 #Work in a temporary directory
53 56 with TemporaryDirectory() as tmpdir:
54 57
55 58 #Write fig to temp file
56 59 input_filename = os.path.join(tmpdir, 'figure.' + data_format)
57 60 with open(input_filename, 'w') as f:
58 61 f.write(data)
59 62
60 63 #Determine command (different on Mac OSX)
61 64 command = INKSCAPE_COMMAND
62 65 if sys.platform == 'darwin':
63 66 command = INKSCAPE_OSX_COMMAND
64 67
65 68 #Call conversion application
66 69 output_filename = os.path.join(tmpdir, 'figure.pdf')
67 70 shell = command.format(from_filename=input_filename,
68 71 to_filename=output_filename)
69 72 subprocess.call(shell, shell=True) #Shell=True okay since input is trusted.
70 73
71 74 #Read output from drive
72 75 if os.path.isfile(output_filename):
73 76 with open(output_filename, 'rb') as f:
74 77 return f.read().encode("base64") #PDF is a nb supported binary
75 78 #data type, so base64 encode.
76 79 else:
77 80 return TypeError("Inkscape svg to png conversion failed")
78 81 #TODO: ERROR: which notebook crashed exporter
General Comments 0
You need to be logged in to leave comments. Login now