Merge dd1433a20d16e08be05625668af1936b19c5dffa into 9001d5f3853ad40149fd604e1ae98ce0bff82fae

This commit is contained in:
Michal Sojka 2022-01-04 12:11:52 +01:00 committed by GitHub
commit 18b83daf7e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 30 deletions

View File

@ -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); - 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. - save sessions into gnuplot scripts, to enable easy plot customization and reproducibility.

View File

@ -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: 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 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 `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 `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 # Package options and initialization

View File

@ -202,7 +202,7 @@ Structure containing the package global options, accessible through `Gnuplot.opt
- `default::Symbol`: default session name (default: `:default`) - `default::Symbol`: default session name (default: `:default`)
- `term::String`: default terminal for interactive use (default: empty string, i.e. use gnuplot settings); - `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)); - `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); - `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`) - `verbose::Bool`: verbosity flag (default: `false`)
- `preferred_format::Symbol`: preferred format to send data to gnuplot. Value must be one of: - `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() const options = Options()
function __init__() function __init__()
# Check whether we are running in an IJulia, Juno, VSCode or Pluto session. # Copied from Plots - insert GnuplotDisplay before text displays,
# (copied from Gaston.jl). # but after graphic displays such as IJulia.
options.gpviewer = !( insert!(Base.Multimedia.displays, findlast(x -> x isa Base.TextDisplay || x isa REPL.REPLDisplay, Base.Multimedia.displays) + 1, GnuplotDisplay())
((isdefined(Main, :IJulia) && Main.IJulia.inited) || atreplinit(i -> begin
(isdefined(Main, :Juno) && Main.Juno.isactive()) || while GnuplotDisplay() in Base.Multimedia.displays
(isdefined(Main, :VSCodeServer)) || popdisplay(GnuplotDisplay())
(isdefined(Main, :PlutoRunner)) ) end
) insert!(Base.Multimedia.displays, findlast(x -> x isa REPL.REPLDisplay, Base.Multimedia.displays) + 1, GnuplotDisplay())
end)
if isdefined(Main, :VSCodeServer) if isdefined(Main, :VSCodeServer)
# VS Code shows "dynamic" plots with fixed and small size :-( # VS Code shows "dynamic" plots with fixed and small size :-(
options.mime[MIME"image/svg+xml"] = replace(options.mime[MIME"image/svg+xml"], "dynamic" => "") 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) reset(gp)
gpexec(sid, "set term unknown") gpexec(sid, "set term unknown")
driver(sid, "set table '$tmpfile'", args...; kw...) 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") gpexec(sid, "unset table")
quit(sid) quit(sid)
out = readlines(tmpfile) out = readlines(tmpfile)
@ -771,21 +776,20 @@ function reset(gp::Session)
gpexec(gp, "set output") gpexec(gp, "set output")
gpexec(gp, "reset session") gpexec(gp, "reset session")
if options.gpviewer # Configure the gnuplot viewer. If options.gpviewer is false, it would be more
# Use gnuplot viewer # appropriate to do this in display(), but doing so leads to hang of the gnuplot
(options.term != "") && gpexec(gp, "set term " * options.term) # 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) # Set window title (if not already set)
term = writeread(gp, "print GPVAL_TERM")[1] term = writeread(gp, "print GPVAL_TERM")[1]
if term in ("aqua", "x11", "qt", "wxt") if term in ("aqua", "x11", "qt", "wxt")
opts = writeread(gp, "print GPVAL_TERMOPTIONS")[1] opts = writeread(gp, "print GPVAL_TERMOPTIONS")[1]
if findfirst("title", opts) == nothing if findfirst("title", opts) == nothing
writeread(gp, "set term $term $opts title 'Gnuplot.jl: $(gp.sid)'") writeread(gp, "set term $term $opts title 'Gnuplot.jl: $(gp.sid)'")
end
end end
else
# Use external viewer
gpexec(gp, "set term unknown")
end end
# Note: the reason to keep Options.term and .init separate are: # 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) function show(obj::SessionID)
gp = getsession(obj.sid) gp = getsession(obj.sid)
@info "Gnuplot session" sid=gp.sid datasets=length(gp.datas) plots=length(gp.plots) @info "Gnuplot session" sid=gp.sid datasets=length(gp.datas) plots=length(gp.plots)

View File

@ -5,7 +5,9 @@ try
catch catch
Gnuplot.options.dry = true Gnuplot.options.dry = true
end end
Gnuplot.options.gpviewer = true
# Test the default, i.e. false
#Gnuplot.options.gpviewer = true
x = [1, 2, 3] x = [1, 2, 3]
y = [4, 5, 6] y = [4, 5, 6]
@ -110,6 +112,24 @@ dummy = terminals()
# Force unknown on Travis CI # Force unknown on Travis CI
Gnuplot.options.term = "unknown" 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 @gp 1:9
@info "using terminal: " terminal() @info "using terminal: " terminal()
#test_terminal("unknown") #test_terminal("unknown")