3

I need to share an interactive plot made using the PlotlyJS package in Julia. According to the documentation of the package PlotlyJS.jl, I need to use the "savehtml" function and set "js" argument to ":embed" in order to view it offline (screenshot attached). However, I got an error "UndefVarError: savehtml not defined". Can anyone tell me what may cause this problem?

FYI, the "savefig" function can save the plot into an HTML but the HTML cannot be viewed on other machines.

It is also acceptable if there is another way to save an HTML plot that can be assessed from other machines. The interactive plot is generated by PlotlyJS.jl.

Thanks very much in advance.

Hongyu
  • 183
  • 5
  • It seems at some point `savehtml` was replaced by a [`to_html` function](https://github.com/sglyon/PlotlyBase.jl/blob/4a56e048063a13660b9f9d53f018571bcf543e0f/src/output.jl#L40) (and the online docs website hasn't updated for some reason). I'm not familiar enough with the details of plotlyjs/javascript ecosystem to figure out the equivalent of `savehtml(... js = :embed)`, but the `include_plotlyjs` seems the most relevant option – Sundar R Jan 31 '22 at 23:36
  • (Let us know if you figure it out and are able to get a stand-alone HTML document, please. It appears to me that there's an undeclared variable usage `embed_plotlyjs` in the function if you directly specify the `.js` file path, so I'd be especially interested if that works.) – Sundar R Jan 31 '22 at 23:43
  • Sundar, there is a bug in the source code so when it goes to this line it crashes. should be easy to correct though ;) – Przemyslaw Szufel Jan 31 '22 at 23:55
  • Thanks Sundar and Przemyslaw! You are right, the code provided by Przemyslaw does the work. I appreciate your help! – Hongyu Feb 01 '22 at 09:38

1 Answers1

2

This creates a standalone file that can be used on other machines. However, those other machines need to have access to the internet:

p  = PlotlyJS.Plot(sin.(1:0.1:10))
open("f.html","w") do f
    PlotlyJS.PlotlyBase.to_html(f, p; include_plotlyjs="cdn", full_html=true)
end

I just checked that this is as far as you can do as of today (version v0.8.18) as there is a bug in the source code of PlotlyBase.

Przemyslaw Szufel
  • 40,002
  • 3
  • 32
  • 62
  • Thanks very much! I cannot even know there is a bug without you telling me. Thanks a lot! Your code works. :) – Hongyu Feb 01 '22 at 09:40
  • Has this issue been fixed yet? I am not able to execute the code listed in the accepted answer... – AaronJPung Mar 22 '22 at 19:39