fonsp / Pluto.jl

🎈 Simple reactive notebooks for Julia
https://plutojl.org/
MIT License
4.94k stars 285 forks source link

Specify backend while "using Plots" with PlutoPkg #1305

Open DebChanda opened 3 years ago

DebChanda commented 3 years ago

I am using the built-in package manager of Pluto. Is there a working example where I can see how to use different backends with Plots, currently gr and plotly are working by default. pyplot gives

ArgumentError: Package PyPlot not found in current path:
- Run `import Pkg; Pkg.add("PyPlot")` to install the PyPlot package.

    top-level scope@backends.jl:507
    eval@boot.jl:360[inlined]
    _initialize_backend(::Plots.PyPlotBackend)@backends.jl:506
    backend@backends.jl:174[inlined]
    var"#pyplot#259"(::Base.Iterators.Pairs{Union{}, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}, ::typeof(Plots.pyplot))@backends.jl:31
    pyplot@backends.jl:31[inlined]
    top-level scope@Local: 1[inlined]
lllusion3418 commented 3 years ago

One workaround to get Pluto to add PyPlot.jl to the environment is to simply import it like this (instead of using PyPlot as not to pollute the scope): Screenshot_20210711_190749

But the user experience with this error message is not ideal - there's no need to give up on PlutoPkg (which is what the suggested snippet would do) just to be able to use the PyPlot backend.

fonsp commented 3 years ago

Like @lllusion3418 suggested, import the backend package before importing Plots, eg:

begin
    import PyPlot
    using Plots
end

By importing it, you tell Pluto that the package should be installed in the env, making it available to Plots.jl.

fonsp commented 3 years ago

The choice to shield your notebook from knowing about a global installation of PyPlot is deliberate: we want notebooks to run independently of what packages you have installed elsewhere.

If not isolated, you might have a hidden dependency on the global PyPlot installation. Someone else opening your notebook would not be able to run it.

fonsp commented 3 years ago

@lllusion3418 (and others) feel free to add a PR to "detect and rewrite" that error message. Take a look at https://github.com/fonsp/Pluto.jl/blob/main/frontend/components/ErrorMessage.js

DebChanda commented 3 years ago

Suggestion of @lllusion3418 works for me, you can close this issue. Or, if you want to keep it open to discuss about the error message, thats also fine. Also, you can add this suggestion in the documentation of PlutoPkg, so others can easily find it.