##// 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 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 jinja2
22 22 import codecs
23 23 import io
24 24 import logging
25 25 import os
26 26 from IPython.utils import path
27 27 import pprint
28 28 import re
29 29 from types import FunctionType
30 30
31 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 37 # IPython imports
35 38 from IPython.nbformat import current as nbformat
36 39 from IPython.config.configurable import Configurable, SingletonConfigurable
37 40 from IPython.utils.traitlets import (List, Unicode, Type, Bool, Dict, CaselessStrEnum,
38 41 Any)
39 42
40 43 # Our own imports
41 44 from IPython.utils.text import indent
42 45 from .utils import remove_ansi
43 46 from markdown import markdown
44 47 from .utils import highlight,ansi2html
45 48 #-----------------------------------------------------------------------------
46 49 # Class declarations
47 50 #-----------------------------------------------------------------------------
48 51 def rm_fake(strng):
49 52 return strng.replace('/files/', '')
50 53
51 54 class ConversionException(Exception):
52 55 pass
53 56
54 57
55 58 def python_comment(string):
56 59 return '# '+'\n# '.join(string.split('\n'))
57 60
58 61
59 62
60 63 def header_body():
61 64 """Return the body of the header as a list of strings."""
62 65
63 66 from pygments.formatters import HtmlFormatter
64 67
65 68 header = []
66 69 static = os.path.join(path.get_ipython_package_dir(),
67 70 'frontend', 'html', 'notebook', 'static',
68 71 )
69 72 here = os.path.split(os.path.realpath(__file__))[0]
70 73 css = os.path.join(static, 'css')
71 74 for sheet in [
72 75 # do we need jquery and prettify?
73 76 # os.path.join(static, 'jquery', 'css', 'themes', 'base',
74 77 # 'jquery-ui.min.css'),
75 78 # os.path.join(static, 'prettify', 'prettify.css'),
76 79 os.path.join(css, 'boilerplate.css'),
77 80 os.path.join(css, 'fbm.css'),
78 81 os.path.join(css, 'notebook.css'),
79 82 os.path.join(css, 'renderedhtml.css'),
80 83 # our overrides:
81 84 os.path.join(here, '..', 'css', 'static_html.css'),
82 85 ]:
83 86
84 87 with io.open(sheet, encoding='utf-8') as f:
85 88 s = f.read()
86 89 header.append(s)
87 90
88 91 pygments_css = HtmlFormatter().get_style_defs('.highlight')
89 92 header.append(pygments_css)
90 93 return header
91 94
92 95 inlining= {}
93 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 106 env.filters['pycomment'] = python_comment
96 107 env.filters['indent'] = indent
97 108 env.filters['rm_fake'] = rm_fake
98 109 env.filters['rm_ansi'] = remove_ansi
99 110 env.filters['markdown'] = markdown
100 111 env.filters['highlight'] = highlight
101 112 env.filters['ansi2html'] = ansi2html
102 113
103 114 class ConverterTemplate(Configurable):
104 115
105 116 display_data_priority = ['pdf', 'svg', 'png', 'jpg', 'text']
106 117 #-------------------------------------------------------------------------
107 118 # Instance-level attributes that are set in the constructor for this
108 119 # class.
109 120 #-------------------------------------------------------------------------
110 121 infile = Any()
111 122
112 123
113 124 infile_dir = Unicode()
114 125
115 126 def __init__(self, tplfile='fullhtml', config=None, **kw):
116 127 self.template = env.get_template(tplfile+'.tpl')
117 128 super(ConverterTemplate,self).__init__(config=config)
118 129
119 130 def _get_prompt_number(self, cell):
120 131 return cell.prompt_number if hasattr(cell, 'prompt_number') \
121 132 else self.blank_symbol
122 133
123 134
124 135 def process(self):
125 136 converted_cells = []
126 137 for worksheet in self.nb.worksheets:
127 138 for cell in worksheet.cells:
128 139 cell.type = cell.cell_type
129 140 cell.haspyout = False
130 141 for out in cell.get('outputs',[]):
131 142 if out.output_type == 'pyout':
132 143 cell.haspyout = True
133 144 break
134 145 converted_cells.append(worksheet)
135 146
136 147 return converted_cells
137 148
138 149 def convert(self, cell_separator='\n'):
139 150 return self.template.render(worksheets=self.process(), inlining=inlining)
140 151
141 152
142 153 def read(self, filename):
143 154 "read and parse notebook into NotebookNode called self.nb"
144 155 with io.open(filename) as f:
145 156 self.nb = nbformat.read(f, 'json')
146 157
@@ -1,10 +1,11 b''
1 1 #!/usr/bin/env python
2 2 # coding: utf-8
3
3 4 from __future__ import print_function
4 5 import sys
5 6 import io
6 7 from converters.template import *
7 8 C = ConverterTemplate(tplfile=sys.argv[1])
8 C.read('tests/ipynbref/IntroNumPy.orig.ipynb')
9 C.read(sys.argv[2])
9 10
10 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 5 {% block codecell %}
6 6 <div class="cell border-box-sizing code_cell vbox">
7 7 {{ super() }}</div>
8 8 {%- endblock codecell %}
9 9
10 10 {% block input_group -%}
11 11 <div class="input hbox">
12 12 {{super()}}
13 13 </div>
14 14 {% endblock input_group %}
15 15
16 16 {% block output_group -%}
17 17 <div class="vbox output_wrapper">
18 18 <div class="output vbox">
19 19 <div class="hbox output_area">
20 20 {{ super() }}
21 21 </div>
22 22 </div>
23 23 </div>
24 24 {% endblock output_group %}
25 25
26 26
27 27 {% block in_prompt -%}
28 28 <div class="prompt input_prompt">In&nbsp;[{{cell.prompt_number}}]:</div>
29 29 {%- endblock in_prompt %}
30 30
31 31 {% block output_prompt -%}
32 32 <div class="prompt output_prompt">
33 33 {%- if cell.haspyout -%}
34 34 Out[{{cell.prompt_number}}]:
35 35 {%- endif -%}
36 36 </div>
37 37 {% endblock output_prompt %}
38 38
39 39 {% block input %}
40 40 <div class="input_area box-flex1">
41 41 {{cell.input | highlight }}
42 42 </div>
43 43 {%- endblock input %}
44 44
45 45
46 46 {% block markdowncell scoped %}
47 47 <div class="text_cell_render border-box-sizing rendered_html">
48 48 {{ cell.source | markdown| rm_fake}}
49 49 </div>
50 50 {%- endblock markdowncell %}
51 51
52 52 {% block headingcell scoped %}
53 53 <div class="text_cell_render border-box-sizing rendered_html">
54 54 <h{{cell.level}}>
55 55 {{cell.source}}
56 56 </h{{cell.level}}>
57 57 </div>
58 58 {% endblock headingcell %}
59 59
60 60 {% block rawcell scoped %}
61 61 {{ cell.source }}
62 62 {% endblock rawcell %}
63 63
64 64 {% block unknowncell scoped %}
65 65 unknown type {{cell.type}}
66 66 {% endblock unknowncell %}
67 67
68 68
69 69 {% block pyout -%}
70 70 <div class="output_subarea output_pyout">
71 <pre>{{output.text | ansi2html}}</pre>
71 {% block data_priority scoped %}{{ super()}}{% endblock %}
72 72 </div>
73 73 {%- endblock pyout %}
74 74
75 75 {% block stream -%}
76 76 <div class="output_subarea output_stream output_stdout">
77 77 <pre>{{output.text |ansi2html}}</pre>
78 78 </div>
79 79 {%- endblock stream %}
80 80
81 {% block display_data -%}
82 <div class="output_subarea output_display_data">
81 {% block data_svg -%}
83 82 {{output.svg}}
84 83 </div>
85 {%- endblock display_data %}
84 {%- endblock data_svg %}
86 85
87 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 %}
96
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 108 {% block pyerr -%}
89 109 <div class="output_subarea output_pyerr">
90 110 <pre>{{super()}}</pre>
91 111 </div>
92 112 {%- endblock pyerr %}
93 113
94 114 {%- block traceback_line %}
95 115 {{line| ansi2html}}
96 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 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 27 {%- for worksheet in 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 -%}{%- endblock stream -%}
45 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 50 {%- elif output.output_type in ['pyerr'] -%}
48 51 {%- block pyerr scoped -%}
49 52 {%- for line in output.traceback -%}
50 53 {%- block traceback_line scoped -%}{%- endblock traceback_line -%}
51 54 {%- endfor -%}
52 55 {%- endblock pyerr -%}
53 56 {%- endif -%}
54 57 {%- endfor -%}
55 58 {%- endblock outputs -%}
56 59 {%- endblock output_group -%}
57 60 {%- endif -%}
58 61 {%- endblock codecell -%}
59 62 {%- elif cell.type in ['markdown'] -%}
60 63 {%- block markdowncell scoped-%}
61 64 {%- endblock markdowncell -%}
62 65 {%- elif cell.type in ['heading'] -%}
63 66 {%- block headingcell scoped-%}
64 67 {%- endblock headingcell -%}
65 68 {%- elif cell.type in ['raw'] -%}
66 69 {%- block rawcell scoped-%}
67 70 {%- endblock rawcell -%}
68 71 {%- else -%}
69 72 {%- block unknowncell scoped-%}
70 73 {%- endblock unknowncell -%}
71 74 {%- endif -%}
72 75 {%- endblock any_cell -%}
73 76 {%- endfor -%}
74 77 {%- endfor -%}
75 78 {%- endblock body -%}
76 79
77 80 {%- block footer -%}
78 81 {%- endblock footer -%}
General Comments 0
You need to be logged in to leave comments. Login now