Skip to content

Commit 6f94b05

Browse files
committed
working Preferences
1 parent 2f493a5 commit 6f94b05

File tree

6 files changed

+99
-109
lines changed

6 files changed

+99
-109
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ NaNMath = "77ba4419-2d1f-58cd-9bb1-8ffee604a2e3"
2121
Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"
2222
PlotThemes = "ccf2f8ad-2431-5c83-bf29-c5338b663b6a"
2323
PlotUtils = "995b91a9-d308-5afd-9ec6-746e21dbc043"
24+
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
2425
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
2526
REPL = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"
2627
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
@@ -58,6 +59,7 @@ PlotUtils = "1"
5859
PlotlyBase = "0.7 - 0.8"
5960
PlotlyJS = "0.18"
6061
PlotlyKaleido = "1"
62+
Preferences = "1"
6163
PyCall = "1"
6264
PyPlot = "2"
6365
RecipesBase = "1.3.1"

src/Plots.jl

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,8 @@ function _check_compat(sim::Module)
2929
end
3030
end
3131

32-
using Dates, Printf, Statistics, Base64, LinearAlgebra, Random
33-
using SparseArrays
34-
using Base.Meta
35-
using Requires
36-
using Reexport
32+
using Base.Meta, Dates, Printf, Statistics, Base64, LinearAlgebra, SparseArrays, Random
33+
using SnoopPrecompile, Preferences, Requires, Reexport
3734
using Unzip
3835
@reexport using RecipesBase
3936
@reexport using PlotThemes
@@ -167,6 +164,7 @@ using .PlotMeasures
167164
import .PlotMeasures: Length, AbsoluteLength, Measure, width, height
168165
# ---------------------------------------------------------
169166

167+
const PLOTS_SEED = 1234
170168
const PX_PER_INCH = 100
171169
const DPI = PX_PER_INCH
172170
const MM_PER_INCH = 25.4
@@ -193,20 +191,10 @@ include("backends.jl")
193191
include("output.jl")
194192
include("ijulia.jl")
195193
include("fileio.jl")
194+
include("shorthands.jl")
195+
include("backends/web.jl")
196196

197-
# Use fixed version of Plotly instead of the latest one for stable dependency
198-
# Ref: https://github.com/JuliaPlots/Plots.jl/pull/2779
199-
const _plotly_min_js_filename = "plotly-2.6.3.min.js"
200197
const CURRENT_BACKEND = CurrentBackend(:none)
201-
const PLOTS_SEED = 1234
202-
203198
include("init.jl")
204199

205-
include("backends/plotly.jl")
206-
include("backends/web.jl")
207-
include("backends/gr.jl")
208-
209-
include("shorthands.jl")
210-
include("precompile.jl")
211-
212200
end

src/backends.jl

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ backend_name() = CURRENT_BACKEND.sym
1616
_backend_instance(sym::Symbol)::AbstractBackend =
1717
haskey(_backendType, sym) ? _backendType[sym]() : error("Unsupported backend $sym")
1818

19+
backend_package_name() = _backend_packages[backend_name()]
1920
backend_package_name(sym::Symbol) = _backend_packages[sym]
2021

2122
macro init_backend(s)
@@ -131,21 +132,20 @@ CurrentBackend(sym::Symbol) = CurrentBackend(sym, _backend_instance(sym))
131132

132133
# ---------------------------------------------------------
133134

134-
_fallback_default_backend() = backend(GRBackend())
135-
136-
function _pick_default_backend()
137-
if (env_default = get(ENV, "PLOTS_DEFAULT_BACKEND", "")) != ""
138-
if (sym = Symbol(lowercase(env_default))) in _backends
139-
backend(sym)
140-
else
141-
@warn """You have set PLOTS_DEFAULT_BACKEND=$env_default, but it is not a valid backend package.
142-
Choose from: \n\t$(join(sort(_backends), "\n\t"))
143-
"""
144-
_fallback_default_backend()
145-
end
146-
else
147-
_fallback_default_backend()
148-
end
135+
function load_default_backend()
136+
default_backend = get(ENV, "PLOTS_DEFAULT_BACKEND", "gr")
137+
CURRENT_BACKEND.sym = @load_preference("backend", default_backend) |> lowercase |> Symbol
138+
backend(CURRENT_BACKEND.sym)
139+
nothing
140+
end
141+
142+
set_backend!(backend::Union{AbstractString,Symbol} = "gr"; kw...) =
143+
set_preferences!(Plots, "backend" => lowercase(string(backend)); kw...)
144+
145+
function diagnostics()
146+
from_env = haskey(ENV, "PLOTS_DEFAULT_BACKEND")
147+
@info "Selected `Plots` backend: $(backend()), from $(from_env ? "environment variable" : "`Preferences`")"
148+
nothing
149149
end
150150

151151
# ---------------------------------------------------------
@@ -154,16 +154,18 @@ end
154154
Returns the current plotting package name. Initializes package on first call.
155155
"""
156156
function backend()
157-
CURRENT_BACKEND.sym === :none && _pick_default_backend()
157+
CURRENT_BACKEND.sym === :none && load_default_backend()
158158
CURRENT_BACKEND.pkg
159159
end
160160

161+
initialized(sym::Symbol) = sym _initialized_backends
162+
161163
"""
162164
Set the plot backend.
163165
"""
164166
function backend(pkg::AbstractBackend)
165167
sym = backend_name(pkg)
166-
if sym _initialized_backends
168+
if !initialized(sym)
167169
_initialize_backend(pkg)
168170
push!(_initialized_backends, sym)
169171
end
@@ -286,11 +288,6 @@ function _initialize_backend(pkg::AbstractBackend)
286288
end
287289
end
288290

289-
# ------------------------------------------------------------------------------
290-
# gr
291-
292-
_initialize_backend(pkg::GRBackend) = nothing # COV_EXCL_LINE
293-
294291
const _gr_attr = merge_with_base_supported([
295292
:annotations,
296293
:legend_background_color,

src/backends/gr.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
# https://github.com/jheinen/GR.jl - significant contributions by @jheinen
22

3-
import GR
4-
export GR
5-
63
const gr_projections = (auto = 1, ortho = 1, orthographic = 1, persp = 2, perspective = 2)
74
const gr_linetypes = (auto = 1, solid = 1, dash = 2, dot = 3, dashdot = 4, dashdotdot = -1)
85
const gr_fill_styles = ((/) = 9, (\) = 10, (|) = 7, (-) = 8, (+) = 11, (x) = 6)

src/init.jl

Lines changed: 73 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,12 @@ using Scratch
33
using REPL
44

55
const plotly_local_file_path = Ref{Union{Nothing,String}}(nothing)
6-
const BACKEND_PATH_GASTON = @path joinpath(@__DIR__, "backends", "gaston.jl")
7-
const BACKEND_PATH_HDF5 = @path joinpath(@__DIR__, "backends", "hdf5.jl")
8-
const BACKEND_PATH_INSPECTDR = @path joinpath(@__DIR__, "backends", "inspectdr.jl")
9-
const BACKEND_PATH_PLOTLYBASE = @path joinpath(@__DIR__, "backends", "plotlybase.jl")
10-
const BACKEND_PATH_PGFPLOTS =
11-
@path joinpath(@__DIR__, "backends", "deprecated", "pgfplots.jl")
12-
const BACKEND_PATH_PGFPLOTSX = @path joinpath(@__DIR__, "backends", "pgfplotsx.jl")
13-
const BACKEND_PATH_PLOTLYJS = @path joinpath(@__DIR__, "backends", "plotlyjs.jl")
14-
const BACKEND_PATH_PYPLOT = @path joinpath(@__DIR__, "backends", "pyplot.jl")
15-
const BACKEND_PATH_UNICODEPLOTS = @path joinpath(@__DIR__, "backends", "unicodeplots.jl")
6+
7+
backend_path(sym) = @path joinpath(@__DIR__, "backends", "$sym.jl")
8+
9+
# Use fixed version of Plotly instead of the latest one for stable dependency
10+
# Ref: https://github.com/JuliaPlots/Plots.jl/pull/2779
11+
const _plotly_min_js_filename = "plotly-2.6.3.min.js"
1612

1713
_plots_defaults() =
1814
if isdefined(Main, :PLOTS_DEFAULTS)
@@ -64,44 +60,50 @@ function __init__()
6460
end,
6561
)
6662

67-
@require HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f" begin
68-
include(BACKEND_PATH_HDF5)
63+
_plots_plotly_defaults()
64+
65+
@require GR = "28b8d3ca-fb5f-59d9-8090-bfdbd6d07a71" begin
66+
initialized(:gr) || include(backend_path(:gr))
6967
end
7068

71-
@require InspectDR = "d0351b0e-4b05-5898-87b3-e2a8edfddd1d" begin
72-
include(BACKEND_PATH_INSPECTDR)
69+
@require PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee" begin
70+
initialized(:pyplot) || include(backend_path(:pyplot))
7371
end
7472

7573
@require PGFPlots = "3b7a836e-365b-5785-a47d-02c71176b4aa" begin
76-
include(BACKEND_PATH_PGFPLOTS)
74+
initialized(:pgfplots) || include(backend_path(:pgfplots))
75+
end
76+
77+
@require PGFPlotsX = "8314cec4-20b6-5062-9cdb-752b83310925" begin
78+
initialized(:pgfplotsx) || include(backend_path(:pgfplotsx))
7779
end
7880

7981
@require PlotlyBase = "a03496cd-edff-5a9b-9e67-9cda94a718b5" begin
8082
@require PlotlyKaleido = "f2990250-8cf9-495f-b13a-cce12b45703c" begin
81-
include(BACKEND_PATH_PLOTLYBASE)
83+
initialized(:plotly) || include(backend_path(:plotly))
84+
initialized(:plotlybase) || include(backend_path(:plotlybase))
8285
end
8386
end
8487

85-
@require PGFPlotsX = "8314cec4-20b6-5062-9cdb-752b83310925" begin
86-
include(BACKEND_PATH_PGFPLOTSX)
87-
end
88-
8988
@require PlotlyJS = "f0f68f2c-4968-5e81-91da-67840de0976a" begin
90-
include(BACKEND_PATH_PLOTLYJS)
89+
initialized(:plotly) || include(backend_path(:plotly))
90+
initialized(:plotlyjs) || include(backend_path(:plotlyjs))
9191
end
9292

93-
_plots_plotly_defaults()
93+
@require UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228" begin
94+
initialized(:unicodeplots) || include(backend_path(:unicodeplots))
95+
end
9496

95-
@require PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee" begin
96-
include(BACKEND_PATH_PYPLOT)
97+
@require Gaston = "4b11ee91-296f-5714-9832-002c20994614" begin
98+
initialized(:gaston) || include(backend_path(:gaston))
9799
end
98100

99-
@require UnicodePlots = "b8865327-cd53-5732-bb35-84acbb429228" begin
100-
include(BACKEND_PATH_UNICODEPLOTS)
101+
@require InspectDR = "d0351b0e-4b05-5898-87b3-e2a8edfddd1d" begin
102+
initialized(:inspectdr) || include(backend_path(:inspectdr))
101103
end
102104

103-
@require Gaston = "4b11ee91-296f-5714-9832-002c20994614" begin
104-
include(BACKEND_PATH_GASTON)
105+
@require HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f" begin
106+
initialized(:hdf5) || include(backend_path(:hdf5))
105107
end
106108

107109
@require IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a" begin
@@ -163,3 +165,46 @@ function __init__()
163165
@reexport using .UnitfulRecipes
164166
end
165167
end
168+
169+
##################################################################
170+
backend() # get from `Preferences` or env, and initialize backend
171+
172+
# needs to be split from `init` for precompilation, because `__init__` is ran after parsing
173+
@eval const $(backend_package_name()) = Main.$(backend_package_name())
174+
175+
include(backend_path(backend_name()))
176+
177+
# COV_EXCL_START
178+
if get(ENV, "PLOTS_PRECOMPILE", "true") == "true"
179+
@precompile_setup begin
180+
n = length(_examples)
181+
imports = sizehint!(Expr[], n)
182+
examples = sizehint!(Expr[], 10n)
183+
for i in setdiff(1:n, _backend_skips[backend_name()], _animation_examples)
184+
_examples[i].external && continue
185+
(imp = _examples[i].imports) === nothing || push!(imports, imp)
186+
func = gensym(string(i))
187+
push!(examples, quote
188+
$func() = begin # evaluate each example in a local scope
189+
$(_examples[i].exprs)
190+
if $i == 1 # only for one example
191+
fn = tempname()
192+
pl = current()
193+
show(devnull, pl)
194+
Sys.iswindows() || savefig(pl, "$fn.png")
195+
Sys.iswindows() || savefig(pl, "$fn.pdf")
196+
end
197+
nothing
198+
end
199+
$func()
200+
end)
201+
end
202+
withenv("GKSwstype" => "nul") do
203+
@precompile_all_calls begin
204+
eval.(imports)
205+
eval.(examples)
206+
end
207+
end
208+
end
209+
end
210+
# COV_EXCL_STOP

src/precompile.jl

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)