|
30 | 30 | import sys
|
31 | 31 | import traceback
|
32 | 32 | import codeop
|
| 33 | +import base64 |
33 | 34 |
|
34 | 35 | from sphinx.errors import ExtensionError
|
35 | 36 | import sphinx.util
|
@@ -174,6 +175,21 @@ def __exit__(self, type_, value, tb):
|
174 | 175 | <br />
|
175 | 176 | <br />"""
|
176 | 177 |
|
| 178 | +HTML_BASE64_HEADER = """.. raw:: html |
| 179 | +
|
| 180 | + <div class="output_subarea output_html rendered_html output_result"> |
| 181 | + <img src="data:image/{0};base64,{1}"> |
| 182 | + </div> |
| 183 | + <br /> |
| 184 | + <br />""" |
| 185 | + |
| 186 | +HTML_SVG_HEADER = """.. raw:: html |
| 187 | +
|
| 188 | + <div class="output_subarea output_html rendered_html output_result"> |
| 189 | +{0} |
| 190 | + </div> |
| 191 | + <br /> |
| 192 | + <br />""" |
177 | 193 |
|
178 | 194 | def codestr2rst(codestr, lang='python', lineno=None):
|
179 | 195 | """Return reStructuredText code block from code string."""
|
@@ -741,7 +757,8 @@ def _get_last_repr(capture_repr, ___):
|
741 | 757 | last_repr = None
|
742 | 758 | repr_meth = None
|
743 | 759 | else:
|
744 |
| - if isinstance(last_repr, str): |
| 760 | + if (isinstance(last_repr, str) or |
| 761 | + isinstance(last_repr, bytes) and meth in ('_repr_png',)): |
745 | 762 | break
|
746 | 763 | return last_repr, repr_meth
|
747 | 764 |
|
@@ -780,6 +797,14 @@ def _get_code_output(is_last_expr, example_globals, gallery_conf, logging_tee,
|
780 | 797 | # give html output its own header
|
781 | 798 | if repr_meth == '_repr_html_':
|
782 | 799 | captured_html = HTML_HEADER.format(indent(last_repr, ' ' * 4))
|
| 800 | + elif repr_meth == '_repr_png_': |
| 801 | + captured_html = HTML_BASE64_HEADER.format( |
| 802 | + 'png', base64.b64encode(last_repr).decode()) |
| 803 | + elif repr_meth == '_repr_jpeg_': |
| 804 | + captured_html = HTML_BASE64_HEADER.format( |
| 805 | + 'jpeg', base64.b64encode(last_repr).decode()) |
| 806 | + elif repr_meth == '_repr_svg_': |
| 807 | + captured_html = HTML_SVG_HEADER.format(indent(last_repr, ' ' * 4)) |
783 | 808 | else:
|
784 | 809 | captured_html = ''
|
785 | 810 |
|
|
0 commit comments