Skip to content

Commit ae50ed1

Browse files
committed
Separate "display priority" for HTML and LaTeX
Closes #20.
1 parent 676bedb commit ae50ed1

File tree

2 files changed

+72
-17
lines changed

2 files changed

+72
-17
lines changed

doc/code-cells.ipynb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
"source": [
1616
"# Code Cells\n",
1717
"\n",
18+
"## Code, Output, Streams\n",
19+
"\n",
1820
"An empty code cell:"
1921
]
2022
},
@@ -163,7 +165,7 @@
163165
},
164166
"outputs": [],
165167
"source": [
166-
"from IPython.display import display, Image, SVG, Math, YouTubeVideo"
168+
"from IPython.display import display"
167169
]
168170
},
169171
{
@@ -181,6 +183,7 @@
181183
},
182184
"outputs": [],
183185
"source": [
186+
"from IPython.display import Image\n",
184187
"i = Image(filename='images/notebook_icon.png')\n",
185188
"i"
186189
]
@@ -211,6 +214,7 @@
211214
},
212215
"outputs": [],
213216
"source": [
217+
"from IPython.display import SVG\n",
214218
"SVG(filename='images/python_logo.svg')"
215219
]
216220
},
@@ -291,6 +295,7 @@
291295
},
292296
"outputs": [],
293297
"source": [
298+
"from IPython.display import Math\n",
294299
"eq = Math(r\"\\int_{-\\infty}^\\infty f(x) \\delta(x - x_0) dx = f(x_0)\")\n",
295300
"eq"
296301
]
@@ -335,6 +340,7 @@
335340
},
336341
"outputs": [],
337342
"source": [
343+
"from IPython.display import YouTubeVideo\n",
338344
"YouTubeVideo('WAikxUGbomY')"
339345
]
340346
},

nbsphinx.py

Lines changed: 65 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@
4343

4444
_ipynbversion = 4
4545

46+
# See nbconvert/exporters/html.py:
47+
DISPLAY_DATA_PRIORITY_HTML = (
48+
'application/javascript',
49+
'text/html',
50+
'text/markdown',
51+
'image/svg+xml',
52+
'text/latex',
53+
'image/png',
54+
'image/jpeg',
55+
'text/plain',
56+
)
57+
# See nbconvert/exporters/latex.py:
58+
DISPLAY_DATA_PRIORITY_LATEX = (
59+
'text/latex',
60+
'application/pdf',
61+
'image/png',
62+
'image/jpeg',
63+
'image/svg+xml',
64+
'text/markdown',
65+
'text/plain',
66+
)
67+
4668
RST_TEMPLATE = """
4769
{% extends 'rst.tpl' %}
4870
@@ -85,17 +107,7 @@
85107
{% endblock input %}
86108
87109
88-
{% block nboutput %}
89-
{%- if output.output_type == 'stream' %}
90-
{%- set datatype = 'ansi' %}
91-
{%- set outputdata = output.text[:-1] %}{# trailing \n is stripped #}
92-
{%- elif output.output_type == 'error' %}
93-
{%- set datatype = 'ansi' %}
94-
{%- set outputdata = '\n'.join(output.traceback) %}
95-
{%- else %}
96-
{%- set datatype = (output.data | filter_data_type)[0] %}
97-
{%- set outputdata = output.data[datatype] %}
98-
{%- endif -%}
110+
{% macro insert_nboutput(datatype, output, cell) -%}
99111
.. nboutput::
100112
{%- if datatype == 'text/plain' %}{# nothing #}
101113
{%- else %} rst
@@ -110,12 +122,12 @@
110122
:class: stderr
111123
{%- endif %}
112124
{%- if datatype == 'text/plain' -%}
113-
{{ insert_empty_lines(outputdata) }}
125+
{{ insert_empty_lines(output.data[datatype]) }}
114126
115-
{{ outputdata.strip(\n) | indent }}
127+
{{ output.data[datatype].strip(\n) | indent }}
116128
{%- elif datatype in ['image/svg+xml', 'image/png', 'image/jpeg', 'application/pdf'] %}
117129
118-
.. image:: {{ output.metadata.filenames[datatype].rsplit('.', 1)[0] + '.*' | posix_path }}
130+
.. image:: {{ output.metadata.filenames[datatype] | posix_path }}
119131
{%- elif datatype in ['text/markdown'] %}
120132
121133
{{ output.data['text/markdown'] | markdown2rst | indent }}
@@ -136,19 +148,34 @@
136148
.. raw:: html
137149
138150
<pre>
139-
{{ outputdata | ansi2html | indent | indent }}
151+
{{ output.data[datatype] | ansi2html | indent | indent }}
140152
</pre>
141153
142154
.. raw:: latex
143155
144156
% This comment is needed to force a line break for adjacent ANSI cells
145157
\\begin{OriginalVerbatim}[commandchars=\\\\\\{\\}]
146-
{{ outputdata | ansi2latex | indent | indent }}
158+
{{ output.data[datatype] | ansi2latex | indent | indent }}
147159
\\end{OriginalVerbatim}
148160
{% else %}
149161
150162
WARNING! Data type not implemented: {{ datatype }}
151163
{%- endif %}
164+
{% endmacro %}
165+
166+
167+
{% block nboutput -%}
168+
{%- set html_datatype, latex_datatype = output | get_output_type %}
169+
{%- if html_datatype == latex_datatype %}
170+
{{ insert_nboutput(html_datatype, output, cell) }}
171+
{%- else %}
172+
.. only:: html
173+
174+
{{ insert_nboutput(html_datatype, output, cell) | indent }}
175+
.. only:: latex
176+
177+
{{ insert_nboutput(latex_datatype, output, cell) | indent }}
178+
{%- endif %}
152179
{% endblock nboutput %}
153180
154181
@@ -411,6 +438,7 @@ def __init__(self, execute='auto', allow_errors=False, timeout=30,
411438
'markdown2rst': markdown2rst,
412439
'get_empty_lines': _get_empty_lines,
413440
'extract_toctree': _extract_toctree,
441+
'get_output_type': _get_output_type,
414442
})
415443

416444
def from_notebook_node(self, nb, resources=None, **kw):
@@ -747,6 +775,27 @@ def _get_empty_lines(text):
747775
return before, after
748776

749777

778+
def _get_output_type(output):
779+
"""Choose appropriate output data types for HTML and LaTeX."""
780+
if output.output_type == 'stream':
781+
html_datatype = latex_datatype = 'ansi'
782+
text = output.text
783+
output.data = {'ansi': text[:-1] if text.endswith('\n') else text}
784+
elif output.output_type == 'error':
785+
html_datatype = latex_datatype = 'ansi'
786+
output.data = {'ansi': '\n'.join(output.traceback)}
787+
else:
788+
for datatype in DISPLAY_DATA_PRIORITY_HTML:
789+
if datatype in output.data:
790+
html_datatype = datatype
791+
break
792+
for datatype in DISPLAY_DATA_PRIORITY_LATEX:
793+
if datatype in output.data:
794+
latex_datatype = datatype
795+
break
796+
return html_datatype, latex_datatype
797+
798+
750799
def _set_empty_lines(node, options):
751800
"""Set "empty lines" attributes on a CodeNode.
752801

0 commit comments

Comments
 (0)