Creating a PNG-image of a VegaLite-diagram directly in memory?

Hello,

I created the diagram fig with VegaLite.jl and want to send it via e-mail by using SMTPClient.jl without creating a temporary file, but using IOBuffer to do it directly in the memory.
I tried this here, but it does not work:

io = IOBuffer()

VegaLite.save(FileIO.Stream{format"PNG"}(io), fig, "png")
png_data = take!(io)
image_data = base64encode(png_data)

html_e-mail = html_e-mail * "<img src=\"data:image/png;base64,$image_data\">"

Error message:

ERROR: LoadError: UndefVarError: @format_str not defined

Does somebody have an idea what is wrong with
VegaLite.save(FileIO.Stream{format"PNG"}(io), fig, "png")
or knows another method how to send a VegaLite.jl diagram via e-mail without creating a file?

Thanks for any advices in advance!

Kind regards,
Miroslav Stimac

2 Likes

I succeeded to get the Base64-encoded string of a PlotlyJS plot (which is similar, I think, with VegaLite) as follows:

using PlotlyJS, FileIO, Base64

#Create a plot:

n=255
I = [i for i in 0:n, j in 0:n]
width=400; height=375
plt=Plot(heatmap(z=mod.(xor.(I, I'), 53), colorscale=colors.algae, showscale=false ),
                 Layout(width=width, height=height, yaxis_autorange="reverse"))       

io = IOBuffer()
PlotlyJS.savefig(io, plt; width=width, height=height, scale=1, format="png") 
bytes = take!(io)
bs64_string= "data:image/png;base64,$(stringmime(MIME("image/png"), bytes))"

#Display image from the bs64 string
Plot(PlotlyJS.image(; source=bs64_string, hoverinfo="none"), 
     Layout(xaxis_visible=false, yaxis_visible=false))

and here is the notebook illustrating the initial plot, and its image displayed from the base64-string.

1 Like