8000 Add support for capturing svg and png · sphinx-gallery/sphinx-gallery@4ee59d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4ee59d9

Browse files
committed
Add support for capturing svg and png
1 parent 7bd5101 commit 4ee59d9

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

sphinx_gallery/gen_gallery.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def __call__(self, gallery_conf, script_vars):
6363
'doc_module': (),
6464
'exclude_implicit_doc': {},
6565
'reference_url': {},
66-
'capture_repr': ('_repr_html_', '__repr__'),
66+
'capture_repr': ('_repr_html_', '_repr_svg_', '_repr_png_', '__repr__'),
6767
'ignore_repr_types': r'',
6868
# Build options
6969
# -------------
@@ -171,7 +171,8 @@ def _fill_gallery_conf_defaults(sphinx_gallery_conf, app=None,
171171

172172
# Check capture_repr
173173
capture_repr = gallery_conf['capture_repr']
174-
supported_reprs = ['__repr__', '__str__', '_repr_html_']
174+
supported_reprs = ('__repr__', '__str__', '_repr_html_', '_repr_png_',
175+
'_repr_svg_')
175176
if isinstance(capture_repr, tuple):
176177
for rep in capture_repr:
177178
if rep not in supported_reprs:

sphinx_gallery/gen_rst.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import sys
3131
import traceback
3232
import codeop
33+
import base64
3334

3435
from sphinx.errors import ExtensionError
3536
import sphinx.util
@@ -174,6 +175,21 @@ def __exit__(self, type_, value, tb):
174175
<br />
175176
<br />"""
176177

178+
HTML_PNG_HEADER = """.. raw:: html
179+
180+
<div class="output_subarea output_html rendered_html output_result">
181+
<img src="data:image/png;base64,{0}">
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 />"""
177193

178194
def codestr2rst(codestr, lang='python', lineno=None):
179195
"""Return reStructuredText code block from code string."""
@@ -741,7 +757,8 @@ def _get_last_repr(capture_repr, ___):
741757
last_repr = None
742758
repr_meth = None
743759
else:
744-
if isinstance(last_repr, str):
760+
if (isinstance(last_repr, str) or
761+
isinstance(last_repr, bytes) and meth in ('_repr_png',)):
745762
break
746763
return last_repr, repr_meth
747764

@@ -780,6 +797,11 @@ def _get_code_output(is_last_expr, example_globals, gallery_conf, logging_tee,
780797
# give html output its own header
781798
if repr_meth == '_repr_html_':
782799
captured_html = HTML_HEADER.format(indent(last_repr, ' ' * 4))
800+
elif repr_meth == '_repr_png_':
801+
captured_html = HTML_PNG_HEADER.format(
802+
base64.b64encode(last_repr).decode())
803+
elif repr_meth == '_repr_svg_':
804+
captured_html = HTML_SVG_HEADER.format(indent(last_repr, ' ' * 4))
783805
else:
784806
captured_html = ''
785807

0 commit comments

Comments
 (0)
0