Image in julia code comments or doc

hi,

I guess it is a silly idea but I often wish to insert small images (typically scan of manually drawn sketches) to better explain part of my codes. Ideally these images could be displayed by the editor (VSCode) or even displayed in the docstrings of a function.

Does anyone knows about a VSCode extension or other ways to do this ?

4 Likes

Perhaps not in the code itself, but have you considered

  • literate programming, Weave.jl and Literate.jl
  • notebooks, Pluto.jl
  • using Documenter.jl etc. to generate documentation for your code.

All of these alternatives allow you to associate images with your code in one way or another

4 Likes

Not the same thing, but FWIW:

By adding a comment to your Julia code with a link to the image file, when hovering over it VS Code will propose to ctrl + click it:

# file:///C:\Users\jrafa\Desktop\Coordinate_descent.png

function coordinate_descent(A::AbstractArray, index)
...

This opens instantaneously in a new VS Code tab:

3 Likes

It might be this issue:

2 Likes

Thank you for these directions !
@baggepinnen : I knew about Documenter.jl and Pluto.jl but the idea was to make this information in the code itself via vscode… In the same spirit, the latex formulas in docstrings are not directly rendered in vscode.
@rafael.guerra : This is closer to my wish. It just lacks to be open without a Ctrl-click and a size options .
@jzr : I did not understood all the thread but some quotes look close to what I am looking for. @davidanthoff contributes to this thread and maybe he could explain how it is related to Julia vscode extension.

As per your wishes:

# file:///C:\Users\jrafa\Desktop\Rosenbrock_image.png
using Plots; plotly()
rosenbrock(x,y) = (1 - x)^2 + 100(y - x^2)^2
...

It shows like this in VS Code when hovering cursor over the commented line:

and it proposes also to follow a link with a single click to open the image in a dedicated VS Code tab.

NB:
Cerise sur le gâteau: an automatic icon is displayed next to the code line number

3 Likes

This is exactly what I meant but… it does not work on my computer:

Please search the hover settings in VS Code and check that Editor > Hover: Enabled.
I’m on Windows here and short of other ideas.

NB:
as per @LaurentPlagne comment below, it requires Image preview VS Code extension

1 Like

This option is already enabled. This is VSCode 1.63.2 and macOS.

julia> versioninfo()
Julia Version 1.7.1
Commit ac5cc99908 (2021-12-22 19:35 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin19.5.0)
  CPU: Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, westmere)
Environment:
  JULIA_EDITOR = code
  JULIA_NUM_THREADS = 4

I will try with another OS. Thank you anyway !

OK, it works with Image preview extension (maybe you also have it installed).
Thanks !

1 Like

Yes, that’s right! And I had completely forgotten…

1 Like

Btw, regarding pictures in a docstring, since julias docstrings are rendered as Markdown by most sensible IDEs, one can insert pictures into docstrings via the regular Markdown syntax ![pic description](picture link), as in

"""
heres a smiley!
![smiley](https://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/640px-Smiley.svg.png)
"""
 smile!() = "😃"
1 Like