PlotlyJS: Select Browser to display html output

I have tried to find advise, how to specify the browser that is used to display the interactive html-ouput from PlotlyJS:
I found some discussion how to change the default browser, but I would not like to change the the default browser but I would like to specify the browser which opens the PlotlyJS output. Therefore, the discussion here does not seem to be the right method:

On Windows my preference to display the PlotlyJS output is the browser edge, the strange thing is, this was not my system wide default browser, but it was the one who was selected to display the PlotlyJS output, unfortunately, this setting has changed (probably due to a package update) now I wonder, if I can change back to the previous setting.

Hello you can try somethig like this:

using PlotlyJS
# Define the thing you want to plot
f(x) = exp(-0.1*x^2)*cos(x)
p = plot(f,-10,10)

#save it as an .html file
savefig(p, "myplot.html")

# using run() to open the html file with an specified browser
# Open the saved plot file in Firefox browser

run(`"C:\\Program Files\\Mozilla Firefox\\firefox.exe" myplot.html`)

That should make the trick, but if you are using a different operating system or different browser, you need to modify the path of executable and run() (or analogue) sintax accordingly.

Thanks for the idea! - Another workaround is: you plot all the time in the same file and open this file with your preferred browser …
Better would be, if I could specify the default browser for PlotlyJS-outputs …

You are welcome!

I usually work with jupyterlab() notebooks from the IJulia package, so this allows to create folders and files for each project you are working on.

Usually, I plot with gr() from the Plots package or with PyPlot which are displayed inside the Jupyter notebooks. When plotting with PlotlyJS in Jupyterlab I have experienced some troubles related to the WebIO extension, so instead, I use that previous code to save the figure, but if the file is not strictly needed, I just copy the plot code in the Julia REPL and their default output works for me.

I don’t think this change was caused by Julia or a package update. The method PlotlyJS uses from PlotlyBase is to just open .html files directly with the Windows system, and your preference for this is set in Windows itself.

If you really want to ensure you will launch PlotlyJS plots, then you can overwrite the PlotlyBase.launch_browser method:

using PlotlyJS
import PlotlyJS: PlotlyBase.launch_browser
# The run command here is specific for Windows:
launch_browser(tmppath::String) = run(`cmd /c start msedge $tmppath`)

Then this launches in MS Edge:

julia> p = Plot(x -> cos(x),-10,10)

but notice that this has uppercase Plot.

If you use lowercase plot then the default is to use Blink and launch an Electron window:

julia> p = plot(x -> cos(x),-10,10)

If you want to by-pass Blink in this case, then you need to overwrite PlotlyJS’s Base.display method:

Base.display(::PlotlyJS.PlotlyJSDisplay, p::PlotlyJS.SyncPlot) = display(p.plot)

but of course, you lose the JavaScript callback functionality.

2 Likes

More of another potential workaround rather than a solution, but I have been using this browser selector for some time on my windows machine:

You set this as default browser and then specify which browser to actually use for each url based on regular expressions

1 Like

@jd-foster Thanks jd!
I will add this to my startup.jl :slight_smile:

1 Like

Thanks for this proposal!