-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Adding png image return for inline backend figures with _repr_html_ #16788
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1e156c8
2a82c32
71d4c19
efd3168
b09de9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1595,6 +1595,45 @@ def __init__(self, figure): | |
self.toolbar = None # NavigationToolbar2 will set me | ||
self._is_idle_drawing = False | ||
|
||
def _repr_html_(self): | ||
# Defer to IPython to handle html output if possible | ||
if 'IPython' in sys.modules: | ||
import IPython | ||
ip = IPython.get_ipython() | ||
# Check whether %matplotlib was run. Is there a better way? | ||
ib_list = [c for c in ip.configurables | ||
if 'InlineBackend' in type(c).__name__] | ||
if ib_list: | ||
return | ||
|
||
fmt = self.get_default_filetype() | ||
|
||
kw = { | ||
"format":fmt, | ||
"facecolor":self.figure.get_facecolor(), | ||
"edgecolor":self.figure.get_edgecolor(), | ||
"dpi":self.figure.dpi, | ||
"bbox_inches":self.figure.bbox_inches | ||
} | ||
|
||
bytes_io = io.BytesIO() | ||
self.print_figure(bytes_io, **kw) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can just pass the kwargs here (having a separate dict doesn't seem to buy much) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and you need to force the format here. again, bytes_io could easily contain a postscript image here, for example. |
||
raw_bytes = bytes_io.getvalue() | ||
|
||
from base64 import b64encode | ||
data = b64encode(raw_bytes).decode() | ||
|
||
if fmt == 'svg': | ||
return raw_bytes.decode() | ||
elif fmt == 'png': | ||
return f'<img src="data:image/png;base64, {data}" />' | ||
elif fmt == 'pdf': | ||
w, h = self.figure.get_size_inches() | ||
w, h = w * self.figure.dpi, h * self.figure.dpi | ||
return f'<embed width="{w}" height="{h}" src="data:application/pdf;base64, {data}">' | ||
elif fmt == 'jpg': | ||
return f'<img src="data:image/jpeg;base64, {data}" />' | ||
|
||
@classmethod | ||
@functools.lru_cache() | ||
def _fix_ipython_backend2gui(cls): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just dived into IPython itself. It looks like we are also storing the sate on a function attribute (!!!).
Not sure it is better though.