Show More
@@ -1,165 +1,169 | |||
|
1 | 1 | """Base classes for the notebook conversion pipeline. |
|
2 | 2 | |
|
3 | 3 | This module defines Converter, from which all objects designed to implement |
|
4 | 4 | a conversion of IPython notebooks to some other format should inherit. |
|
5 | 5 | """ |
|
6 | 6 | #----------------------------------------------------------------------------- |
|
7 | 7 | # Copyright (c) 2012, the IPython Development Team. |
|
8 | 8 | # |
|
9 | 9 | # Distributed under the terms of the Modified BSD License. |
|
10 | 10 | # |
|
11 | 11 | # The full license is in the file COPYING.txt, distributed with this software. |
|
12 | 12 | #----------------------------------------------------------------------------- |
|
13 | 13 | |
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | # Imports |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | 17 | |
|
18 | 18 | from __future__ import print_function, absolute_import |
|
19 | 19 | |
|
20 | 20 | # Stdlib imports |
|
21 | 21 | import io |
|
22 | 22 | import os |
|
23 | 23 | from IPython.utils import path |
|
24 | 24 | |
|
25 | 25 | from jinja2 import Environment, FileSystemLoader |
|
26 | 26 | env = Environment( |
|
27 | 27 | loader=FileSystemLoader('./templates/'), |
|
28 | 28 | extensions=['jinja2.ext.loopcontrols'] |
|
29 | 29 | ) |
|
30 | 30 | |
|
31 | 31 | # IPython imports |
|
32 | 32 | from IPython.nbformat import current as nbformat |
|
33 | 33 | from IPython.config.configurable import Configurable |
|
34 | 34 | from IPython.utils.traitlets import ( Unicode, Any) |
|
35 | 35 | |
|
36 | 36 | # Our own imports |
|
37 | 37 | from IPython.utils.text import indent |
|
38 | 38 | from .utils import remove_ansi |
|
39 | 39 | from markdown import markdown |
|
40 | 40 | from .utils import highlight, ansi2html |
|
41 | 41 | #----------------------------------------------------------------------------- |
|
42 | 42 | # Class declarations |
|
43 | 43 | #----------------------------------------------------------------------------- |
|
44 | 44 | def rm_fake(strng): |
|
45 | 45 | return strng.replace('/files/', '') |
|
46 | 46 | |
|
47 | 47 | class ConversionException(Exception): |
|
48 | 48 | pass |
|
49 | 49 | |
|
50 | 50 | |
|
51 | 51 | def python_comment(string): |
|
52 | 52 | return '# '+'\n# '.join(string.split('\n')) |
|
53 | 53 | |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | def header_body(): |
|
57 | 57 | """Return the body of the header as a list of strings.""" |
|
58 | 58 | |
|
59 | 59 | from pygments.formatters import HtmlFormatter |
|
60 | 60 | |
|
61 | 61 | header = [] |
|
62 | 62 | static = os.path.join(path.get_ipython_package_dir(), |
|
63 | 63 | 'frontend', 'html', 'notebook', 'static', |
|
64 | 64 | ) |
|
65 | 65 | here = os.path.split(os.path.realpath(__file__))[0] |
|
66 | 66 | css = os.path.join(static, 'css') |
|
67 | 67 | for sheet in [ |
|
68 | 68 | # do we need jquery and prettify? |
|
69 | 69 | # os.path.join(static, 'jquery', 'css', 'themes', 'base', |
|
70 | 70 | # 'jquery-ui.min.css'), |
|
71 | 71 | # os.path.join(static, 'prettify', 'prettify.css'), |
|
72 | 72 | os.path.join(css, 'boilerplate.css'), |
|
73 | 73 | os.path.join(css, 'fbm.css'), |
|
74 | 74 | os.path.join(css, 'notebook.css'), |
|
75 | 75 | os.path.join(css, 'renderedhtml.css'), |
|
76 | 76 | # our overrides: |
|
77 | 77 | os.path.join(here, '..', 'css', 'static_html.css'), |
|
78 | 78 | ]: |
|
79 | 79 | |
|
80 | 80 | with io.open(sheet, encoding='utf-8') as f: |
|
81 | 81 | s = f.read() |
|
82 | 82 | header.append(s) |
|
83 | 83 | |
|
84 | 84 | pygments_css = HtmlFormatter().get_style_defs('.highlight') |
|
85 | 85 | header.append(pygments_css) |
|
86 | 86 | return header |
|
87 | 87 | |
|
88 | 88 | inlining = {} |
|
89 | 89 | inlining['css'] = header_body() |
|
90 | 90 | |
|
91 | 91 | |
|
92 | 92 | def filter_data_type(output): |
|
93 | 93 | for fmt in ['html', 'pdf', 'svg', 'latex', 'png', 'jpg', 'jpeg' , 'text']: |
|
94 | 94 | if fmt in output: |
|
95 | 95 | return [fmt] |
|
96 | 96 | |
|
97 | 97 | |
|
98 | 98 | env.filters['filter_data_type'] = filter_data_type |
|
99 | 99 | env.filters['pycomment'] = python_comment |
|
100 | 100 | env.filters['indent'] = indent |
|
101 | 101 | env.filters['rm_fake'] = rm_fake |
|
102 | 102 | env.filters['rm_ansi'] = remove_ansi |
|
103 | 103 | env.filters['markdown'] = markdown |
|
104 | 104 | env.filters['highlight'] = highlight |
|
105 | 105 | env.filters['ansi2html'] = ansi2html |
|
106 | 106 | |
|
107 | ||
|
108 | def haspyout_transformer(nb,_): | |
|
109 | print('calling...') | |
|
110 | for worksheet in nb.worksheets: | |
|
111 | print('worksheet') | |
|
112 | for cell in worksheet.cells: | |
|
113 | cell.type = cell.cell_type | |
|
114 | cell.haspyout = False | |
|
115 | for out in cell.get('outputs', []): | |
|
116 | if out.output_type == 'pyout': | |
|
117 | cell.haspyout = True | |
|
118 | break | |
|
119 | return nb | |
|
120 | ||
|
107 | 121 | class ConverterTemplate(Configurable): |
|
108 | 122 | |
|
109 | 123 | display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text'] |
|
110 | 124 | #------------------------------------------------------------------------- |
|
111 | 125 | # Instance-level attributes that are set in the constructor for this |
|
112 | 126 | # class. |
|
113 | 127 | #------------------------------------------------------------------------- |
|
114 | 128 | infile = Any() |
|
115 | 129 | |
|
116 | 130 | |
|
117 | 131 | infile_dir = Unicode() |
|
118 | 132 | |
|
119 | 133 | def __init__(self, tplfile='fullhtml', preprocessors=[], config=None, **kw): |
|
120 | 134 | """ |
|
121 | 135 | preprocessors: list of function to run on ipynb json data before conversion |
|
122 | 136 | to extract/inline file, |
|
123 | 137 | |
|
124 | 138 | |
|
125 | 139 | |
|
126 | 140 | """ |
|
127 | 141 | self.template = env.get_template(tplfile+'.tpl') |
|
128 | 142 | self.nb = None |
|
129 | 143 | self.preprocessors = preprocessors |
|
144 | self.preprocessors.append(haspyout_transformer) | |
|
130 | 145 | super(ConverterTemplate, self).__init__(config=config, **kw) |
|
131 | 146 | |
|
147 | ||
|
132 | 148 | def process(self): |
|
133 | 149 | nb = self.nb |
|
134 | 150 | |
|
135 | 151 | for preprocessor in self.preprocessors: |
|
136 | 152 | nb = preprocessor(nb,{}) |
|
137 | 153 | |
|
138 |
|
|
|
139 | for worksheet in nb.worksheets: | |
|
140 | for cell in worksheet.cells: | |
|
141 | cell.type = cell.cell_type | |
|
142 | cell.haspyout = False | |
|
143 | for out in cell.get('outputs', []): | |
|
144 | if out.output_type == 'pyout': | |
|
145 | cell.haspyout = True | |
|
146 | break | |
|
147 | worksheets.append(worksheet) | |
|
148 | ||
|
149 | ||
|
150 | return worksheets | |
|
154 | return nb | |
|
151 | 155 | |
|
152 | 156 | def convert(self): |
|
153 | 157 | """ convert the ipynb file |
|
154 | 158 | |
|
155 | 159 | return both the converted ipynb file and a dict containing potential |
|
156 | 160 | other resources |
|
157 | 161 | """ |
|
158 |
return self.template.render( |
|
|
162 | return self.template.render(nb=self.process(), inlining=inlining), {} | |
|
159 | 163 | |
|
160 | 164 | |
|
161 | 165 | def read(self, filename): |
|
162 | 166 | "read and parse notebook into NotebookNode called self.nb" |
|
163 | 167 | with io.open(filename) as f: |
|
164 | 168 | self.nb = nbformat.read(f, 'json') |
|
165 | 169 |
@@ -1,89 +1,89 | |||
|
1 | 1 | {# |
|
2 | 2 | |
|
3 | 3 | DO NOT USE THIS AS A BASE WORK, |
|
4 | 4 | IF YOU ARE COPY AND PASTING THIS FILE |
|
5 | 5 | YOU ARE PROBABLY DOING THINGS WRONG. |
|
6 | 6 | |
|
7 | 7 | Null template, Does nothing except defining a basic structure |
|
8 | 8 | To layout the diferents blocks of a notebook. |
|
9 | 9 | |
|
10 | 10 | Subtemplates can Override Blocks to define their custom reresentation. |
|
11 | 11 | |
|
12 | 12 | If one of the block you do overrite is not a leave block, consider |
|
13 | 13 | calling super. |
|
14 | 14 | |
|
15 | 15 | {%- block nonLeaveBlock -%} |
|
16 | 16 | #add stuff at beginning |
|
17 | 17 | {{ super() }} |
|
18 | 18 | #add stuff at end |
|
19 | 19 | {%- endblock nonLeaveBlock -%} |
|
20 | 20 | |
|
21 | 21 | consider calling super even if block is leave block, we might insert more block later. |
|
22 | 22 | |
|
23 | 23 | #} |
|
24 | 24 | {%- block header -%} |
|
25 | 25 | {%- endblock header -%} |
|
26 | 26 | {%- block body -%} |
|
27 | {%- for worksheet in worksheets -%} | |
|
27 | {%- for worksheet in nb.worksheets -%} | |
|
28 | 28 | {%- for cell in worksheet.cells -%} |
|
29 | 29 | {%- block any_cell scoped -%} |
|
30 | 30 | {%- if cell.type in ['code'] -%} |
|
31 | 31 | {%- block codecell scoped -%} |
|
32 | 32 | {%- block input_group -%} |
|
33 | 33 | {%- block in_prompt -%}{%- endblock in_prompt -%} |
|
34 | 34 | {%- block input -%}{%- endblock input -%} |
|
35 | 35 | {%- endblock input_group -%} |
|
36 | 36 | {%- if cell.outputs -%} |
|
37 | 37 | {%- block output_group -%} |
|
38 | 38 | {%- block output_prompt -%}{%- endblock output_prompt -%} |
|
39 | 39 | {%- block outputs -%} |
|
40 | 40 | {%- for output in cell.outputs -%} |
|
41 | 41 | {%- if output.output_type in ['pyout'] -%} |
|
42 | 42 | {%- block pyout scoped -%}{%- endblock pyout -%} |
|
43 | 43 | {%- elif output.output_type in ['stream'] -%} |
|
44 | 44 | {%- block stream scoped -%} |
|
45 | 45 | {%- if output.stream in ['stdout'] -%} |
|
46 | 46 | {%- block stream_stdout scoped -%} |
|
47 | 47 | {%- endblock stream_stdout -%} |
|
48 | 48 | {%- elif output.stream in ['stderr'] -%} |
|
49 | 49 | {%- block stream_stderr scoped -%} |
|
50 | 50 | {%- endblock stream_stderr -%} |
|
51 | 51 | {%- endif -%} |
|
52 | 52 | {%- endblock stream -%} |
|
53 | 53 | {%- elif output.output_type in ['display_data'] -%} |
|
54 | 54 | {%- block display_data scoped -%} |
|
55 | 55 | {%- block data_priority scoped -%} |
|
56 | 56 | {%- endblock data_priority -%} |
|
57 | 57 | {%- endblock display_data -%} |
|
58 | 58 | {%- elif output.output_type in ['pyerr'] -%} |
|
59 | 59 | {%- block pyerr scoped -%} |
|
60 | 60 | {%- for line in output.traceback -%} |
|
61 | 61 | {%- block traceback_line scoped -%}{%- endblock traceback_line -%} |
|
62 | 62 | {%- endfor -%} |
|
63 | 63 | {%- endblock pyerr -%} |
|
64 | 64 | {%- endif -%} |
|
65 | 65 | {%- endfor -%} |
|
66 | 66 | {%- endblock outputs -%} |
|
67 | 67 | {%- endblock output_group -%} |
|
68 | 68 | {%- endif -%} |
|
69 | 69 | {%- endblock codecell -%} |
|
70 | 70 | {%- elif cell.type in ['markdown'] -%} |
|
71 | 71 | {%- block markdowncell scoped-%} |
|
72 | 72 | {%- endblock markdowncell -%} |
|
73 | 73 | {%- elif cell.type in ['heading'] -%} |
|
74 | 74 | {%- block headingcell scoped-%} |
|
75 | 75 | {%- endblock headingcell -%} |
|
76 | 76 | {%- elif cell.type in ['raw'] -%} |
|
77 | 77 | {%- block rawcell scoped-%} |
|
78 | 78 | {%- endblock rawcell -%} |
|
79 | 79 | {%- else -%} |
|
80 | 80 | {%- block unknowncell scoped-%} |
|
81 | 81 | {%- endblock unknowncell -%} |
|
82 | 82 | {%- endif -%} |
|
83 | 83 | {%- endblock any_cell -%} |
|
84 | 84 | {%- endfor -%} |
|
85 | 85 | {%- endfor -%} |
|
86 | 86 | {%- endblock body -%} |
|
87 | 87 | |
|
88 | 88 | {%- block footer -%} |
|
89 | 89 | {%- endblock footer -%} |
General Comments 0
You need to be logged in to leave comments.
Login now