Using environment variables with Plots

Hi!

I’m using the Plots (v1.41.1) package to create and save figures in Julia. Currently, I’m using PlotlyJS as backend and everything works well. The problem is that my application does not have access to internet so I want the rendered HTML figures from Plots to point to a local plotly JavaScript file. My current solution is that I simply change the content of the HTML files by string-handling and regexp which obviously is not something that I prefer…

I have looked into the source code of Plots and found that there exists an environment variable PLOTSBASE_HOST_DEPENDENCY_LOCAL which appears to do what I want. My problem is that it doesn’t work and the HTML is still pointing to an online JavaScript resource. Can someone help me how to use the environment variables in Plots package correct?

My current implementation looks something like this:

using Scratch

# Copy a local plotly.js file into the "plotly" scratch space.
# This is how I interpret the use of the ENV-variable in init.jl code in PlotsBase.
plotly_scratch = @get_scratch!("plotly")
source_path = joinpath(@__DIR__, "plotly-2.3.0.min.js")
dest_path = joinpath(plotly_scratch, "plotly-2.3.0.min.js")
cp(source_path, dest_path; force=true)

# Set the environment variable.
ENV["PLOTSBASE_HOST_DEPENDENCY_LOCAL"] = "1"

# Rebuild the Plots packages to make sure the init code is evaluated again.
Pkg.build("PlotlyBase")
Pkg.build("PlotlyKaleido")
Pkg.build("PlotlyJS")
Pkg.build("Plots")

using Plots

plotlyjs()
plt = plot([1, 3], [10, 30])
savefig(plt, "plt.html")

I think you need to give it the path to the plotly file in that environment variable.

Also you seem to read the documentation for Plots 2.0 (which is not released) on Plots v1.x the variable is called PLOTS_HOST_DEPENDENCY_LOCAL

1 Like