Simplest way to display a picture in a webapp with Genie.jl

I think the most immediate issue that you’re facing here is the path to image not being correct. I had to put my image into “public” directory for it to be successfully found, otherwise it throws a 404. Also, html() is not a recognized function, I have to use HTML(), all capital.

As for having a random image generated and rendered into HTML from memory, without writing to disk, I’m not sure… I managed to get a random image to generate every time I refresh the page with the following routes.jl code:

using Genie.Router, Images, ImageMagick
import Random.randstring

route("/") do
        newfilename = "$(randstring()).png"
        randimage = rand(RGB, 1000, 1000)
        ImageMagick.save("public/img/$newfilename", randimage)
        HTML("<img src='img/$newfilename' />")
end

A notable downside of this approach is that it generates and saves a random image into public/img, but doesn’t delete it thereafter. If I added code to delete the image at the end of the route() block, the image simply didn’t render when I visited the page. So definitely undesirable behavior if you plan for your web-app to be used a lot, for it to flood your disk with all sorts of data and never reclaim the used up space. If you find a better way, I would like to know about it!

1 Like