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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Gullumluvl opened this issue Mar 14, 2018 · 5 comments

Comments

@Gullumluvl
Copy link

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
Copy link
Author

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.

@takluyver
Copy link
Owner

takluyver commented Mar 14, 2018 via email

@Gullumluvl
Copy link
Author

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

@Gullumluvl
Copy link
Author

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

@takluyver
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants