Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ipython mimetypes extension #58

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@ Type "help", "copyright", "credits" or "license" for more information.

You should see a plot!

### image mimetype support

If you want to display a wider variety of objects using ipython's mimetype capturing, load our bundled ipython extension:

```ipython
$ %load_ext itermplot.ipython
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're okay with adding load_ipython_extension to itermplot/__init__.py, it'd make this a cleaner %load_ext itermplot

$ from PIL import Image
$ Image.open("my_cool_image.png")
# your image should be displayed
```

## Uninstall

You can disable this backend by unsetting the `MPLBACKEND` environment variable.
Expand Down
2 changes: 1 addition & 1 deletion itermplot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def rev(c):
return x


def imgcat(data, fn="plot.pdf"):
def imgcat(data: bytes, fn="plot.pdf"):
"""Output the image data to the iTerm2 console. If `lines` is greater
than zero then advance the console `lines` number of blank lines, move
back, and then output the image. This is the default behaviour if TMUX
Expand Down
39 changes: 39 additions & 0 deletions itermplot/ipython/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
ipython extensions for image output handling.
e.g.

```ipython
%load_ext itermplot.ipython
```

implemented based on https://ipython.readthedocs.io/en/stable/config/shell_mimerenderer.html
"""

from .. import imgcat


def register_mimerenderer(ipython, mime, handler):
ipython.display_formatter.active_types.append(mime)
ipython.display_formatter.formatters[mime].enabled = True
ipython.mime_renderers[mime] = handler


def imgcat_factory(fn):
def _wrapper(img, _):
if isinstance(img, bytes):
return imgcat(img, fn=fn)
elif hasattr(img, "read"):
return imgcat(img.read(), fn=fn)
elif isinstance(img, str):
return imgcat(img.encode(), fn=fn)
else:
raise ValueError(
"imgcat only accepts bytes, file-like objects, and strings"
)

return _wrapper


def load_ipython_extension(ipython):
register_mimerenderer(ipython, "image/png", imgcat_factory(fn="img.png"))
register_mimerenderer(ipython, "image/jpeg", imgcat_factory(fn="img.jpg"))