Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.
Sign upSupport for image/png format in the terminal #10610
Conversation
This comment has been minimized.
This comment has been minimized.
|
I'm on my phone so quick comment :
I think some of this is already available as extensions like
https://github.com/asmeurer/iterm2-tools/
…On May 28, 2017 12:36 PM, "Stephan Houben" ***@***.***> wrote:
Introduction
There are nowadays several terminal emulators which support showing images
in the terminal.
On such terminal emulators the terminal IPython version can support the
image/png mime type.
The following is a summary of such terminal emulators to the extend that I
am aware of them.
- ITerm2 on macOS
- Terminology, the terminal emulator of Enlightenment
- All terminal emulators which support the Sixel protocol
<https://en.wikipedia.org/wiki/Sixel>
In all these cases there is a simple program which takes the name of the
image file and just displays
it on the terminal. E.g. for ITerm2 there is the catimg script
<https://github.com/asmeurer/catimg> which used like this:
catimg someimage.png
Similar (but unfortunately different) programs exist for the other
terminal emulators mentioned.
This pull request introduces an environment variable IPYTHON_IMAGE_VIEWER
. When set, the terminal IPython will use the named program to view an
image of type image/png .
Unresolved issues
- I decided to use an environment variable and not a configurable.
This is because IPYTHON_IMAGE_VIEWER really describes a property of the
terminal and is therefore a bit analogous to TERM. However this is probably
something to be discussed.
- It should now be possible to use %matplotlib inline in the terminal
but I couldn't figure out how to enable that.
- I am unsure how to write a testcase for this.
------------------------------
You can view, comment on, or merge this pull request online at:
#10610
Commit Summary
- Support for image/png formats in the terminal if environment
variable is set
- removed redundant image_viewer member
File Changes
- *M* IPython/core/displayhook.py
<https://github.com/ipython/ipython/pull/10610/files#diff-0> (41)
- *M* IPython/terminal/interactiveshell.py
<https://github.com/ipython/ipython/pull/10610/files#diff-1> (4)
Patch Links:
- https://github.com/ipython/ipython/pull/10610.patch
- https://github.com/ipython/ipython/pull/10610.diff
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#10610>, or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAUez9FeZEophMdzDyFlC4ixdHuEYSlAks5r-cyygaJpZM4NovJA>
.
|
This comment has been minimized.
This comment has been minimized.
|
I looked at the iterm2-tools code but it is unclear what it is supposed to do in IPython, since that is not documented and the code does not, in fact, work, with the current IPython. There is at least no suggestion in the source that it is trying to anything with image/png mime types, or that it even enables the generation of that mime type in terminal IPython. The example image shows some arrows being added to the IPython prompts. |
| image_data = format_dict.get("image/png") | ||
| if image_data is not None and self.view_image(image_data): | ||
| return | ||
|
|
This comment has been minimized.
This comment has been minimized.
Carreau
May 28, 2017
Member
Hum that seem to be wrong, you likely do not want to modify anything in IPython.core as it will affect kernels regardless of the frontend. You likely want to get the data in the frontend and display it. It might be hard to find I'll see if I can have shot I finding where to put this.
This comment has been minimized.
This comment has been minimized.
stephanh42
May 29, 2017
Author
Hi @Carreau,
I moved the code from core/displayhook.py to the RichPromptDisplayHook in terminal/prompts.py . Hope this is better.
| md_dict : dict (optional) | ||
| The metadata dict to be associated with the display data. | ||
| """ | ||
| image_data = format_dict.get("image/png") |
This comment has been minimized.
This comment has been minimized.
Carreau
Jun 1, 2017
Member
I think we might want to dispatch on image/* but i'm ok starting with just image/png
This comment has been minimized.
This comment has been minimized.
stephanh42
Jun 3, 2017
Author
I wanted to avoid a situation where we also have to specify a list of valid MIME types for each viewer. The ones I checked all support png.
This comment has been minimized.
This comment has been minimized.
Carreau
Jun 5, 2017
Member
I wanted to avoid a situation where we also have to specify a list of valid MIME types for each viewer. The ones I checked all support png.
That's fair, and I don't think many backend do return anything other than PNG.
| try: | ||
| # We don't want to clutter the display with errors on failure, | ||
| # so redirect stderr to DEVNULL. | ||
| subprocess.check_call(tuple(image_viewer) + (png_file,), stderr=subprocess.DEVNULL) |
This comment has been minimized.
This comment has been minimized.
Carreau
Jun 1, 2017
Member
we likely need something more complex, as image viewer may take arguments to open in a non-blocking way.
This comment has been minimized.
This comment has been minimized.
stephanh42
Jun 3, 2017
Author
Hi @Carreau ,
Please note that I already allow options to come before the filename, by using shlex.split on the value of the environment variable. Do you think something even more complex is needed, i.e. a syntax like
viewer -some_option %s -another_option
where %s is then replaced by the actual filename?
Stephan
This comment has been minimized.
This comment has been minimized.
Carreau
Jun 3, 2017
Member
Ah I missed the shlex.split(), no I think this is fine. I"m wondering if we should use the normal IPython config system that allow different option based on profile or sub-application. I was also thinking that we might want to extend this mechnism to other mimetype (I don't see why an MP3, or GeoJson file could not have a viewer), but that a question for likely another PR.
This comment has been minimized.
This comment has been minimized.
stephanh42
Jun 3, 2017
Author
Hi @Carreau,
Yes, I was wondering about the configurable too. Perhaps that is better after all.
Perhaps a syntax like this:
c.TerminalIPythonApp.ExternalViewers = {
"image/png": "imgcat",
"video/mp4": "some-video-player"
}
I can make this change if you wish.
Stephan
This comment has been minimized.
This comment has been minimized.
Carreau
Jun 5, 2017
Member
Let's get that (with just images) in first. We can figure out the rest after. I'm trying to have it work locally.
This comment has been minimized.
This comment has been minimized.
Carreau
Jun 5, 2017
Member
ok, so that works with blocking image viewers, not with non blocking one as the temporary directory will be torn down before the viewer actually read the the file. We'll have to (likely) attach the teardown to a sys.exit hook, or to the del method of RichPromptDisplayHook. Maybe with a cache to not let more than a couple of things open.
I'm also thinking that the viewer should likely not be a subprocess, but a python callable, so that you can hook into pure-python display system like for example inline images.
We'll have to also figure out a clean way to tell matplotlib it can use inline.
This comment has been minimized.
This comment has been minimized.
stephanh42
Jun 6, 2017
Author
Hi @Carreau,
The image viewers which use the terminal imaging capability are all blocking, but they execute very quickly. Basically, they just send a bunch of control sequences to stdout and then exit. I am not sure what kind of non-blocking viewers should be supported.
Viewers which show the result in an external window already work for matplotlib. But those don't rely on external programs.
This comment has been minimized.
This comment has been minimized.
|
I'm happier this way I think we can get something along these lines. Let's get |
This comment has been minimized.
This comment has been minimized.
Yes, but you don't need an external viewer to see int he terminal, it's relatively straitforward to write in pure python:
Which is way easier to ensure we have available (heck if it's less than 12 lines we can bundle it and special case it), so it would be nice to have the ability to have this be a python callable than to have to subprocess. An example of non-blocking viewer on OSX is |
This comment has been minimized.
This comment has been minimized.
|
Hi @Carreau, You bring up several issues, so let me discuss them point-by-point.
Thanks, Stephan |


stephanh42 commentedMay 28, 2017
Introduction
There are nowadays several terminal emulators which support showing images in the terminal.
On such terminal emulators the terminal IPython version can support the image/png mime type.
The following is a summary of such terminal emulators to the extend that I am aware of them.
In all these cases there is a simple program which takes the name of the image file and just displays
it on the terminal. E.g. for ITerm2 there is the catimg script which used like this:
catimg someimage.pngSimilar (but unfortunately different) programs exist for the other terminal emulators mentioned.
This pull request introduces an environment variable IPYTHON_IMAGE_VIEWER . When set, the terminal IPython will use the named program to view an image of type image/png .
Unresolved issues
%matplotlib inlinein the terminal but I couldn't figure out how to enable that.