Display png image in IntelliJ

Does anyone know whether it is possible in IntellJ to display an image rather than text when it displays the toString value?

I’ve seen this done using Jupyter, and also using emacs. I don’t know how it’s done?

Basically each of the instances of a certain class in my application correspond to tree-like diagrams. I can generate the .png file via an external call to graphviz. It would be really cool for demo purposes if InteliJ scratch mode could simply display these trees, rather than cryptic text.

Has anyone seen anything like this before?

Scalafiddle can do such things, I believe

Agreed, but the graphviz integration probably won’t work – remember that Scalafiddle runs client-side, so you’re basically restricted to what’s available in it.

You could probably display the tree yourself using Scalafiddle: if you look at its docs and examples, it has machinery to render to the canvas, and a simple-looking line-drawing library. But I don’t see a straightforward way to call out to an external tool to generate a PNG…

My plan is to use the scratch mode of IntellJ during a live demo.
I can just write a function which takes such an object and opens up a separate window to display it. I haven’t looked, but I assume there’s a Scala function which lets me run a given shell command with specified arguments…

You can open the .png image in intellij. You can use the idea.sh script, or you can install https://plugins.jetbrains.com/plugin/6027-remote-call and use an http call to open in.

@nafg, is your idea that I would still need to make an explicit call to trigger something being displayed in the browser? Or are you suggesting that the image could display directly in the results pane of the Scala worksheet?

No it wouldn’t show in the worksheet. You can make it show in intellij in a separate tab, or in a browser or other program.

I see. well since i’m running on the Mac, I can just call a shell command “view file.png” to pop up an x-window displaying the image.

It would be really cool if it could show up in the worksheet incorporated into the printed representation of any object. I don’t know how that works in emacs nor in IntelliJ, but in emacs, you can put some kind of hook on the display of an object. Knowing emacs it is probably something connected to regular expression pattern matching and mode hooks. I have even less idea how it works in the Jupyter notebook.

Another alternative is to install an IntelliJ Markdown plugin. You can use for example this:

![alt text](gf2.png "A Tilte")

in markdown text. The IDE will the render the Markdown showing the image (default configuration). BTW combining this with Tut or MDoc will also allow you to incorporate code into your text. SBT plugins already exists.

will that allow the toString method to output a string such as """![alt text](gf2.png "A Tilte")""" and have it show the image automatically when the system prints the object?

Not sure I follow the question.

If you want to view a generated image in the browser without saving it to disk first, you could generate a base64 date uri and open that in the browser.

@nafg, sorry, let me clarify my response. I already have a method which I can call on an instance of my class which will popup a window and display the image, via a temp file. So calling that method with something like obj.viewObj() gets the job done. A solution such as typing text such like """![alt text](gf2.png "A Tilte")"" does not really help if it displays the image elsewhere.

I was hope for a solution where the worksheet itself could recognize that the object is being printed, and rather than displaying the text of the toString method, instead would run my method convertToImageThisAndThat and display that image directly in the results window.

This model is already used successfully in environments such as emacs and Jupyter. Jupiter does this (for Python) by calling a method (whose name I don’t know) like .convertToSvg if such is callable, and displays that image, but if that method does not exist, then it simply displays the test of the equivalent of the .toString method.

I hope that explanation helps.

You have two options:

  • You can use the toString's output to write the link into a Markdown file.
  • You can use Tut for example to embed Scala code into the document to output the link (á lá Jupyter notebook style).

In the IDE you only have to have the file open and it will display the content. As far as printing is concerned, I have never tried that.

Edit: neglected to mention that if you use Tut or MDoc you have to open the processed file found in the target directory.