Show More
@@ -1,64 +1,58 b'' | |||
|
1 | 1 | """Module containing a preprocessor 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 | from .base import Preprocessor |
|
17 | 17 | from IPython.utils.traitlets import Unicode |
|
18 | 18 | |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | # Classes |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | |
|
23 | 23 | class ConvertFiguresPreprocessor(Preprocessor): |
|
24 | 24 | """ |
|
25 | 25 | Converts all of the outputs in a notebook from one format to another. |
|
26 | 26 | """ |
|
27 | 27 | |
|
28 | 28 | from_format = Unicode(config=True, help='Format the converter accepts') |
|
29 | 29 | to_format = Unicode(config=True, help='Format the converter writes') |
|
30 | 30 | |
|
31 | 31 | def __init__(self, **kw): |
|
32 | 32 | """ |
|
33 | 33 | Public constructor |
|
34 | 34 | """ |
|
35 | 35 | super(ConvertFiguresPreprocessor, self).__init__(**kw) |
|
36 | 36 | |
|
37 | 37 | |
|
38 | 38 | def convert_figure(self, data_format, data): |
|
39 | 39 | raise NotImplementedError() |
|
40 | 40 | |
|
41 | 41 | |
|
42 | 42 | def preprocess_cell(self, cell, resources, cell_index): |
|
43 | 43 | """ |
|
44 | 44 | Apply a transformation on each cell, |
|
45 | 45 | |
|
46 | 46 | See base.py |
|
47 | 47 | """ |
|
48 | 48 | |
|
49 | 49 | # Loop through all of the datatypes of the outputs in the cell. |
|
50 |
for |
|
|
51 | for data_type, data in list(cell_out.items()): | |
|
52 | # this must run *before* extract outputs, | |
|
53 | # so figure_name and filename do not exist | |
|
54 | self._convert_figure(cell_out, resources, data_type, data) | |
|
55 | return cell, resources | |
|
50 | for output in cell.get('outputs', []): | |
|
51 | if output.output_type in {'execute_result', 'display_data'} \ | |
|
52 | and self.from_format in output.data \ | |
|
53 | and self.to_format not in output.data: | |
|
56 | 54 | |
|
55 | output.data[self.to_format] = self.convert_figure( | |
|
56 | self.from_format, output.data[self.from_format]) | |
|
57 | 57 | |
|
58 | def _convert_figure(self, cell_out, resources, data_type, data): | |
|
59 | """ | |
|
60 | Convert a figure and output the results to the cell output | |
|
61 | """ | |
|
62 | if not self.to_format in cell_out and data_type == self.from_format: | |
|
63 | data = self.convert_figure(data_type, data) | |
|
64 | cell_out[self.to_format] = data | |
|
58 | return cell, resources |
@@ -1,82 +1,82 b'' | |||
|
1 | 1 | """Module containing a preprocessor that converts outputs in the notebook from |
|
2 | 2 | one format to another. |
|
3 | 3 | """ |
|
4 | 4 | |
|
5 | 5 | # Copyright (c) IPython Development Team. |
|
6 | 6 | # Distributed under the terms of the Modified BSD License. |
|
7 | 7 | |
|
8 | 8 | import base64 |
|
9 | 9 | import io |
|
10 | 10 | import os |
|
11 | 11 | import sys |
|
12 | 12 | import subprocess |
|
13 | 13 | |
|
14 | 14 | from IPython.utils.tempdir import TemporaryDirectory |
|
15 | 15 | from IPython.utils.traitlets import Unicode |
|
16 | 16 | |
|
17 | 17 | from .convertfigures import ConvertFiguresPreprocessor |
|
18 | 18 | |
|
19 | 19 | |
|
20 | 20 | INKSCAPE_APP = '/Applications/Inkscape.app/Contents/Resources/bin/inkscape' |
|
21 | 21 | |
|
22 | 22 | |
|
23 | 23 | class SVG2PDFPreprocessor(ConvertFiguresPreprocessor): |
|
24 | 24 | """ |
|
25 | 25 | Converts all of the outputs in a notebook from SVG to PDF. |
|
26 | 26 | """ |
|
27 | 27 | |
|
28 | 28 | def _from_format_default(self): |
|
29 | 29 | return 'image/svg+xml' |
|
30 | 30 | def _to_format_default(self): |
|
31 | 31 | return 'application/pdf' |
|
32 | 32 | |
|
33 | 33 | command = Unicode(config=True, |
|
34 | 34 | help="""The command to use for converting SVG to PDF |
|
35 | 35 | |
|
36 | 36 | This string is a template, which will be formatted with the keys |
|
37 | 37 | to_filename and from_filename. |
|
38 | 38 | |
|
39 | 39 | The conversion call must read the SVG from {from_flename}, |
|
40 | 40 | and write a PDF to {to_filename}. |
|
41 | 41 | """) |
|
42 | 42 | |
|
43 | 43 | def _command_default(self): |
|
44 | 44 | return self.inkscape + \ |
|
45 | 45 | ' --without-gui --export-pdf="{to_filename}" "{from_filename}"' |
|
46 | 46 | |
|
47 | 47 | inkscape = Unicode(config=True, help="The path to Inkscape, if necessary") |
|
48 | 48 | def _inkscape_default(self): |
|
49 | 49 | if sys.platform == "darwin": |
|
50 | 50 | if os.path.isfile(INKSCAPE_APP): |
|
51 | 51 | return INKSCAPE_APP |
|
52 | 52 | return "inkscape" |
|
53 | 53 | |
|
54 | 54 | |
|
55 | 55 | def convert_figure(self, data_format, data): |
|
56 | 56 | """ |
|
57 | 57 | Convert a single SVG figure to PDF. Returns converted data. |
|
58 | 58 | """ |
|
59 | 59 | |
|
60 | 60 | #Work in a temporary directory |
|
61 | 61 | with TemporaryDirectory() as tmpdir: |
|
62 | 62 | |
|
63 | 63 | #Write fig to temp file |
|
64 |
input_filename = os.path.join(tmpdir, 'figure.' |
|
|
64 | input_filename = os.path.join(tmpdir, 'figure.svg') | |
|
65 | 65 | # SVG data is unicode text |
|
66 | 66 | with io.open(input_filename, 'w', encoding='utf8') as f: |
|
67 | 67 | f.write(data) |
|
68 | 68 | |
|
69 | 69 | #Call conversion application |
|
70 | 70 | output_filename = os.path.join(tmpdir, 'figure.pdf') |
|
71 | 71 | shell = self.command.format(from_filename=input_filename, |
|
72 | 72 | to_filename=output_filename) |
|
73 | 73 | subprocess.call(shell, shell=True) #Shell=True okay since input is trusted. |
|
74 | 74 | |
|
75 | 75 | #Read output from drive |
|
76 | 76 | # return value expects a filename |
|
77 | 77 | if os.path.isfile(output_filename): |
|
78 | 78 | with open(output_filename, 'rb') as f: |
|
79 | 79 | # PDF is a nb supported binary, data type, so base64 encode. |
|
80 | 80 | return base64.encodestring(f.read()) |
|
81 | 81 | else: |
|
82 | 82 | raise TypeError("Inkscape svg to pdf conversion failed") |
General Comments 0
You need to be logged in to leave comments.
Login now