Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from4 commits
1e156c8
2a82c32
71d4c19
efd3168
b09de9c
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1595,6 +1595,73 @@ def __init__(self, figure): | ||
self.toolbar = None # NavigationToolbar2 will set me | ||
self._is_idle_drawing = False | ||
def _repr_html_(self): | ||
if not self.figure.axes and not self.figure.lines: | ||
return | ||
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. What if the figure only contains a Rectangle an arrow and a Text? 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. True, I copied that from elsewhere. Is there a clean way to determine if the figure has changed at all? 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. Checkhttps://matplotlib.org/faq/howto_faq.html#check-whether-a-figure-is-empty 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. What do you mean "changed"? I agree with@ImportanceOfBeingErnest if we are going to have a repr, it should always showsomething even if that something is an empty image. | ||
dpi = self.figure.dpi | ||
is_retina = False | ||
import IPython | ||
ip = IPython.get_ipython() | ||
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. This would fail whenever IPython is not available. 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. Wouldn't 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. I guess IPython invented this; but in principle anything can try to call such method if it's available. 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. The other way you can check this is | ||
ib_list = [c for c in ip.configurables | ||
if 'InlineBackend' in type(c).__name__] | ||
# having an 'inline' backend doesn't mean '%matplotlib inline' has been run | ||
# Running %matplotlib inline runs pylabtools.configure_inline_support | ||
# which appends the InlineBackend to the list of configurables | ||
if get_backend() == 'module://ipykernel.pylab.backend_inline' and ib_list: | ||
ib = ib_list[0] | ||
bbox_inches = ib.print_figure_kwargs['bbox_inches'] | ||
fmt = next(iter(ib.figure_formats)) | ||
if fmt == 'retina': | ||
is_retina = True | ||
dpi = dpi * 2 | ||
fmt = self.get_default_filetype() | ||
else: | ||
bbox_inches = 'tight' # how to let user choose self.figure.bbox_inches? | ||
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. This just should not be passed or set to None. That IPython inline defaults to | ||
fmt = self.get_default_filetype() | ||
if fmt not in {'png', 'svg', 'jpg', 'pdf'}: | ||
fmt = 'png' | ||
kw = { | ||
"format":fmt, | ||
"facecolor":self.figure.get_facecolor(), | ||
"edgecolor":self.figure.get_edgecolor(), | ||
"dpi":dpi, | ||
"bbox_inches":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 IPython.core.display import _pngxy, _jpegxy | ||
if fmt == 'svg': | ||
return raw_bytes.decode() | ||
elif fmt == 'png': | ||
w, h = _pngxy(raw_bytes) | ||
elif fmt == 'jpg': | ||
w, h = _jpegxy(raw_bytes) | ||
elif fmt == 'pdf': | ||
w, h = self.figure.get_size_inches() | ||
w, h = w * dpi, h * dpi | ||
if is_retina: | ||
w, h = w // 2, h // 2 | ||
from base64 import b64encode | ||
data = b64encode(raw_bytes).decode() | ||
if fmt == 'png': | ||
return f'<img width="{w}" height="{h}" src="data:image/png;base64, {data}" />' | ||
elif fmt == 'pdf': | ||
return f'<embed width="{w}" height="{h}" src="data:application/pdf;base64, {data}">' | ||
elif fmt == 'jpg': | ||
return f'<img width="{w}" height="{h}" src="data:image/jpeg;base64, {data}" />' | ||
@classmethod | ||
@functools.lru_cache() | ||
def _fix_ipython_backend2gui(cls): | ||