Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
30 changes: 30 additions & 0 deletions itermplot/ipython/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
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, _):
imgcat(img, fn=fn)

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"))