Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove custom default backends #4870

Merged
merged 11 commits into from
Jan 23, 2024
8 changes: 3 additions & 5 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
PlotThemes = "ccf2f8ad-2431-5c83-bf29-c5338b663b6a"
PlotUtils = "995b91a9-d308-5afd-9ec6-746e21dbc043"
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
Expand Down Expand Up @@ -64,7 +63,7 @@ FixedPointNumbers = "0.6 - 0.8"
GR = "0.69.5 - 0.73"
Gaston = "1"
HDF5 = "0.16"
InspectDR = "0.4"
InspectDR = "0.5"
JLFzf = "0.1"
JSON = "0.21, 1"
LaTeXStrings = "1"
Expand All @@ -79,7 +78,6 @@ PlotlyBase = "0.7 - 0.8"
PlotlyJS = "0.18"
PlotlyKaleido = "1"
PrecompileTools = "1"
Preferences = "1"
PyPlot = "2"
PythonPlot = "1 - 1.0.2"
RecipesBase = "1.3.1"
Expand All @@ -95,7 +93,7 @@ UnicodeFun = "0.4"
UnicodePlots = "3.4"
UnitfulLatexify = "1"
Unzip = "0.1 - 0.2"
julia = "1.6"
julia = "1.10"

[extras]
Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595"
Expand Down Expand Up @@ -132,4 +130,4 @@ Unitful = "1986cc42-f94f-5a68-af5c-568840ba703d"
VisualRegressionTests = "34922c18-7c2a-561c-bac1-01e79b2c4c92"

[targets]
test = ["Aqua", "Colors", "Distributions", "FileIO", "FilePathsBase", "FreeType", "Gaston", "GeometryBasics", "Gtk", "ImageMagick", "Images", "InspectDR", "LibGit2", "OffsetArrays", "PGFPlotsX", "PlotlyJS", "PlotlyBase", "PyPlot", "PythonPlot", "PlotlyKaleido", "HDF5", "RDatasets", "SentinelArrays", "StableRNGs", "StaticArrays", "StatsPlots", "Test", "TestImages", "UnicodePlots", "Unitful", "VisualRegressionTests"]
test = ["Aqua", "Colors", "Distributions", "FileIO", "FilePathsBase", "FreeType", "Gaston", "GeometryBasics", "Gtk", "ImageMagick", "Images", "LibGit2", "OffsetArrays", "PGFPlotsX", "PlotlyJS", "PlotlyBase", "PyPlot", "PythonPlot", "PlotlyKaleido", "HDF5", "RDatasets", "SentinelArrays", "StableRNGs", "StaticArrays", "StatsPlots", "Test", "TestImages", "UnicodePlots", "Unitful", "VisualRegressionTests"]
2 changes: 1 addition & 1 deletion src/Plots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if isdefined(Base, :Experimental) && isdefined(Base.Experimental, Symbol("@max_m
end

using Pkg, Dates, Printf, Statistics, Base64, LinearAlgebra, SparseArrays, Random
using PrecompileTools, Preferences, Reexport, RelocatableFolders
using PrecompileTools, Reexport, RelocatableFolders
using Base.Meta
@reexport using RecipesBase
@reexport using PlotThemes
Expand Down
26 changes: 2 additions & 24 deletions src/backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,35 +194,13 @@ end
CurrentBackend(sym::Symbol) = CurrentBackend(sym, _backend_instance(sym))

# ---------------------------------------------------------
# from github.com/JuliaPackaging/Preferences.jl/blob/master/README.md:
# "Preferences that are accessed during compilation are automatically marked as compile-time preferences"
# ==> this must always be done during precompilation, otherwise
# the cache will not invalidate when preferences change
const PLOTS_DEFAULT_BACKEND = lowercase(load_preference(Plots, "default_backend", "gr"))
const PLOTS_DEFAULT_BACKEND = "gr"

function load_default_backend()
# environment variable preempts the `Preferences` based mechanism
CURRENT_BACKEND.sym =
get(ENV, "PLOTS_DEFAULT_BACKEND", PLOTS_DEFAULT_BACKEND) |> lowercase |> Symbol
CURRENT_BACKEND.sym = :gr
backend(CURRENT_BACKEND.sym)
end

function set_default_backend!(
backend::Union{Nothing,AbstractString,Symbol} = nothing;
force = true,
kw...,
)
if backend === nothing
delete_preferences!(Plots, "default_backend"; force, kw...)
else
# NOTE: `_check_installed` already throws a warning
if (value = lowercase(string(backend))) |> _check_installed !== nothing
set_preferences!(Plots, "default_backend" => value; force, kw...)
end
end
nothing
end

function diagnostics(io::IO = stdout)
origin = if has_preference(Plots, "default_backend")
"`Preferences`"
Expand Down
6 changes: 3 additions & 3 deletions src/examples.jl
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ const _examples = PlotExample[
"3D axis flip / mirror",
:(using LinearAlgebra),
quote
with(scalefonts = 0.5) do
Plots.with(scalefonts = 0.5) do
x, y = collect(-6:0.5:10), collect(-8:0.5:8)

args = x, y, (x, y) -> sinc(norm([x, y]) / π)
Expand Down Expand Up @@ -1198,7 +1198,7 @@ const _examples = PlotExample[
legs,
)
w, h = Plots._plot_defaults[:size]
with(scalefonts = 0.5, size = (2w, 2h)) do
Plots.with(scalefonts = 0.5, size = (2w, 2h)) do
plot(leg_plots()..., leg_plots(legend_column = -1)...; layout = (6, 3))
end
end,
Expand Down Expand Up @@ -1230,7 +1230,7 @@ const _examples = PlotExample[
legs,
)
w, h = Plots._plot_defaults[:size]
with(scalefonts = 0.5, size = (2w, 2h)) do
Plots.with(scalefonts = 0.5, size = (2w, 2h)) do
plot(leg_plots()..., leg_plots(legend_column = -1)...; layout = (6, 3))
end
end,
Expand Down
9 changes: 2 additions & 7 deletions src/init.jl
BeastyBlacksmith marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ include(_path(backend_name()))
n = length(_examples)
imports = sizehint!(Expr[], n)
examples = sizehint!(Expr[], 10n)
scratch_dir = mktempdir()
for i in setdiff(1:n, _backend_skips[backend_name()], _animation_examples)
_examples[i].external && continue
(imp = _examples[i].imports) === nothing || push!(imports, imp)
Expand All @@ -99,15 +100,9 @@ include(_path(backend_name()))
$func() = begin # evaluate each example in a local scope
$(_examples[i].exprs)
$i == 1 || return # only for one example
fn = tempname()
fn = joinpath(scratch_dir, tempname())
pl = current()
show(devnull, pl)
# FIXME: pgfplotsx requires bug
backend_name() === :pgfplotsx && return
if backend_name() === :unicodeplots
savefig(pl, "$fn.txt")
return
end
showable(MIME"image/png"(), pl) && savefig(pl, "$fn.png")
showable(MIME"application/pdf"(), pl) && savefig(pl, "$fn.pdf")
if showable(MIME"image/svg+xml"(), pl)
Expand Down
2 changes: 1 addition & 1 deletion src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ allFunctions(arg) = trueOrAllTrue(a -> isa(a, Function), arg)
"""
Allows temporary setting of backend and defaults for Plots. Settings apply only for the `do` block. Example:
```
with(:gr, size=(400,400), type=:histogram) do
Plots.with(:gr, size=(400,400), type=:histogram) do
plot(rand(10))
plot(rand(10))
end
Expand Down
14 changes: 1 addition & 13 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ using VisualRegressionTests
using RecipesPipeline
using FilePathsBase
using LaTeXStrings
using Preferences
using RecipesBase
using TestImages
using Unitful
Expand All @@ -23,12 +22,8 @@ using Dates
using Test
using Gtk # see JuliaPlots/VisualRegressionTests.jl/issues/30

# get `Preferences` set backend, if any
const PREVIOUS_DEFAULT_BACKEND = load_preference(Plots, "default_backend")

# NOTE: don't use `plotly` (test hang, not surprised), test only the backends used in the docs
const TEST_BACKENDS =
:gr, :unicodeplots, :pythonplot, :pgfplotsx, :plotlyjs, :gaston, :inspectdr
const TEST_BACKENDS = :gr, :unicodeplots, :pythonplot, :pgfplotsx, :plotlyjs, :gaston

# initial load - required for `should_warn_on_unsupported`
unicodeplots()
Expand Down Expand Up @@ -61,7 +56,6 @@ for name in (
"plotly",
"animations",
"output",
"preferences",
"backends",
)
@testset "$name" begin
Expand All @@ -73,9 +67,3 @@ for name in (
include("test_$name.jl")
end
end

if PREVIOUS_DEFAULT_BACKEND === nothing
delete_preferences!(Plots, "default_backend") # restore the absence of a preference
else
Plots.set_default_backend!(PREVIOUS_DEFAULT_BACKEND) # reset to previous state
end
2 changes: 1 addition & 1 deletion test/test_axes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
["500.×10^{0}", "1.00×10^{3}", "1.50×10^{3}"]
@test Plots.get_labels(:latex, 1:3, :identity) == ["\$1\$", "\$2\$", "\$3\$"]
# GR is used during tests and it correctly overrides labelfunc(), but PGFPlotsX did not
with(:pgfplotsx) do
Plots.with(:pgfplotsx) do
@test Plots.get_labels(:auto, 1:3, :log10) == ["10^{1}", "10^{2}", "10^{3}"]
end
@test Plots.get_labels(:auto, 1:3, :log10) == ["10^{1}", "10^{2}", "10^{3}"]
Expand Down
14 changes: 7 additions & 7 deletions test/test_backends.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,25 @@ end
## Uncomment the following lines to update reference images for different backends
#=

with(:gr) do
Plots.with(:gr) do
image_comparison_facts(:gr, tol = PLOTS_IMG_TOL, skip = Plots._backend_skips[:gr])
end

with(:plotlyjs) do
Plots.with(:plotlyjs) do
image_comparison_facts(:plotlyjs, tol = PLOTS_IMG_TOL, skip = Plots._backend_skips[:plotlyjs])
end

with(:pyplot) do
Plots.with(:pyplot) do
image_comparison_facts(:pyplot, tol = PLOTS_IMG_TOL, skip = Plots._backend_skips[:pyplot])
end

with(:pgfplotsx) do
Plots.with(:pgfplotsx) do
image_comparison_facts(:pgfplotsx, tol = PLOTS_IMG_TOL, skip = Plots._backend_skips[:pgfplotsx])
end
=#

@testset "UnicodePlots" begin
with(:unicodeplots) do
Plots.with(:unicodeplots) do
@test backend() == Plots.UnicodePlotsBackend()

io = IOContext(IOBuffer(), :color => true)
Expand Down Expand Up @@ -190,7 +190,7 @@ end
push!(blacklist, 50) # NOTE: remove when github.com/jheinen/GR.jl/issues/507 is resolved

@testset "GR - reference images" begin
with(:gr) do
Plots.with(:gr) do
# NOTE: use `ENV["VISUAL_REGRESSION_TESTS_AUTO"] = true;` to automatically replace reference images
@test backend() == Plots.GRBackend()
@test backend_name() === :gr
Expand All @@ -203,7 +203,7 @@ push!(blacklist, 50) # NOTE: remove when github.com/jheinen/GR.jl/issues/507 i
end

is_pkgeval() || @testset "PlotlyJS" begin
with(:plotlyjs) do
Plots.with(:plotlyjs) do
@test backend() == Plots.PlotlyJSBackend()
pl = plot(rand(10))
@test pl isa Plot
Expand Down
6 changes: 3 additions & 3 deletions test/test_misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ end
end

@testset "NoFail" begin
with(:unicodeplots) do
Plots.with(:unicodeplots) do
@test backend() == Plots.UnicodePlotsBackend()

dsp = TextDisplay(IOContext(IOBuffer(), :color => true))
Expand Down Expand Up @@ -223,7 +223,7 @@ end

@testset "wrap" begin
# not sure what is intended here ...
wrapped = wrap([:red, :blue])
wrapped = Plots.wrap([:red, :blue])
@test !isempty(wrapped)
@test scatter(1:2, color = wrapped) isa Plots.Plot
end
Expand All @@ -241,7 +241,7 @@ end
@test plot(skipmissing(1:5)) isa Plots.Plot
end

with(:gr) do
Plots.with(:gr) do
@testset "text" begin
io = PipeBuffer()
x = y = range(-3, 3, length = 10)
Expand Down
18 changes: 9 additions & 9 deletions test/test_output.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ macro test_save(fmt)
end |> esc
end

with(:gr) do
Plots.with(:gr) do
@test Plots.defaultOutputFormat(plot()) == "png"
@test Plots.addExtension("foo", "bar") == "foo.bar"

Expand All @@ -39,14 +39,14 @@ with(:gr) do
@test_save :ps
end

with(:unicodeplots) do
Plots.with(:unicodeplots) do
@test_save :txt
if Plots.UnicodePlots.get_font_face() ≢ nothing
@test_save :png
end
end

with(:plotlyjs) do
Plots.with(:plotlyjs) do
@test_save :html
@test_save :json
@test_save :pdf
Expand All @@ -55,21 +55,21 @@ with(:plotlyjs) do
# @test_save :eps
end

with(:plotly) do
Plots.with(:plotly) do
@test_save :pdf
@test_save :png
@test_save :svg
@test_save :html
end

if Sys.islinux() && Sys.which("pdflatex") ≢ nothing
with(:pgfplotsx) do
Plots.with(:pgfplotsx) do
@test_save :tex
@test_save :png
@test_save :pdf
end

with(:pythonplot) do
Plots.with(:pythonplot) do
@test_save :pdf
@test_save :png
@test_save :svg
Expand All @@ -79,14 +79,14 @@ if Sys.islinux() && Sys.which("pdflatex") ≢ nothing
end

#=
with(:gaston) do
Plots.with(:gaston) do
@test_save :png
@test_save :pdf
@test_save :eps
@test_save :svg
end

with(:inspectdr) do
Plots.with(:inspectdr) do
@test_save :png
@test_save :pdf
@test_save :eps
Expand All @@ -95,7 +95,7 @@ end
=#

@testset "html" begin
with(:gr) do
Plots.with(:gr) do
io = PipeBuffer()
pl = plot(1:2)
pl.attr[:html_output_format] = :auto
Expand Down
2 changes: 1 addition & 1 deletion test/test_pgfplotsx.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function get_pgf_axes(pl)
Plots.pgfx_axes(pl.o)
end

with(:pgfplotsx) do
Plots.with(:pgfplotsx) do
pl = plot(1:5)
axis = first(get_pgf_axes(pl))
@test pl.o.the_plot isa PGFPlotsX.TikzDocument
Expand Down
2 changes: 1 addition & 1 deletion test/test_plotly.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Plots, Test
with(:plotly) do
Plots.with(:plotly) do
@testset "Basic" begin
@test backend() == Plots.PlotlyBackend()

Expand Down
Loading
Loading