Merge dd1433a20d16e08be05625668af1936b19c5dffa into 9001d5f3853ad40149fd604e1ae98ce0bff82fae
This commit is contained in:
commit
18b83daf7e
@ -25,7 +25,7 @@ The **Gnuplot.jl** package allows easy and fast use of [gnuplot](http://gnuplot.
|
||||
|
||||
- export to a huge number of formats such as `pdf`, `png`, `gif`, ``\LaTeX``, `svg`, etc. (actually all those supported by gnuplot);
|
||||
|
||||
- compatibility with Jupyter and Juno;
|
||||
- compatibility with Jupyter, VS Code, Pluto, Juno, Weave.jl, etc.
|
||||
|
||||
- save sessions into gnuplot scripts, to enable easy plot customization and reproducibility.
|
||||
|
||||
|
||||
@ -13,13 +13,11 @@ saveas(file) = save(term="pngcairo size 550,350 fontscale 0.8", output="assets/$
|
||||
|
||||
The display behaviour of **Gnuplot.jl** depends on the value of the `Gnuplot.options.gpviewer` flag:
|
||||
|
||||
- if `true` the plot is displayed in a gnuplot window, using one of the interactive terminals such as `wxt`, `qt` or `aqua`. This is the default setting when running a Julia REPL session; The terminal options can be customized using `Gnuplot.options.term`;
|
||||
|
||||
- if `false` the plot is displayed through the Julia [multimedia interface](https://docs.julialang.org/en/v1/base/io-network/#Multimedia-I/O-1), i.e. it is exported as either a `png`, `svg` or `html` file, and displayed in an external viewer. This is the default setting when running a Jupyter, JupyterLab or Juno session. The terminal options can be customized using the `Gnuplot.options.mime` dictionary.
|
||||
|
||||
The `Gnuplot.options.gpviewer` flag is automatically set when the package is first loaded according to the runtime environment, however the user can change its value at any time to fit specific needs. Further informations and examples for both options are available in this Jupyter [notebook](https://github.com/gcalderone/Gnuplot.jl/blob/gh-pages/v1.3.0/options/display.ipynb).
|
||||
- if `false` (the default) the plot is displayed through the Julia [multimedia interface](https://docs.julialang.org/en/v1/base/io-network/#Multimedia-I/O-1), i.e. depending on the current environment, it is displayed either in a gnuplot window (REPL) or exported as either a `png`, `svg` or `html` file, and displayed in an external viewer (IDE, notebook). The terminal options for the gnuplot window can be customized using `Gnuplot.options.term`; options for exporting can be customized using the `Gnuplot.options.mime` dictionary.
|
||||
|
||||
- if `true` the plot is always displayed in a gnuplot window, using one of the interactive terminals such as `wxt`, `qt` or `aqua`. The terminal options can be customized using `Gnuplot.options.term`;
|
||||
|
||||
Further information and examples for both options are available in this Jupyter [notebook](https://github.com/gcalderone/Gnuplot.jl/blob/gh-pages/v1.3.0/options/display.ipynb).
|
||||
|
||||
|
||||
# Package options and initialization
|
||||
|
||||
@ -202,7 +202,7 @@ Structure containing the package global options, accessible through `Gnuplot.opt
|
||||
- `default::Symbol`: default session name (default: `:default`)
|
||||
- `term::String`: default terminal for interactive use (default: empty string, i.e. use gnuplot settings);
|
||||
- `mime::Dict{DataType, String}`: dictionary of MIME types and corresponding gnuplot terminals. Used to export images with either [`save()`](@ref) or `show()` (see [Display options](@ref));
|
||||
- `gpviewer::Bool`: use a gnuplot terminal as main plotting device (if `true`) or an external viewer (if `false`);
|
||||
- `gpviewer::Bool`: force using a gnuplot terminal as main plotting device (if `true`) or use Julia multimedia I/O, which automatically selects between the gnuplot terminal and external viewers (if `false`);
|
||||
- `init::Vector{String}`: commands to initialize the session when it is created or reset (e.g., to set default palette);
|
||||
- `verbose::Bool`: verbosity flag (default: `false`)
|
||||
- `preferred_format::Symbol`: preferred format to send data to gnuplot. Value must be one of:
|
||||
@ -236,14 +236,15 @@ const sessions = OrderedDict{Symbol, Session}()
|
||||
const options = Options()
|
||||
|
||||
function __init__()
|
||||
# Check whether we are running in an IJulia, Juno, VSCode or Pluto session.
|
||||
# (copied from Gaston.jl).
|
||||
options.gpviewer = !(
|
||||
((isdefined(Main, :IJulia) && Main.IJulia.inited) ||
|
||||
(isdefined(Main, :Juno) && Main.Juno.isactive()) ||
|
||||
(isdefined(Main, :VSCodeServer)) ||
|
||||
(isdefined(Main, :PlutoRunner)) )
|
||||
)
|
||||
# Copied from Plots - insert GnuplotDisplay before text displays,
|
||||
# but after graphic displays such as IJulia.
|
||||
insert!(Base.Multimedia.displays, findlast(x -> x isa Base.TextDisplay || x isa REPL.REPLDisplay, Base.Multimedia.displays) + 1, GnuplotDisplay())
|
||||
atreplinit(i -> begin
|
||||
while GnuplotDisplay() in Base.Multimedia.displays
|
||||
popdisplay(GnuplotDisplay())
|
||||
end
|
||||
insert!(Base.Multimedia.displays, findlast(x -> x isa REPL.REPLDisplay, Base.Multimedia.displays) + 1, GnuplotDisplay())
|
||||
end)
|
||||
if isdefined(Main, :VSCodeServer)
|
||||
# VS Code shows "dynamic" plots with fixed and small size :-(
|
||||
options.mime[MIME"image/svg+xml"] = replace(options.mime[MIME"image/svg+xml"], "dynamic" => "")
|
||||
@ -590,6 +591,10 @@ function gp_write_table(args...; kw...)
|
||||
reset(gp)
|
||||
gpexec(sid, "set term unknown")
|
||||
driver(sid, "set table '$tmpfile'", args...; kw...)
|
||||
if !Gnuplot.options.gpviewer
|
||||
# driver() did not send plots to the gnuplot process - do it now
|
||||
execall(gp, term="unknown")
|
||||
end
|
||||
gpexec(sid, "unset table")
|
||||
quit(sid)
|
||||
out = readlines(tmpfile)
|
||||
@ -771,21 +776,20 @@ function reset(gp::Session)
|
||||
gpexec(gp, "set output")
|
||||
gpexec(gp, "reset session")
|
||||
|
||||
if options.gpviewer
|
||||
# Use gnuplot viewer
|
||||
(options.term != "") && gpexec(gp, "set term " * options.term)
|
||||
# Configure the gnuplot viewer. If options.gpviewer is false, it would be more
|
||||
# appropriate to do this in display(), but doing so leads to hang of the gnuplot
|
||||
# process in some situations (after manual close of the Qt window, gnuplot
|
||||
# receives the CAPTURE_END marker, but does not print it back). Therefore, we do
|
||||
# in here unconditionally.
|
||||
(options.term != "") && gpexec(gp, "set term " * options.term)
|
||||
|
||||
# Set window title (if not already set)
|
||||
term = writeread(gp, "print GPVAL_TERM")[1]
|
||||
if term in ("aqua", "x11", "qt", "wxt")
|
||||
opts = writeread(gp, "print GPVAL_TERMOPTIONS")[1]
|
||||
if findfirst("title", opts) == nothing
|
||||
writeread(gp, "set term $term $opts title 'Gnuplot.jl: $(gp.sid)'")
|
||||
end
|
||||
# Set window title (if not already set)
|
||||
term = writeread(gp, "print GPVAL_TERM")[1]
|
||||
if term in ("aqua", "x11", "qt", "wxt")
|
||||
opts = writeread(gp, "print GPVAL_TERMOPTIONS")[1]
|
||||
if findfirst("title", opts) == nothing
|
||||
writeread(gp, "set term $term $opts title 'Gnuplot.jl: $(gp.sid)'")
|
||||
end
|
||||
else
|
||||
# Use external viewer
|
||||
gpexec(gp, "set term unknown")
|
||||
end
|
||||
|
||||
# Note: the reason to keep Options.term and .init separate are:
|
||||
@ -1561,10 +1565,21 @@ end
|
||||
|
||||
|
||||
# ╭───────────────────────────────────────────────────────────────────╮
|
||||
# │ Interfacing Julia's show │
|
||||
# │ Interfacing Julia's Multimedia I/O │
|
||||
# ╰───────────────────────────────────────────────────────────────────╯
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
struct GnuplotDisplay <: AbstractDisplay end
|
||||
|
||||
function Base.display(d::GnuplotDisplay, obj::SessionID)
|
||||
if !options.gpviewer && obj.dump
|
||||
gp = getsession(obj.sid)
|
||||
execall(gp)
|
||||
else
|
||||
throw(MethodError(display, (d, obj)))
|
||||
end
|
||||
end
|
||||
|
||||
function show(obj::SessionID)
|
||||
gp = getsession(obj.sid)
|
||||
@info "Gnuplot session" sid=gp.sid datasets=length(gp.datas) plots=length(gp.plots)
|
||||
|
||||
@ -5,7 +5,9 @@ try
|
||||
catch
|
||||
Gnuplot.options.dry = true
|
||||
end
|
||||
Gnuplot.options.gpviewer = true
|
||||
|
||||
# Test the default, i.e. false
|
||||
#Gnuplot.options.gpviewer = true
|
||||
|
||||
x = [1, 2, 3]
|
||||
y = [4, 5, 6]
|
||||
@ -110,6 +112,24 @@ dummy = terminals()
|
||||
# Force unknown on Travis CI
|
||||
Gnuplot.options.term = "unknown"
|
||||
|
||||
if Gnuplot.options.gpviewer == false
|
||||
# Wrap @gp and @gsp macrocalls in display() to ensure that the whole
|
||||
# plot is sent to the gnuplot process during testing. In interactive
|
||||
# sessions display() is called by the REPL.
|
||||
macro gp(args...)
|
||||
gpcall = :(Gnuplot.@gp)
|
||||
push!(gpcall.args, args...)
|
||||
out = :(display($gpcall))
|
||||
return esc(out)
|
||||
end
|
||||
macro gsp(args...)
|
||||
gpcall = :(Gnuplot.@gsp)
|
||||
push!(gpcall.args, args...)
|
||||
out = :(display($gpcall))
|
||||
return esc(out)
|
||||
end
|
||||
end
|
||||
|
||||
@gp 1:9
|
||||
@info "using terminal: " terminal()
|
||||
#test_terminal("unknown")
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user