Skip to content

Commit 26aeac1

Browse files
committed
move initialization code to init.jl
1 parent b3b6248 commit 26aeac1

2 files changed

Lines changed: 240 additions & 234 deletions

File tree

src/PyPlot.jl

Lines changed: 1 addition & 234 deletions
Original file line numberDiff line numberDiff line change
@@ -49,241 +49,8 @@ function Base.Docs.catdoc(hs::LazyHelp...)
4949
end
5050

5151
###########################################################################
52-
# global PyObject constants that get initialized at runtime. We
53-
# initialize them here (rather than via "global const foo = ..." in __init__)
54-
# so that their type is known at compile-time.
55-
56-
# remove this once we tag and require a newer PyCall version:
57-
if isdefined(PyCall,:PyNULL)
58-
PyNULL() = PyCall.PyNULL()
59-
else
60-
PyNULL() = PyCall.PyObject()
61-
end
62-
63-
const matplotlib = PyNULL()
64-
const plt = PyNULL()
65-
const Gcf = PyNULL()
66-
const orig_draw = PyNULL()
67-
const orig_gcf = PyNULL()
68-
const orig_figure = PyNULL()
69-
const orig_show = PyNULL()
70-
const mplot3d = PyNULL()
71-
const axes3D = PyNULL()
72-
const art3D = PyNULL()
73-
74-
###########################################################################
75-
# file formats supported by Agg backend, from MIME types
76-
const aggformats = Dict("application/eps" => "eps",
77-
"image/eps" => "eps",
78-
"application/pdf" => "pdf",
79-
"image/png" => "png",
80-
"application/postscript" => "ps",
81-
"image/svg+xml" => "svg")
82-
83-
function isdisplayok()
84-
for mime in keys(aggformats)
85-
if displayable(mime)
86-
return true
87-
end
88-
end
89-
false
90-
end
91-
92-
###########################################################################
93-
# We allow the user to turn on or off the Python gui interactively via
94-
# pygui(true/false). This is done by loading pyplot with a GUI backend
95-
# if possible, then switching to a Julia-display backend (if available)
96-
97-
# return (backend,gui) tuple
98-
function find_backend(matplotlib::PyObject)
99-
gui2matplotlib = Dict(:wx=>"WXAgg",:gtk=>"GTKAgg",:gtk3=>"GTK3Agg",
100-
:qt_pyqt4=>"Qt4Agg", :qt_pyqt5=>"Qt5Agg",
101-
:qt_pyside=>"Qt4Agg", :qt4=>"Qt4Agg",
102-
:qt5=>"Qt5Agg", :qt=>"Qt4Agg",:tk=>"TkAgg")
103-
conda = PyCall.conda || !isempty(PyCall.anaconda_conda())
104-
if is_linux()
105-
guis = [:tk, :gtk3, :gtk, :qt5, :qt4, :wx]
106-
elseif is_apple() && conda # partially work around #164
107-
guis = [:qt5, :qt4, :tk, :wx, :gtk, :gtk3]
108-
else
109-
guis = [:tk, :qt4, :qt5, :wx, :gtk, :gtk3]
110-
end
111-
options = [(g,gui2matplotlib[g]) for g in guis]
112-
113-
matplotlib2gui = Dict("wx"=>:wx, "wxagg"=>:wx,
114-
"gtkagg"=>:gtk, "gtk"=>:gtk,"gtkcairo"=>:gtk,
115-
"gtk3agg"=>:gtk3, "gtk3"=>:gtk3,"gtk3cairo"=>:gtk3,
116-
"qt5agg"=>:qt5, "qt4agg"=>:qt4, "tkagg"=>:tk,
117-
"agg"=>:none,"ps"=>:none,"pdf"=>:none,
118-
"svg"=>:none,"cairo"=>:none,"gdk"=>:none,
119-
"module://gr.matplotlib.backend_gr"=>:gr)
120-
121-
qt2gui = Dict("pyqt5"=>:qt_pyqt5, "pyqt4"=>:qt_pyqt4, "pyside"=>:qt_pyside)
122-
123-
rcParams = PyDict(matplotlib["rcParams"])
124-
default = lowercase(get(ENV, "MPLBACKEND", "none"))
125-
if default == "none"
126-
default = lowercase(get(rcParams, "backend", "none"))
127-
if is_windows() && startswith(default, "qt")
128-
default = "tkagg" # workaround issue #278
129-
end
130-
end
131-
if haskey(matplotlib2gui,default)
132-
defaultgui = matplotlib2gui[default]
133-
134-
# if the user explicitly requested a particular GUI,
135-
# it makes sense to ensure that the relevant Conda
136-
# package is installed (if we are using Conda).
137-
if conda
138-
if defaultgui == :qt || defaultgui == :qt4
139-
# default to pyqt rather than pyside, as below:
140-
defaultgui = haskey(rcParams,"backend.qt4") ? qt2gui[lowercase(rcParams["backend.qt4"])] : :qt_pyqt4
141-
if defaultgui == :qt_pyside
142-
pyimport_conda("PySide", "pyside")
143-
else
144-
try
145-
pyimport_conda("PyQt5", "pyqt")
146-
catch
147-
pyimport("PyQt4")
148-
end
149-
end
150-
elseif defaultgui == :qt5
151-
pyimport_conda("PyQt5", "pyqt")
152-
elseif defaultgui == :wx
153-
pyimport_conda("wx", "wxpython")
154-
end
155-
end
156-
157-
insert!(options, 1, (defaultgui,default))
158-
end
159-
160-
try
161-
# We will get an exception when we import pyplot below (on
162-
# Unix) if an X server is not available, even though
163-
# pygui_works and matplotlib.use(backend) succeed, at
164-
# which point it will be too late to switch backends. So,
165-
# throw exception (drop to catch block below) if DISPLAY
166-
# is not set. [Might be more reliable to test
167-
# success(`xdpyinfo`), but only if xdpyinfo is installed.]
168-
if options[1][1] != :none && is_unix() && !is_apple()
169-
ENV["DISPLAY"]
170-
end
171-
172-
if PyCall.gui == :default
173-
# try to ensure that GUI both exists and has a matplotlib backend
174-
for (g,b) in options
175-
if g == :none # Matplotlib is configured to be non-interactive
176-
pygui(:default)
177-
matplotlib["use"](b)
178-
matplotlib["interactive"](false)
179-
return (b, g)
180-
elseif g == :gr
181-
return (b, g)
182-
elseif PyCall.pygui_works(g)
183-
# must call matplotlib.use *before* loading backends module
184-
matplotlib["use"](b)
185-
if g == :qt || g == :qt4
186-
if haskey(rcParams,"backend.qt4")
187-
g = qt2gui[lowercase(rcParams["backend.qt4"])]
188-
elseif !PyCall.pyexists("PyQt5") && !PyCall.pyexists("PyQt4")
189-
# both Matplotlib and PyCall default to PyQt4
190-
# if it is available, but we need to tell
191-
# Matplotlib to use PySide otherwise.
192-
rcParams["backend.qt4"] = "PySide"
193-
end
194-
end
195-
if pyexists("matplotlib.backends.backend_" * lowercase(b))
196-
isjulia_display[1] || pygui_start(g)
197-
matplotlib["interactive"](!isjulia_display[1] && Base.isinteractive())
198-
return (b, g)
199-
end
200-
end
201-
end
202-
error("no gui found") # go to catch clause below
203-
else # the user specified a desired backend via pygui(gui)
204-
gui = pygui()
205-
matplotlib["use"](gui2matplotlib[gui])
206-
if (gui==:qt && !PyCall.pyexists("PyQt5") && !PyCall.pyexists("PyQt4")) || gui==:qt_pyside
207-
rcParams["backend.qt4"] = "PySide"
208-
end
209-
isjulia_display[1] || pygui_start(gui)
210-
matplotlib["interactive"](!isjulia_display[1] && Base.isinteractive())
211-
return (gui2matplotlib[gui], gui)
212-
end
213-
catch e
214-
if !isjulia_display[1]
215-
warn("No working GUI backend found for matplotlib")
216-
isjulia_display[1] = true
217-
end
218-
pygui(:default)
219-
matplotlib["use"]("Agg") # GUI not available
220-
matplotlib["interactive"](false)
221-
return ("Agg", :none)
222-
end
223-
end
224-
225-
# initialization -- anything that depends on Python has to go here,
226-
# so that it occurs at runtime (while the rest of PyPlot can be precompiled).
227-
function __init__()
228-
global const isjulia_display = Bool[isdisplayok()]
229-
copy!(matplotlib, pyimport_conda("matplotlib", "matplotlib"))
230-
global const version = try
231-
convert(VersionNumber, matplotlib[:__version__])
232-
catch
233-
v"0.0" # fallback
234-
end
235-
236-
backend_gui = find_backend(matplotlib)
237-
# workaround JuliaLang/julia#8925
238-
global const backend = backend_gui[1]
239-
global const gui = backend_gui[2]
240-
241-
copy!(plt, pyimport("matplotlib.pyplot")) # raw Python module
24252

243-
pytype_mapping(plt["Figure"], Figure)
244-
245-
copy!(Gcf, pyimport("matplotlib._pylab_helpers")["Gcf"])
246-
copy!(orig_gcf, plt["gcf"])
247-
copy!(orig_figure, plt["figure"])
248-
plt["gcf"] = gcf
249-
plt["figure"] = figure
250-
251-
if isdefined(Main, :IJulia) && Main.IJulia.inited
252-
Main.IJulia.push_preexecute_hook(force_new_fig)
253-
Main.IJulia.push_postexecute_hook(display_figs)
254-
Main.IJulia.push_posterror_hook(close_figs)
255-
end
256-
257-
if isjulia_display[1] && gui != :gr && backend != "Agg"
258-
plt["switch_backend"]("Agg")
259-
plt["ioff"]()
260-
end
261-
262-
copy!(mplot3d, pyimport("mpl_toolkits.mplot3d"))
263-
copy!(axes3D, pyimport("mpl_toolkits.mplot3d.axes3d"))
264-
265-
copy!(art3D, pyimport("mpl_toolkits.mplot3d.art3d"))
266-
267-
init_colormaps()
268-
end
269-
270-
function pygui(b::Bool)
271-
if !b != isjulia_display[1]
272-
if backend != "Agg"
273-
plt["switch_backend"](b ? backend : "Agg")
274-
if b
275-
pygui_start(gui) # make sure event loop is started
276-
Base.isinteractive() && plt["ion"]()
277-
else
278-
plt["ioff"]()
279-
end
280-
elseif b
281-
error("No working GUI backend found for matplotlib.")
282-
end
283-
isjulia_display[1] = !b
284-
end
285-
return b
286-
end
53+
include("init.jl")
28754

28855
###########################################################################
28956
# Figure methods

0 commit comments

Comments
 (0)