##// END OF EJS Templates
try to play with data display priority
Matthias BUSSONNIER -
Show More
@@ -0,0 +1,45 b''
1 {%- extends 'null.tpl' -%}
2
3 {#display data priority#}
4
5
6 {%- block data_priority scoped -%}
7 {%- for type in output | filter_data_type -%}
8 {%- if type in ['pdf']%}
9 {%- block data_pdf -%}
10 ->> pdf
11 {%- endblock -%}
12 {%- endif -%}
13 {%- if type in ['svg']%}
14 {%- block data_svg -%}
15 ->> svg
16 {%- endblock -%}
17 {%- endif -%}
18 {%- if type in ['png']%}
19 {%- block data_png -%}
20 ->> png
21 {%- endblock -%}
22 {%- endif -%}
23 {%- if type in ['html']%}
24 {%- block data_html -%}
25 ->> html
26 {%- endblock -%}
27 {%- endif -%}
28 {%- if type in ['jpeg']%}
29 {%- block data_jpg -%}
30 ->> jpg
31 {%- endblock -%}
32 {%- endif -%}
33 {%- if type in ['text']%}
34 {%- block data_text -%}
35 ->> text
36 {%- endblock -%}
37 {%- endif -%}
38
39 {%- if type in ['latex']%}
40 {%- block data_latex -%}
41 ->> latext
42 {%- endblock -%}
43 {%- endif -%}
44 {%- endfor -%}
45 {%- endblock data_priority -%}
@@ -1,146 +1,157 b''
1 """Base classes for the notebook conversion pipeline.
1 """Base classes for the notebook conversion pipeline.
2
2
3 This module defines Converter, from which all objects designed to implement
3 This module defines Converter, from which all objects designed to implement
4 a conversion of IPython notebooks to some other format should inherit.
4 a conversion of IPython notebooks to some other format should inherit.
5 """
5 """
6 #-----------------------------------------------------------------------------
6 #-----------------------------------------------------------------------------
7 # Copyright (c) 2012, the IPython Development Team.
7 # Copyright (c) 2012, the IPython Development Team.
8 #
8 #
9 # Distributed under the terms of the Modified BSD License.
9 # Distributed under the terms of the Modified BSD License.
10 #
10 #
11 # The full license is in the file COPYING.txt, distributed with this software.
11 # The full license is in the file COPYING.txt, distributed with this software.
12 #-----------------------------------------------------------------------------
12 #-----------------------------------------------------------------------------
13
13
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17
17
18 from __future__ import print_function, absolute_import
18 from __future__ import print_function, absolute_import
19
19
20 # Stdlib imports
20 # Stdlib imports
21 import jinja2
21 import jinja2
22 import codecs
22 import codecs
23 import io
23 import io
24 import logging
24 import logging
25 import os
25 import os
26 from IPython.utils import path
26 from IPython.utils import path
27 import pprint
27 import pprint
28 import re
28 import re
29 from types import FunctionType
29 from types import FunctionType
30
30
31 from jinja2 import Environment, PackageLoader, FileSystemLoader
31 from jinja2 import Environment, PackageLoader, FileSystemLoader
32 env = Environment(loader=FileSystemLoader('./templates/'))
32 env = Environment(
33 loader=FileSystemLoader('./templates/'),
34 extensions=['jinja2.ext.loopcontrols']
35 )
33
36
34 # IPython imports
37 # IPython imports
35 from IPython.nbformat import current as nbformat
38 from IPython.nbformat import current as nbformat
36 from IPython.config.configurable import Configurable, SingletonConfigurable
39 from IPython.config.configurable import Configurable, SingletonConfigurable
37 from IPython.utils.traitlets import (List, Unicode, Type, Bool, Dict, CaselessStrEnum,
40 from IPython.utils.traitlets import (List, Unicode, Type, Bool, Dict, CaselessStrEnum,
38 Any)
41 Any)
39
42
40 # Our own imports
43 # Our own imports
41 from IPython.utils.text import indent
44 from IPython.utils.text import indent
42 from .utils import remove_ansi
45 from .utils import remove_ansi
43 from markdown import markdown
46 from markdown import markdown
44 from .utils import highlight,ansi2html
47 from .utils import highlight,ansi2html
45 #-----------------------------------------------------------------------------
48 #-----------------------------------------------------------------------------
46 # Class declarations
49 # Class declarations
47 #-----------------------------------------------------------------------------
50 #-----------------------------------------------------------------------------
48 def rm_fake(strng):
51 def rm_fake(strng):
49 return strng.replace('/files/', '')
52 return strng.replace('/files/', '')
50
53
51 class ConversionException(Exception):
54 class ConversionException(Exception):
52 pass
55 pass
53
56
54
57
55 def python_comment(string):
58 def python_comment(string):
56 return '# '+'\n# '.join(string.split('\n'))
59 return '# '+'\n# '.join(string.split('\n'))
57
60
58
61
59
62
60 def header_body():
63 def header_body():
61 """Return the body of the header as a list of strings."""
64 """Return the body of the header as a list of strings."""
62
65
63 from pygments.formatters import HtmlFormatter
66 from pygments.formatters import HtmlFormatter
64
67
65 header = []
68 header = []
66 static = os.path.join(path.get_ipython_package_dir(),
69 static = os.path.join(path.get_ipython_package_dir(),
67 'frontend', 'html', 'notebook', 'static',
70 'frontend', 'html', 'notebook', 'static',
68 )
71 )
69 here = os.path.split(os.path.realpath(__file__))[0]
72 here = os.path.split(os.path.realpath(__file__))[0]
70 css = os.path.join(static, 'css')
73 css = os.path.join(static, 'css')
71 for sheet in [
74 for sheet in [
72 # do we need jquery and prettify?
75 # do we need jquery and prettify?
73 # os.path.join(static, 'jquery', 'css', 'themes', 'base',
76 # os.path.join(static, 'jquery', 'css', 'themes', 'base',
74 # 'jquery-ui.min.css'),
77 # 'jquery-ui.min.css'),
75 # os.path.join(static, 'prettify', 'prettify.css'),
78 # os.path.join(static, 'prettify', 'prettify.css'),
76 os.path.join(css, 'boilerplate.css'),
79 os.path.join(css, 'boilerplate.css'),
77 os.path.join(css, 'fbm.css'),
80 os.path.join(css, 'fbm.css'),
78 os.path.join(css, 'notebook.css'),
81 os.path.join(css, 'notebook.css'),
79 os.path.join(css, 'renderedhtml.css'),
82 os.path.join(css, 'renderedhtml.css'),
80 # our overrides:
83 # our overrides:
81 os.path.join(here, '..', 'css', 'static_html.css'),
84 os.path.join(here, '..', 'css', 'static_html.css'),
82 ]:
85 ]:
83
86
84 with io.open(sheet, encoding='utf-8') as f:
87 with io.open(sheet, encoding='utf-8') as f:
85 s = f.read()
88 s = f.read()
86 header.append(s)
89 header.append(s)
87
90
88 pygments_css = HtmlFormatter().get_style_defs('.highlight')
91 pygments_css = HtmlFormatter().get_style_defs('.highlight')
89 header.append(pygments_css)
92 header.append(pygments_css)
90 return header
93 return header
91
94
92 inlining= {}
95 inlining= {}
93 inlining['css'] = header_body()
96 inlining['css'] = header_body()
94
97
98
99 def filter_data_type(output):
100 for fmt in ['html', 'pdf', 'svg', 'latex','png', 'jpg','jpeg' , 'text']:
101 if fmt in output:
102 return [fmt]
103
104
105 env.filters['filter_data_type'] = filter_data_type
95 env.filters['pycomment'] = python_comment
106 env.filters['pycomment'] = python_comment
96 env.filters['indent'] = indent
107 env.filters['indent'] = indent
97 env.filters['rm_fake'] = rm_fake
108 env.filters['rm_fake'] = rm_fake
98 env.filters['rm_ansi'] = remove_ansi
109 env.filters['rm_ansi'] = remove_ansi
99 env.filters['markdown'] = markdown
110 env.filters['markdown'] = markdown
100 env.filters['highlight'] = highlight
111 env.filters['highlight'] = highlight
101 env.filters['ansi2html'] = ansi2html
112 env.filters['ansi2html'] = ansi2html
102
113
103 class ConverterTemplate(Configurable):
114 class ConverterTemplate(Configurable):
104
115
105 display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text']
116 display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text']
106 #-------------------------------------------------------------------------
117 #-------------------------------------------------------------------------
107 # Instance-level attributes that are set in the constructor for this
118 # Instance-level attributes that are set in the constructor for this
108 # class.
119 # class.
109 #-------------------------------------------------------------------------
120 #-------------------------------------------------------------------------
110 infile = Any()
121 infile = Any()
111
122
112
123
113 infile_dir = Unicode()
124 infile_dir = Unicode()
114
125
115 def __init__(self, tplfile='fullhtml', config=None, **kw):
126 def __init__(self, tplfile='fullhtml', config=None, **kw):
116 self.template = env.get_template(tplfile+'.tpl')
127 self.template = env.get_template(tplfile+'.tpl')
117 super(ConverterTemplate,self).__init__(config=config)
128 super(ConverterTemplate,self).__init__(config=config)
118
129
119 def _get_prompt_number(self, cell):
130 def _get_prompt_number(self, cell):
120 return cell.prompt_number if hasattr(cell, 'prompt_number') \
131 return cell.prompt_number if hasattr(cell, 'prompt_number') \
121 else self.blank_symbol
132 else self.blank_symbol
122
133
123
134
124 def process(self):
135 def process(self):
125 converted_cells = []
136 converted_cells = []
126 for worksheet in self.nb.worksheets:
137 for worksheet in self.nb.worksheets:
127 for cell in worksheet.cells:
138 for cell in worksheet.cells:
128 cell.type = cell.cell_type
139 cell.type = cell.cell_type
129 cell.haspyout = False
140 cell.haspyout = False
130 for out in cell.get('outputs',[]):
141 for out in cell.get('outputs',[]):
131 if out.output_type == 'pyout':
142 if out.output_type == 'pyout':
132 cell.haspyout = True
143 cell.haspyout = True
133 break
144 break
134 converted_cells.append(worksheet)
145 converted_cells.append(worksheet)
135
146
136 return converted_cells
147 return converted_cells
137
148
138 def convert(self, cell_separator='\n'):
149 def convert(self, cell_separator='\n'):
139 return self.template.render(worksheets=self.process(), inlining=inlining)
150 return self.template.render(worksheets=self.process(), inlining=inlining)
140
151
141
152
142 def read(self, filename):
153 def read(self, filename):
143 "read and parse notebook into NotebookNode called self.nb"
154 "read and parse notebook into NotebookNode called self.nb"
144 with io.open(filename) as f:
155 with io.open(filename) as f:
145 self.nb = nbformat.read(f, 'json')
156 self.nb = nbformat.read(f, 'json')
146
157
@@ -1,10 +1,11 b''
1 #!/usr/bin/env python
1 #!/usr/bin/env python
2 # coding: utf-8
2 # coding: utf-8
3
3 from __future__ import print_function
4 from __future__ import print_function
4 import sys
5 import sys
5 import io
6 import io
6 from converters.template import *
7 from converters.template import *
7 C = ConverterTemplate(tplfile=sys.argv[1])
8 C = ConverterTemplate(tplfile=sys.argv[1])
8 C.read('tests/ipynbref/IntroNumPy.orig.ipynb')
9 C.read(sys.argv[2])
9
10
10 print(C.convert().encode('utf-8'))
11 print(C.convert().encode('utf-8'))
@@ -1,96 +1,121 b''
1 {%- extends 'null.tpl' -%}
1 {%- extends 'display_priority.tpl' -%}
2
2
3
3
4
4
5 {% block codecell %}
5 {% block codecell %}
6 <div class="cell border-box-sizing code_cell vbox">
6 <div class="cell border-box-sizing code_cell vbox">
7 {{ super() }}</div>
7 {{ super() }}</div>
8 {%- endblock codecell %}
8 {%- endblock codecell %}
9
9
10 {% block input_group -%}
10 {% block input_group -%}
11 <div class="input hbox">
11 <div class="input hbox">
12 {{super()}}
12 {{super()}}
13 </div>
13 </div>
14 {% endblock input_group %}
14 {% endblock input_group %}
15
15
16 {% block output_group -%}
16 {% block output_group -%}
17 <div class="vbox output_wrapper">
17 <div class="vbox output_wrapper">
18 <div class="output vbox">
18 <div class="output vbox">
19 <div class="hbox output_area">
19 <div class="hbox output_area">
20 {{ super() }}
20 {{ super() }}
21 </div>
21 </div>
22 </div>
22 </div>
23 </div>
23 </div>
24 {% endblock output_group %}
24 {% endblock output_group %}
25
25
26
26
27 {% block in_prompt -%}
27 {% block in_prompt -%}
28 <div class="prompt input_prompt">In&nbsp;[{{cell.prompt_number}}]:</div>
28 <div class="prompt input_prompt">In&nbsp;[{{cell.prompt_number}}]:</div>
29 {%- endblock in_prompt %}
29 {%- endblock in_prompt %}
30
30
31 {% block output_prompt -%}
31 {% block output_prompt -%}
32 <div class="prompt output_prompt">
32 <div class="prompt output_prompt">
33 {%- if cell.haspyout -%}
33 {%- if cell.haspyout -%}
34 Out[{{cell.prompt_number}}]:
34 Out[{{cell.prompt_number}}]:
35 {%- endif -%}
35 {%- endif -%}
36 </div>
36 </div>
37 {% endblock output_prompt %}
37 {% endblock output_prompt %}
38
38
39 {% block input %}
39 {% block input %}
40 <div class="input_area box-flex1">
40 <div class="input_area box-flex1">
41 {{cell.input | highlight }}
41 {{cell.input | highlight }}
42 </div>
42 </div>
43 {%- endblock input %}
43 {%- endblock input %}
44
44
45
45
46 {% block markdowncell scoped %}
46 {% block markdowncell scoped %}
47 <div class="text_cell_render border-box-sizing rendered_html">
47 <div class="text_cell_render border-box-sizing rendered_html">
48 {{ cell.source | markdown| rm_fake}}
48 {{ cell.source | markdown| rm_fake}}
49 </div>
49 </div>
50 {%- endblock markdowncell %}
50 {%- endblock markdowncell %}
51
51
52 {% block headingcell scoped %}
52 {% block headingcell scoped %}
53 <div class="text_cell_render border-box-sizing rendered_html">
53 <div class="text_cell_render border-box-sizing rendered_html">
54 <h{{cell.level}}>
54 <h{{cell.level}}>
55 {{cell.source}}
55 {{cell.source}}
56 </h{{cell.level}}>
56 </h{{cell.level}}>
57 </div>
57 </div>
58 {% endblock headingcell %}
58 {% endblock headingcell %}
59
59
60 {% block rawcell scoped %}
60 {% block rawcell scoped %}
61 {{ cell.source }}
61 {{ cell.source }}
62 {% endblock rawcell %}
62 {% endblock rawcell %}
63
63
64 {% block unknowncell scoped %}
64 {% block unknowncell scoped %}
65 unknown type {{cell.type}}
65 unknown type {{cell.type}}
66 {% endblock unknowncell %}
66 {% endblock unknowncell %}
67
67
68
68
69 {% block pyout -%}
69 {% block pyout -%}
70 <div class="output_subarea output_pyout">
70 <div class="output_subarea output_pyout">
71 <pre>{{output.text | ansi2html}}</pre>
71 {% block data_priority scoped %}{{ super()}}{% endblock %}
72 </div>
72 </div>
73 {%- endblock pyout %}
73 {%- endblock pyout %}
74
74
75 {% block stream -%}
75 {% block stream -%}
76 <div class="output_subarea output_stream output_stdout">
76 <div class="output_subarea output_stream output_stdout">
77 <pre>{{output.text |ansi2html}}</pre>
77 <pre>{{output.text |ansi2html}}</pre>
78 </div>
78 </div>
79 {%- endblock stream %}
79 {%- endblock stream %}
80
80
81 {% block display_data -%}
81 {% block data_svg -%}
82 <div class="output_subarea output_display_data">
83 {{output.svg}}
82 {{output.svg}}
84 </div>
83 </div>
85 {%- endblock display_data %}
84 {%- endblock data_svg %}
85
86
87 {% block data_html %}
88 {{output.html}}
89 </div>
90 {%- endblock data_html %}
91
92 {% block data_png %}
93 <img src="data:image/png;base64,{{output.png}}"></img>
94 </div>
95 {%- endblock data_png %}
86
96
87
97
98 {% block data_jpg %}
99 <img src="data:image/jpeg;base64,{{output.jpeg}}"></img>
100 </div>
101 {%- endblock data_jpg %}
102
103
104 {% block data_latex %}
105 {{output.latex}}
106 {%- endblock data_latex %}
107
88 {% block pyerr -%}
108 {% block pyerr -%}
89 <div class="output_subarea output_pyerr">
109 <div class="output_subarea output_pyerr">
90 <pre>{{super()}}</pre>
110 <pre>{{super()}}</pre>
91 </div>
111 </div>
92 {%- endblock pyerr %}
112 {%- endblock pyerr %}
93
113
94 {%- block traceback_line %}
114 {%- block traceback_line %}
95 {{line| ansi2html}}
115 {{line| ansi2html}}
96 {%- endblock traceback_line %}
116 {%- endblock traceback_line %}
117
118
119 {%- block data_text %}
120 <pre>{{output.text |Β ansi2html}}</pre>
121 {%- endblock -%}
@@ -1,78 +1,81 b''
1 {#
1 {#
2
2
3 DO NOT USE THIS AS A BASE WORK,
3 DO NOT USE THIS AS A BASE WORK,
4 IF YOU ARE COPY AND PASTING THIS FILE
4 IF YOU ARE COPY AND PASTING THIS FILE
5 YOU ARE PROBABLY DOING THINGS WRONG.
5 YOU ARE PROBABLY DOING THINGS WRONG.
6
6
7 Null template, Does nothing except defining a basic structure
7 Null template, Does nothing except defining a basic structure
8 To layout the diferents blocks of a notebook.
8 To layout the diferents blocks of a notebook.
9
9
10 Subtemplates can Override Blocks to define their custom reresentation.
10 Subtemplates can Override Blocks to define their custom reresentation.
11
11
12 If one of the block you do overrite is not a leave block, consider
12 If one of the block you do overrite is not a leave block, consider
13 calling super.
13 calling super.
14
14
15 {%- block nonLeaveBlock -%}
15 {%- block nonLeaveBlock -%}
16 #add stuff at beginning
16 #add stuff at beginning
17 {{ super() }}
17 {{ super() }}
18 #add stuff at end
18 #add stuff at end
19 {%- endblock nonLeaveBlock -%}
19 {%- endblock nonLeaveBlock -%}
20
20
21 consider calling super even if block is leave block, we might insert more block later.
21 consider calling super even if block is leave block, we might insert more block later.
22
22
23 #}
23 #}
24 {%- block header -%}
24 {%- block header -%}
25 {%- endblock header -%}
25 {%- endblock header -%}
26 {%- block body -%}
26 {%- block body -%}
27 {%- for worksheet in worksheets -%}
27 {%- for worksheet in worksheets -%}
28 {%- for cell in worksheet.cells -%}
28 {%- for cell in worksheet.cells -%}
29 {%- block any_cell scoped -%}
29 {%- block any_cell scoped -%}
30 {%- if cell.type in ['code'] -%}
30 {%- if cell.type in ['code'] -%}
31 {%- block codecell scoped -%}
31 {%- block codecell scoped -%}
32 {%- block input_group -%}
32 {%- block input_group -%}
33 {%- block in_prompt -%}{%- endblock in_prompt -%}
33 {%- block in_prompt -%}{%- endblock in_prompt -%}
34 {%- block input -%}{%- endblock input -%}
34 {%- block input -%}{%- endblock input -%}
35 {%- endblock input_group -%}
35 {%- endblock input_group -%}
36 {%- if cell.outputs -%}
36 {%- if cell.outputs -%}
37 {%- block output_group -%}
37 {%- block output_group -%}
38 {%- block output_prompt -%}{%- endblock output_prompt -%}
38 {%- block output_prompt -%}{%- endblock output_prompt -%}
39 {%- block outputs -%}
39 {%- block outputs -%}
40 {%- for output in cell.outputs -%}
40 {%- for output in cell.outputs -%}
41 {%- if output.output_type in ['pyout'] -%}
41 {%- if output.output_type in ['pyout'] -%}
42 {%- block pyout scoped -%}{%- endblock pyout -%}
42 {%- block pyout scoped -%}{%- endblock pyout -%}
43 {%- elif output.output_type in ['stream'] -%}
43 {%- elif output.output_type in ['stream'] -%}
44 {%- block stream scoped -%}{%- endblock stream -%}
44 {%- block stream scoped -%}{%- endblock stream -%}
45 {%- elif output.output_type in ['display_data'] -%}
45 {%- elif output.output_type in ['display_data'] -%}
46 {%- block display_data scoped -%}{%- endblock display_data -%}
46 {%- block display_data scoped -%}
47 {%- block data_priority scoped -%}
48 {%- endblock data_priority -%}
49 {%- endblock display_data -%}
47 {%- elif output.output_type in ['pyerr'] -%}
50 {%- elif output.output_type in ['pyerr'] -%}
48 {%- block pyerr scoped -%}
51 {%- block pyerr scoped -%}
49 {%- for line in output.traceback -%}
52 {%- for line in output.traceback -%}
50 {%- block traceback_line scoped -%}{%- endblock traceback_line -%}
53 {%- block traceback_line scoped -%}{%- endblock traceback_line -%}
51 {%- endfor -%}
54 {%- endfor -%}
52 {%- endblock pyerr -%}
55 {%- endblock pyerr -%}
53 {%- endif -%}
56 {%- endif -%}
54 {%- endfor -%}
57 {%- endfor -%}
55 {%- endblock outputs -%}
58 {%- endblock outputs -%}
56 {%- endblock output_group -%}
59 {%- endblock output_group -%}
57 {%- endif -%}
60 {%- endif -%}
58 {%- endblock codecell -%}
61 {%- endblock codecell -%}
59 {%- elif cell.type in ['markdown'] -%}
62 {%- elif cell.type in ['markdown'] -%}
60 {%- block markdowncell scoped-%}
63 {%- block markdowncell scoped-%}
61 {%- endblock markdowncell -%}
64 {%- endblock markdowncell -%}
62 {%- elif cell.type in ['heading'] -%}
65 {%- elif cell.type in ['heading'] -%}
63 {%- block headingcell scoped-%}
66 {%- block headingcell scoped-%}
64 {%- endblock headingcell -%}
67 {%- endblock headingcell -%}
65 {%- elif cell.type in ['raw'] -%}
68 {%- elif cell.type in ['raw'] -%}
66 {%- block rawcell scoped-%}
69 {%- block rawcell scoped-%}
67 {%- endblock rawcell -%}
70 {%- endblock rawcell -%}
68 {%- else -%}
71 {%- else -%}
69 {%- block unknowncell scoped-%}
72 {%- block unknowncell scoped-%}
70 {%- endblock unknowncell -%}
73 {%- endblock unknowncell -%}
71 {%- endif -%}
74 {%- endif -%}
72 {%- endblock any_cell -%}
75 {%- endblock any_cell -%}
73 {%- endfor -%}
76 {%- endfor -%}
74 {%- endfor -%}
77 {%- endfor -%}
75 {%- endblock body -%}
78 {%- endblock body -%}
76
79
77 {%- block footer -%}
80 {%- block footer -%}
78 {%- endblock footer -%}
81 {%- endblock footer -%}
General Comments 0
You need to be logged in to leave comments. Login now