bash_kernel icon indicating copy to clipboard operation
bash_kernel copied to clipboard

Display image file or any other supported filetype the same way as the ipython kernel

Open Gullumluvl opened this issue 6 years ago • 5 comments

I am not sure you will consider this feature request as useful, but in the ipython kernel you can display an image using:

from IPython.display import Image
Image(filename="myimg.png")

Which allows you to display a file in any of your system folder (as opposed to markdown which can't go above the notebook directory).

Also I find that it might be useful to directly pipe any output from a bash command to be displayed in the cell output (as I would do in a regular terminal, like interactively displaying a matplotlib figure using the Qt of Tk backend).

I tried:

python3 -c '
from IPython.display import Image
Image(filename="../fig/myimg.png")'
python3 -c '
from IPython.display import display, Image
display(Image(filename="../fig/myimg.png"))'
python3 -c '
from sys import exit
from IPython.display import Image
exit(Image(filename="../fig/myimg.png"))'

and using ipython3 -c instead of python -c, unsurprisingly without success.

Right now I just symlink the images to be in the jupyter tree and display them with markdown, but it's a convoluted workflow.

I wonder if you would find it interesting and doable (something to add in the kernel wrapper?).

Gullumluvl avatar Mar 14 '18 09:03 Gullumluvl

Oh wait, sorry, I just saw in Issue #23 that I can do:

cat ../fig/myimg.png | display

PS: Unfortunately, although display can normally show svg, here it doesn't work.

Gullumluvl avatar Mar 14 '18 10:03 Gullumluvl

Do you want to have a look at the code (images.py) and see if it's easy to extend for SVG?

On Wed, 14 Mar 2018, 10:06 a.m. Gullumluvl, [email protected] wrote:

Oh wait, sorry, I just saw in Issue #23 https://github.com/takluyver/bash_kernel/issues/23 that I can do:

cat ../fig/myimg.png | display

PS: Unfortunately, although display can normally show svg, here it doesn't work.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/takluyver/bash_kernel/issues/76#issuecomment-372967737, or mute the thread https://github.com/notifications/unsubscribe-auth/AAUA9XtKblG-Nhbz2sVXE9C0MR21dmpOks5teOuwgaJpZM4SqIki .

takluyver avatar Mar 14 '18 10:03 takluyver

Amazing, I can have a look into it yes :)

Gullumluvl avatar Mar 14 '18 10:03 Gullumluvl

Recognizing SVG file is not as straightforward as other binary image formats supported by imghdr. As described here, one could check for valid xml structure with the presence of a svg top-element, but the solution presented would require adding a large dependency (xml.etree).

I think the check for a valid xml structure can be dropped, and a regex match would be safe enough, anyway it won't render if the data is broken, but maybe I'm not cautious enough.

When I have time I will try to add something, and maybe make a pull request? :D

Gullumluvl avatar Mar 16 '18 14:03 Gullumluvl

Thanks! I think it's fine to load the first 100 bytes or so and just see if it contains <svg.

A step beyond that might be to make it so display can take a filename (as well as reading data from stdin) - then it can also use the file extension to guess the format.

takluyver avatar Mar 16 '18 14:03 takluyver