-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for capturing svg, png, and jpeg and _repr_mimebundle_
- Loading branch information
Showing
14 changed files
with
270 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
sphinx_gallery/tests/tinybuild/examples/plot_mime_bundle.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" | ||
Test using _repr_mimebundle_ | ||
===================================== | ||
Test repr capturing via ``_repr_mimebundle_`` and make sure that the | ||
``capture_repr`` ordering is honored as well as ``_repr_mimebundle_`` | ||
over other ``_repr_*_`` having precedence. | ||
""" | ||
# sphinx_gallery_capture_repr = ('_repr_svg_', '_repr_html_') | ||
# %% | ||
# First define a class with only an SVG in the MIME bundle | ||
|
||
|
||
class A: | ||
def _repr_mimebundle_(self, **kwargs): | ||
return {"image/svg+xml": """ | ||
<svg viewBox="0 0 50 50" xmlns="http://www.w3.org/2000/svg"> | ||
<line x1="0" y1="0" x2="50" y2="50" stroke="black" /> | ||
</svg> | ||
"""} | ||
|
||
|
||
A() | ||
|
||
|
||
# %% | ||
# Then, only a HTML representation, but both ``_repr_html_`` and | ||
# ``_repr_mimebundle_``. | ||
class B: | ||
def _repr_html_(self): | ||
return '<p><b>This should not print</b></p>' | ||
|
||
def _repr_mimebundle_(self, **kwargs): | ||
# Breaking the string here, so one can use the sentence to | ||
# check the correct HTML output | ||
return {"text/html": '<p><b>This should' | ||
' actually print</b></p>'} | ||
|
||
|
||
B() | ||
|
||
|
||
# %% | ||
# Then, both SVG and HTML. The SVG should be selected based on the order. | ||
class C: | ||
def _repr_mimebundle_(self, **kwargs): | ||
return {"image/svg+xml": """ | ||
<svg viewBox="0 0 50 50" xmlns="http://www.w3.org/2000/svg"> | ||
<line x1="0" y1="0" x2="50" y2="50" stroke="black" /> | ||
</svg> | ||
""", | ||
"text/html": '<p><b>This should not print</b></p>'} | ||
|
||
|
||
C() | ||
|
||
|
||
# %% | ||
# Finally, a separate ``_repr_svg_`` that should be selected since | ||
# ``_repr_mimebundle_`` only includes HTML. | ||
class D: | ||
def _repr_svg_(self): | ||
return """ | ||
<svg viewBox="0 0 50 50" xmlns="http://www.w3.org/2000/svg"> | ||
<line x1="0" y1="0" x2="50" y2="50" stroke="black" /> | ||
</svg> | ||
""" | ||
|
||
def _repr_mimebundle_(self, **kwargs): | ||
return {"text/html": '<p><b>This should not print</b></p>'} | ||
|
||
|
||
D() |
26 changes: 26 additions & 0 deletions
26
sphinx_gallery/tests/tinybuild/examples/plot_random_jpg.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
""" | ||
Capture JPG test | ||
================ | ||
Test that it is possible to capture a JPG from a class with a ``_repr_jpeg__`` | ||
method. | ||
""" | ||
|
||
|
||
# sphinx_gallery_capture_repr = ('_repr_jpeg_',) | ||
import io | ||
import numpy | ||
from PIL import Image | ||
|
||
|
||
class RandomImage: | ||
def __init__(self): | ||
imarray = numpy.random.rand(160, 160, 3) * 255 | ||
self._image = Image.fromarray(imarray.astype('uint8')).convert('RGB') | ||
|
||
def _repr_jpeg_(self): | ||
b = io.BytesIO() | ||
self._image.save(b, "JPEG") | ||
return b.getvalue() | ||
|
||
|
||
RandomImage() |
Oops, something went wrong.